客户端 : java 访问服务器并不复杂, 只须要将主机名和 端口号传输给Socket 类的构造方法 java
public Socket(String host, int port)服务器
throws UnknownHostException, IOException并发
{socket
this(host != null ? new InetSocketAddress(host, port) :this
new InetSocketAddress(InetAddress.getByName(null), port),spa
(SocketAddress) null, true);orm
}对象
获取网路链接的地址: 使用InetAddress对象 ,经过getByName()方法来获取 get
socket 读写文本数据 :经过socket.getInputStream()或者getOutputStream方法 it
OutputStream os = socket.getOutputStream();
//写入并发送报文
os.write(MessageFormat.format("{0,number,00000000}", payload.length).getBytes("GBK"));
os.write(payload);
os.flush();
//接收报文
InputStream is = socket.getInputStream();