package com.knightsight.chatroom;
import java.net.*;
import java.io.*;
/*
* 输入流与输出流独立,客户端程序
*/
public class Client {
public static void main(String[] args) throws IOException,UnknownHostException{
Socket client =new Socket("localhost",9900);
//控制台输入流
BufferedReader console=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
DataInputStream dis=new DataInputStream(client.getInputStream());
while(true){
String info=console.readLine();
//输出流
dos.writeUTF(info);
dos.flush();
//输入流
String str=dis.readUTF();
System.out.println(str);
}
}
}
package com.knightsight.chatroom;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(9900);
Socket socket=server.accept();
DataInputStream dis=new DataInputStream(socket.getInputStream());
String str=dis.readUTF();
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF("服务器已收到"+str);
dos.flush();
}
}