62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TCPClientB {
public static void main(String[] args) {
Socket s=null;
BufferedReader br=null;
PrintWriter pw=null;
try {
s=new Socket("127.0.0.1",8888);
BufferedReader brin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input your words:");
String str=brin.readLine();
pw=new PrintWriter(s.getOutputStream());
pw.println(str);
pw.flush();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(IOException e){}
if(s!=null)try{s.close();}catch(IOException e){}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerB {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintWriter pw=null;
BufferedReader br=null;
try {
ss=new ServerSocket(8888);
System.out.println("Server was Running on port 8888...");
int i=0;
while (true) {
s = ss.accept();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br.readLine();
i++;
System.out.println("第"+i+"个访问者:"+s.getInetAddress()+":"+str);
pw = new PrintWriter(s.getOutputStream());
pw.println("您是第"+i+"位访问者:" + str.toUpperCase());
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(pw!=null) pw.close();
if(br!=null)try{br.close();}catch(IOException e){}
if(s!=null)try{s.close();}catch(IOException e){}
if(ss!=null)try{ss.close();}catch(IOException e){}
}
}
}
String line = is.readLine();