生产就绪Java中的Socket服务器

时间:2015-06-29 07:31:48

标签: java sockets

有许多教程可以解释有关套接字服务器/客户端的内容,但所有这些内容都非常简单。是否有生产就绪代码的教程?我是套接字的新手。有一个客户端,它将字符串发送到服务器。我必须创建服务器端。在服务器端我从客户端读取字符串,并在一些操作后将它们保存在db中。我必须回复客户端,如果我得到字符串"错误"例如。如果在30秒内没有来自客户端的任何daya,我必须关闭客户端连接,但服务器端必须工作。 这是我的测试客户端:

public class ClientSideSocket2 {
    public static void main(String[] args) {
        String serverName = "localhost";
        int port = 5555;
        String line = "";
        Socket client = null;

        try {
            System.out.println("Connecting to " + serverName + " on port " + port);
            client = new Socket(serverName, port);
            System.out.println("Just connected to " + client.getRemoteSocketAddress());

            PrintWriter toServer = new PrintWriter(client.getOutputStream(), true);
            BufferedReader fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));

            List<String> messages = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                messages.add("Message " + i+1);
            }
            messages.add("abc");

            for (int i = 0; i < messages.size(); i++) {
                toServer.println(messages.get(i));
                if ((line = fromServer.readLine()) != null) {
                    System.out.println("Responce from server: " + line);
                }
            }

            toServer.close();
            fromServer.close();


        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

和我的服务器端:

public class TRSServerInterface implements Runnable {

    private ServerSocket serverSocket = null;
    private Socket socket = null;
    boolean runner = true;
    String message = "";

    public TRSServerInterface() {}

    @Override
    public void run() { // default run method of Thread class and Runnable interface

        try {
            int serverPort = 5555;
            ServerSocket serverSocket = new ServerSocket(serverPort);

            while(true) {
                System.out.println("Waiting for connection...");
                socket = serverSocket.accept();
                System.out.println("Connected to " + socket.getRemoteSocketAddress());
                //get the input and output streams

                PrintWriter toClient = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                do {
                    message = fromClient.readLine();
                    System.out.println("From client > " + message);
                    if (message.equals("abc")) {
                        toClient.println("Message from server");
                    }
                    else {
                        toClient.println("");
                    }
                } while (!message.equals(""));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
//          try {
//              objectOut.close();
//              objectIn.close();
//              socket.close();
//          } catch (IOException e) {
//              e.printStackTrace();
//          }
        }
    }
}

是我的解决方案,如果30秒内没有任何数据,我可以如何关闭与客户端的连接。

1 个答案:

答案 0 :(得分:0)

有几个生产就绪框架应该使用而不是自己滚动。套接字超时可用于控制在抛出异常之前允许不同操作的时间。