代码如下
ServerSocket service = new ServerSocket(8888);
BufferedOutputStream output;
Scanner sc;
BufferedInputStream input ;
Socket client;
while(true){
client = service.accept();
input = new BufferedInputStream(client.getInputStream());
output = new BufferedOutputStream(client.getOutputStream());
sc = new Scanner(input);
System.out.println("用户上线了");
while(true){
String line = sc.nextLine();
System.out.println(line);
output.write(line.getBytes());//返回消息给客户端,我用的telnet 127.0.0.1 8888连接 serversocket的
if(line.toString().startsWith("end")){
System.out.println("as");
client.close();