服务器:
public static void main(String[] args) throws IOException {
ServerSocket socketServer = new ServerSocket(8888);
while (true){
Socket socket = socketServer.accept();
InputStream is = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1){
String a = new String(bytes,0,len);
System.out.println(a);
}
is.close();
}
}
客户端:
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",8888);
OutputStream os = socket.getOutputStream();
String a = "你好";
os.write(a.getBytes());
os.close();
}