zoukankan      html  css  js  c++  java
  • JAVA Socket简单实例

    服务端
    SocketManager.java
    SocketManagerSocketManagerSocketManagerSocketManager

    点击(此处)折叠或打开

    1. import java.io.DataInputStream;
    2. import java.io.DataOutputStream;
    3. import java.io.IOException;
    4. import java.net.ServerSocket;
    5. import java.net.Socket;

    6. public class SocketManager {
    7.     /**
    8.      * @param args
    9.      * @throws IOException 
    10.      */
    11.     public static void main(String[] args) {
    12.         SocketManager manager = new SocketManager();
    13.         manager.doListen();
    14.     }

    15.     public void doListen() {
    16.         ServerSocket server;
    17.         try {
    18.             server = new ServerSocket(9991);
    19.             while (true) {
    20.                 Socket client = server.accept();
    21.                 new Thread(new SSocket(client)).start();
    22.             }
    23.         } catch (IOException e) {
    24.             e.printStackTrace();
    25.         }

    26.     }

    27.     //服务器进程
    28.     class SSocket implements Runnable {

    29.         Socket client;

    30.         public SSocket(Socket client) {
    31.             this.client = client;
    32.         }

    33.         public void run() {
    34.             DataInputStream input;
    35.             DataOutputStream output;
    36.             try {
    37.                 input = new DataInputStream(client.getInputStream());
    38.                 output = new DataOutputStream(client.getOutputStream());
    39.                 //
    40.                 String listMsg = input.readUTF();
    41.                 output.writeUTF("Send : " + listMsg + " HelloVillage...");
    42.                 System.out.println("Recive: " + listMsg);
    43.                 listMsg = input.readUTF();
    44.                 output.writeUTF("Send Second: " + listMsg + " HelloVillage...");
    45.                 System.out.println("Recive Second: " + listMsg);
    46.             } catch (IOException e) {
    47.                 e.printStackTrace();
    48.             }
    49.         }
    50.     }
    51. }

    客服端
    SocketClient.java

    点击(此处)折叠或打开

    1. import java.io.DataInputStream;
    2. import java.io.DataOutputStream;
    3. import java.io.IOException;
    4. import java.io.OutputStream;
    5. import java.net.Socket;
    6. import java.net.UnknownHostException;

    7. public class SocketClient {

    8.     public static void main(String[] args) {
    9.         Socket socket = null;
    10.         try {
    11.             socket = new Socket("127.0.0.1", 9991);
    12.             //向服务器端第一次发送字符串 
    13.             OutputStream netOut = socket.getOutputStream();
    14.             DataOutputStream doc = new DataOutputStream(netOut);
    15.             DataInputStream in = new DataInputStream(socket.getInputStream());
    16.             //向服务器端第二次发送字符串 
    17.             doc.writeUTF("list");
    18.             String res = in.readUTF();
    19.             System.out.println(res);
    20.             doc.writeUTF("bye");
    21.             res = in.readUTF();
    22.             System.out.println(res);
    23.             doc.close();
    24.             in.close();
    25.         } catch (UnknownHostException e) {
    26.             e.printStackTrace();
    27.         } catch (IOException e) {
    28.             e.printStackTrace();
    29.         } {
    30.             if (socket != null) {
    31.                 try {
    32.                     socket.close();
    33.                 } catch (IOException e) {
    34.                 }
    35.             }
    36.         }
    37.     }
    38. }
  • 相关阅读:
    win7最新版下载与密钥 | Win7用户福音:微软集成更新的新版Windows 7镜像泄露
    迅捷PDF编辑器 v2.1.0.1 中文免费版
    解决移动网络无法访问胡萝卜周网站(www.carrotchou.blog)
    vue启动流程
    vue--综合组件间的通信
    网络请求
    vue环境搭建
    vue--路由嵌套
    vue路由高级用法
    vue-router实现组件间的跳转---参数传递
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317919.html
Copyright © 2011-2022 走看看