• socket编程---一个简单例子


    服务器端代码(单线程):

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TalkServer {
    
        public static void main(String[] args) {
            ServerSocket server = null;
            try {
                server = new ServerSocket(4700);
            } catch (IOException e) {
                System.out.println("Can not listen 4700.");
            }
            Socket socket = null;
            try {
                socket = server.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            String line;
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
                BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Client:" + reader.readLine());
                line = sysin.readLine();
                while (!line.equals("bye")) {
                    printWriter.print(line);
                    printWriter.flush();
                    System.out.println("Server:" + line);
                    System.out.println("Client:" + reader.readLine());
                    line = sysin.readLine();
                }
                reader.close();
                sysin.close();
                printWriter.close();
                socket.close();
                server.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }

    客户端代码:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class TalkClient {
        public static void main(String[] args) {
            try {
                Socket socket = new Socket("127.0.0.1", 4700);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
                String readline = sysin.readLine();
                while (!readline.equals("bye")) {
                    out.println(readline);
                    out.flush();
                    System.out.println("Client:" + readline);
                    System.out.println("Server:" + in.readLine());
                    readline = sysin.readLine();
                }
                in.close();
                out.close();
                sysin.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    服务器端代码(多线程):

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class MultiEchoServer {
        private static ServerSocket serverSocket;
        private static final int PORT = 12345;
    
        public static void main(String[] args) throws IOException {
            try {
                serverSocket = new ServerSocket(PORT);
            } catch (IOException e) {
                System.out.println("Unable connect to the port");
                System.exit(1);
            }
            do {
                Socket client = serverSocket.accept();
                System.out.println("New client accept...");
                ClientHandler clientHandler = new ClientHandler(client);
                clientHandler.start();
            } while (true);
        }
    }
    
    class ClientHandler extends Thread {
        private Socket client;
        private Scanner input;
        private PrintWriter output;
    
        public ClientHandler(Socket client) {
            this.client = client;
            try {
                input = new Scanner(client.getInputStream());
                output = new PrintWriter(client.getOutputStream(),true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            String receive = "";
            do {
                receive = input.nextLine();
                System.out.println("Server receive message:" + receive);
                output.println("Echo:" + receive);
            } while (!receive.equalsIgnoreCase("q"));
            try {
                if (client != null) {
                    System.out.println("Closing connection...");
                    client.close();
                }
            } catch (IOException e) {
                System.out.println("Unable to close connection...");
            }
        }
    }

    客户端代码:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class MultiEchoClient {
        private static InetAddress host;
        private static final int PORT = 12345;
    
        public static void main(String[] args) {
            try {
                host = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                System.out.println("Host id not found!");
                System.exit(1);
            }
            SenderMessages();
        }
    
        public static void SenderMessages() {
            Socket socket = null;
            try {
                socket = new Socket(host, PORT);
                Scanner input = new Scanner(socket.getInputStream());
                PrintWriter outpout = new PrintWriter(socket.getOutputStream(),true);
                Scanner userInput = new Scanner(System.in);
                String message, response;
                do {
                    System.out.println("Enter Message:");
                    message = userInput.nextLine();
                    outpout.println(message);
                    response = input.nextLine();
                    System.out.println("Server " + response);
                } while (!message.equalsIgnoreCase("q"));
                input.close();
                userInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null) {
                        System.out.println("Closing connetion...");
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    最终一致性解决实例
    分布式事务一致性方案
    分布式事务
    OSX
    JAVA
    Eclipse
    Activiti
    CentOS
    用Visual Studio 2015 编写 MASM 汇编程序(二)从头开发一个Win32汇编程序
    Oracle
  • 原文地址:https://www.cnblogs.com/liaojie970/p/5398102.html
Copyright © 2020-2023  润新知