import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Server {
public static void main(String[] args){
try(ServerSocket ss=new ServerSocket(3001);Scanner in=new Scanner(System.in);){
//注意端口号是数字 在try中国定义的变量智能在try中使用
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
String str;
while(!(str=in.nextLine()).equals("exit")){
ps.println(str);
}
ps.println("exit");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args)throws IOException{
Socket socket=new Socket();
socket.connect(new InetSocketAddress("127.0.0.1",3001),10000);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=null;
while(!(line=br.readLine()).equals("exit"))
System.out.println(line);
br.close();
socket.close();
}
}