java - socket -客户端

 客户端 :  java 访问服务器并不复杂, 只需要将主机名和 端口号传输给Socket 类的构造方法   

                             public Socket(String host, int port)

                                    throws UnknownHostException, IOException

                                {

                                    this(host != null ? new InetSocketAddress(host, port) :

                                         new InetSocketAddress(InetAddress.getByName(null), port),

                                         (SocketAddress) null, true);

                                }

                获取网路连接的地址: 使用InetAddress对象 ,通过getByName()方法来获取 

                socket 读写文本数据 :通过socket.getInputStream()或者getOutputStream方法 


                        OutputStream os = socket.getOutputStream();

//写入并发送报文 

                        os.write(MessageFormat.format("{0,number,00000000}", payload.length).getBytes("GBK"));

os.write(payload);

os.flush();

                         //接收报文 

                        InputStream is = socket.getInputStream();

                                                                   

你可能感兴趣的:(socket)