日期:2014-05-20 浏览次数:20954 次
public class TCPClient {
public static void main(String[] args){
try{
Socket s=new Socket("127.0.0.1",6668);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader brk=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str="";
do{
str=brk.readLine();
System.out.println("客户:"+str);
bw.write(str);
bw.flush();
}while(!str.equals("bye"));
//br.close();
bw.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TestTCPServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(6668);
Socket s = ss.accept();
System.out.println("有一个客户端连接上来了!!~·");
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//BufferedReader brk=new BufferedReader(new InputStreamReader(System.in));
//BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String str="";
do{
str=br.readLine();
System.out.println("客户:"+str);
}while(!str.equals("bye"));
br.close();
//bw.close();
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}