import java.net.*;
import java.io.*;
public class TestServer { //服务器端
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:" + br.readLine());
String str2 = br1.readLine();
while(!str2.equalsIgnoreCase("bye")){
bw.write(str2+"\n");
bw.flush();
//System.out.println("Server:" + str2);
System.out.println("Client:" + br.readLine());
str2 = br1.readLine();
}
br.close();
br1.close();
bw.close();
s.close();
ss.close();
}catch(IOException e){
e.printStackTrace();}
}
}
import java.net.*;
import java.io.*;
public class TestClient{ //客服端
public static void main(String[] args) {
try{
Socket s = new Socket("127.0.0.1",9999);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String str = br1.readLine();
while(!str.equalsIgnoreCase("bye")){
bw.write(str+"\n");
bw.flush();
//System.out.println("Client:" + str);
System.out.println("Server:" + br.readLine());
str = br1.readLine();
}
br.close();
br1.close();
bw.close();
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
|
|