Server
1.創(chuàng)建服務(wù)器指定端口
ServerSocket server =new ServerSocket();
2.接收客戶端連接 阻塞式
Socket socket =server.accept(); System.out.println("客戶端建立成功");
3.發(fā)送數(shù)據(jù)
String meg="歡迎使用"; //輸出流
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()));
Client
1.創(chuàng)建客戶端 必須指定服務(wù)器+端口
Socket client = new Socket("localhost",8888);
2.接收數(shù)據(jù)
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputSteram())); String echo = br.readLine(); System.out.println(echo);