Server
1.创建服务器指定端口
ServerSocket server =new ServerSocket();
2.接收客户端连接 阻塞式
Socket socket =server.accept(); System.out.println("客户端建立成功");
3.发送数据
String meg="欢迎使用"; //输出流
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()));
Client
1.创建客户端 必须指定服务器+端口
Socket client = new Socket("localhost",8888);
2.接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputSteram())); String echo = br.readLine(); System.out.println(echo);
网友评论