Socket简单通信

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket简单通信相关的知识,希望对你有一定的参考价值。

服务端代码:

public class MyServer {
    private ServerSocket serverSocket;

    public MyServer() {
        try {
            // 设置你服务器监听的端口为10000,用户能使用的端口为1025-65535
            serverSocket = new ServerSocket(10001);
        } catch (IOException e) {
            // 捕获异常,不懂的话好好看java,
            e.printStackTrace();
        }
        // 创建新的监听主线程,这个线程创建ServerSocket监听
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    Socket socket = null;
                    try {
                        socket = serverSocket.accept();
                        // 当监听到了客户端连接后,创建新线程传输数据,这样可以实现多个客户端同时访问
                        new Thread(new SocketHandler(socket)).start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    class SocketHandler implements Runnable {
        private Socket socket;
        private BufferedReader reader;
        private PrintWriter writer;

        SocketHandler(Socket socket) {
            try {
                this.socket = socket;
                reader = new BufferedReader(new InputStreamReader(
                        this.socket.getInputStream(), "GB2312"));
                writer = new PrintWriter(socket.getOutputStream(), true);
                writer.println("-------welcome---------");
                writer.println("-------welcome---------");
                writer.println("-------welcome---------");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        // 这里是覆盖实现接口Runnable里的run()
        public void run() {
            try {
                // 读取数据,这里只能读取一行String
                String line = reader.readLine();
                System.out.println(line);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 最后要关闭Socket
                try {
                    if (socket != null)
                        socket.close();
                    if (reader != null)
                        reader.close();
                    if (writer != null)
                        writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        new MyServer();
    }

}

客户端代码:

public class SocketClientDemo implements Runnable {
    private Socket socket;
    BufferedReader reader;
    private PrintWriter writer;

    public SocketClientDemo() {
        try {
            // 127.0.0.1表示本机IP,10000为服务器Socket设置的端口
            socket = new Socket("127.0.0.1", 10001);
            reader = new BufferedReader(new InputStreamReader(
                    socket.getInputStream(), "GB2312"));
            writer = new PrintWriter(socket.getOutputStream(), true);
            writer.println("working.............");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            // 这里就可以读取所有行String
            String line, buffer = "";
            while (!((line = reader.readLine()) == null))
                buffer += line;
            System.out.println(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("problem");
        } finally {
            // 最后关闭Socket
            try {
                if (socket != null)
                    socket.close();
                if (reader != null)
                    reader.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        new Thread(new SocketClientDemo()).start();

    }

}

 

以上是关于Socket简单通信的主要内容,如果未能解决你的问题,请参考以下文章

简单的通信----使用Socket实现TCP协议

socket通信流程和一个简单的网络通信程序

Socket通信的简单例子

socket通信

Socket简单通信

Java中Socket 实现最简单的客户端与服务端通信