socket通讯 客户端和服务端

Server端:

       package     test.bwl;                  import     java.io.DataInputStream;          import     java.io.DataOutputStream;          import     java.io.IOException;          import     java.net.ServerSocket;          import     java.net.Socket;                 public         class     SocketManager {               /**               * @param args                * @throws IOException                 */                   public         static         void     main(String[] args) {                SocketManager manager =     new     SocketManager();                manager.doListen();            }                       public         void     doListen() {                ServerSocket server;                    try     {                    server =     new     ServerSocket(   9991   );                        while     (    true    ) {                        Socket client = server.accept();                            new     Thread(    new     SSocket(client)).start();                    }                }     catch     (IOException e) {                    e.printStackTrace();                }                   }                      //服务器[fu wu qi]进程[jin cheng]                   class     SSocket     implements     Runnable {                       Socket client;                           public     SSocket(Socket client) {                        this    .client = client;                }                           public         void     run() {                    DataInputStream input;                    DataOutputStream output;                        try     {                        input =     new     DataInputStream(client.getInputStream());                        output =     new     DataOutputStream(client.getOutputStream());                           //                           String listMsg = input.readUTF();                        output.writeUTF(   "Recive:  "    + listMsg +    "    /r/n Thx..."   );                        System.out.println(   "Recive:   "    + listMsg);                        listMsg = input.readUTF();                        output.writeUTF(   "Recive Second:  "    + listMsg +    "    /r/n Thx..."   );                        System.out.println(   "Recive Second:   "    + listMsg);                    }     catch     (IOException e) {                        e.printStackTrace();                    }                }            }               }      
 

Client端:

 

       package     test.bwl;                  import     java.io.DataInputStream;          import     java.io.DataOutputStream;          import     java.io.IOException;          import     java.io.OutputStream;          import     java.net.Socket;          import     java.net.UnknownHostException;                 public         class     SocketClient {                       public         static         void     main(String[] args) {                Socket socket =     null    ;                    try     {                    socket =     new     Socket(   "127.0.0.1"   ,    9991   );                       //向服务器[fu wu qi]端第一次发送字符[zi fu]串[zi fu chuan]                          OutputStream netOut = socket.getOutputStream();                    DataOutputStream doc =     new     DataOutputStream(netOut);                    DataInputStream in =     new     DataInputStream(socket.getInputStream());                       //向服务器[fu wu qi]端第二次发送字符[zi fu]串[zi fu chuan]                          doc.writeUTF(   "list"   );                    String res = in.readUTF();                    System.out.println(res);                    doc.writeUTF(   "bye"   );                    res = in.readUTF();                    System.out.println(res);                    doc.close();                    in.close();                }     catch     (UnknownHostException e) {                    e.printStackTrace();                }     catch     (IOException e) {                    e.printStackTrace();                }     finally     {                        if     (socket !=     null    ) {                            try     {                            socket.close();                        }     catch     (IOException e) {                        }                    }                }            }        }     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值