黑马程序员技术交流社区

标题: Socket编写出分别在两台电脑上的dos命令行里互相通信的代码 [打印本页]

作者: 赵国刚    时间: 2013-8-16 12:32
标题: Socket编写出分别在两台电脑上的dos命令行里互相通信的代码
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();

}

}

}

作者: 神之梦    时间: 2013-8-16 14:15
哥们,你这基本没一句注释啊
作者: 赵国刚    时间: 2013-8-16 15:51
神之梦 发表于 2013-8-16 14:15
哥们,你这基本没一句注释啊

呃,忘加了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2