java socket例子

Server端: 

  1. package test.bwl;   
  2.   
  3. import java.io.DataInputStream;   
  4. import java.io.DataOutputStream;   
  5. import java.io.IOException;   
  6. import java.net.ServerSocket;   
  7. import java.net.Socket;   
  8.   
  9. public class SocketManager {   
  10.     /**  
  11.      * @param args  
  12.      * @throws IOException   
  13.      */  
  14.     public static void main(String[] args) {   
  15.         SocketManager manager = new SocketManager();   
  16.         manager.doListen();   
  17.     }   
  18.   
  19.     public void doListen() {   
  20.         ServerSocket server;   
  21.         try {   
  22.             server = new ServerSocket(9991);   
  23.             while (true) {   
  24.                 Socket client = server.accept();   
  25.                 new Thread(new SSocket(client)).start();   
  26.             }   
  27.         } catch (IOException e) {   
  28.             e.printStackTrace();   
  29.         }   
  30.   
  31.     }   
  32.   
  33.     //服务器进程   
  34.     class SSocket implements Runnable {   
  35.   
  36.         Socket client;   
  37.   
  38.         public SSocket(Socket client) {   
  39.             this.client = client;   
  40.         }   
  41.   
  42.         public void run() {   
  43.             DataInputStream input;   
  44.             DataOutputStream output;   
  45.             try {   
  46.                 input = new DataInputStream(client.getInputStream());   
  47.                 output = new DataOutputStream(client.getOutputStream());   
  48.                 //   
  49.                 String listMsg = input.readUTF();   
  50.                 output.writeUTF("Recive:  " + listMsg + "    /r/n Thx...");   
  51.                 System.out.println("Recive:   " + listMsg);   
  52.                 listMsg = input.readUTF();   
  53.                 output.writeUTF("Recive Second:  " + listMsg + "    /r/n Thx...");   
  54.                 System.out.println("Recive Second:   " + listMsg);   
  55.             } catch (IOException e) {   
  56.                 e.printStackTrace();   
  57.             }   
  58.         }   
  59.     }   
  60.   
  61. }  

 

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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值