1 public class Test { 2 3 public static void main(String[] args) throws Exception{ 4 5 //1. 构造ServerSocket实例,指定服务端口。 6 ServerSocket servSock = new ServerSocket(); 7 servSock.bind(new InetSocketAddress("192.168.1.35", 8400)); 8 System.out.println("socket服务端:"+servSock.getInetAddress()+":"+servSock.getLocalPort()+""); 9 10 while(true){ 11 // 2.调用accept方法,建立和客户端的连接 12 Socket client = servSock.accept(); 13 SocketAddress clientAddress =client.getRemoteSocketAddress(); 14 System.out.println("socket链接客户端:" + clientAddress); 15 new Thread(new SSocket(client)).start(); 16 } 17 } 18 19 } 20 21 //服务器进程 22 class SSocket implements Runnable { 23 24 Socket client; 25 26 public SSocket(Socket client) { 27 this.client = client; 28 } 29 30 public void run() { 31 try { 32 StringBuffer text=new StringBuffer(); 33 BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); 34 PrintStream ps = new PrintStream(client.getOutputStream()); 35 String msg; 36 //如果输入流不为空,将接受到的信息打印到相应的文本框中并反馈回收到的信息 37 if ((msg = br.readLine()) != null) { 38 text.append("服务器端收到:" + msg + " "); 39 ps.println(msg); 40 } 41 System.out.println(msg); 42 ps.close(); 43 br.close(); 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 } 48 }
此代码是接收来自客户端请求结果集的编程....
学习就是小进步吧..