java socket例子

  1. server端
  2. package test.bwl;  
  3.   
  4. import java.io.DataInputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.IOException;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9.   
  10. public class SocketManager {  
  11.     /** 
  12.      * @param args 
  13.      * @throws IOException  
  14.      */  
  15.     public static void main(String[] args) {  
  16.         SocketManager manager = new SocketManager();  
  17.         manager.doListen();  
  18.     }  
  19.   
  20.     public void doListen() {  
  21.         ServerSocket server;  
  22.         try {  
  23.             server = new ServerSocket(9991);  
  24.             while (true) {  
  25.                 Socket client = server.accept();  
  26.                 new Thread(new SSocket(client)).start();  
  27.             }  
  28.         } catch (IOException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.   
  32.     }  
  33.   
  34.     //服务器进程  
  35.     class SSocket implements Runnable {  
  36.   
  37.         Socket client;  
  38.   
  39.         public SSocket(Socket client) {  
  40.             this.client = client;  
  41.         }  
  42.   
  43.         public void run() {  
  44.             DataInputStream input;  
  45.             DataOutputStream output;  
  46.             try {  
  47.                 input = new DataInputStream(client.getInputStream());  
  48.                 output = new DataOutputStream(client.getOutputStream());  
  49.                 //  
  50.                 String listMsg = input.readUTF();  
  51.                 output.writeUTF("Recive:  " + listMsg + "    \r\n Thx...");  
  52.                 System.out.println("Recive:   " + listMsg);  
  53.                 listMsg = input.readUTF();  
  54.                 output.writeUTF("Recive Second:  " + listMsg + "    \r\n Thx...");  
  55.                 System.out.println("Recive Second:   " + listMsg);  
  56.             } catch (IOException e) {  
  57.                 e.printStackTrace();  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62. }  
  63. Client端:
    1. package test.bwl;  
    2.   
    3. import java.io.DataInputStream;  
    4. import java.io.DataOutputStream;  
    5. import java.io.IOException;  
    6. import java.io.OutputStream;  
    7. import java.net.Socket;  
    8. import java.net.UnknownHostException;  
    9.   
    10. public class SocketClient {  
    11.   
    12.     public static void main(String[] args) {  
    13.         Socket socket = null;  
    14.         try {  
    15.             socket = new Socket("127.0.0.1"9991);  
    16.             //向服务器端第一次发送字符串     
    17.             OutputStream netOut = socket.getOutputStream();  
    18.             DataOutputStream doc = new DataOutputStream(netOut);  
    19.             DataInputStream in = new DataInputStream(socket.getInputStream());  
    20.             //向服务器端第二次发送字符串     
    21.             doc.writeUTF("list");  
    22.             String res = in.readUTF();  
    23.             System.out.println(res);  
    24.             doc.writeUTF("bye");  
    25.             res = in.readUTF();  
    26.             System.out.println(res);  
    27.             doc.close();  
    28.             in.close();  
    29.         } catch (UnknownHostException e) {  
    30.             e.printStackTrace();  
    31.         } catch (IOException e) {  
    32.             e.printStackTrace();  
    33.         } finally {  
    34.             if (socket != null) {  
    35.                 try {  
    36.                     socket.close();  
    37.                 } catch (IOException e) {  
    38.                 }  
    39.             }  
    40.         }  
    41.     }  
    42. }  
posted @ 2013-12-07 08:39  mr.g.  阅读(292)  评论(0编辑  收藏  举报