套接字不写入服务器

时间:2014-09-21 12:18:52

标签: java sockets

对于同样的问题存在疑问,但他们并没有帮助我。

以下是我一直在处理ServerSocket未收到来自客户Socket的任何消息的代码。

服务器

package rescue_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TreeMap;

/**
 *
 * @author Mohammad Faisal
 */
public class RServer {

    private TreeMap<String, PrintWriter> clients;
    private static final int PORT = 6621;

    private class ClientHandler implements Runnable {

        private PrintWriter writer;
        private BufferedReader reader;

        public ClientHandler(Socket client) {
            try {
                writer = new PrintWriter(client.getOutputStream());
                reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }

        @Override
        public void run() {
            System.out.println("client handler thread started");
            String msg;
            try {
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public RServer() {
        clients = new TreeMap<>();
    }

    public void start() {
        try {
            ServerSocket server = new ServerSocket(RServer.PORT);
            while (true) {
                System.out.println("waiting....");
                Socket client = server.accept();
                Thread tclient = new Thread(new ClientHandler(client));
                tclient.setDaemon(true);
                tclient.start();
                System.out.println("got a connection.");
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new RServer().start();
    }
}

客户端

package rescue_client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Mohammad Faisal
 */
public class RClient {

    private final static int SERVER_PORT = 6621;
    private final static String SERVER_IP = "127.0.0.1";
    private Socket socket;
    private PrintWriter writer;
    private BufferedReader reader;

    private class ServerHandler implements Runnable {

        @Override
        public void run() {
            System.out.println("server handler started.");
            String msg;
            try {
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public RClient() {
        JFrame frame = new JFrame(SERVER_IP);
        frame.setVisible(true);
        frame.setBounds(10, 20, 100, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void startServerHandler() {
        Thread tserver = new Thread(new ServerHandler());
        tserver.setDaemon(true);
        tserver.start();
        sendWelcomeMsg();
    }

    private void sendWelcomeMsg() {
        try {
            writer.write(InetAddress.getLocalHost().getHostName());
            writer.flush();
        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        }
        writer.flush();
    }

    public void start() {
        System.out.println("starting client...");
        try {
            socket = new Socket(RClient.SERVER_IP, RClient.SERVER_PORT);
            writer = new PrintWriter(socket.getOutputStream());
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            startServerHandler();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new RClient().start();
    }
}

问题是我没有从server client收到消息。

1 个答案:

答案 0 :(得分:1)

来自JavaDoc

  

一条线被视为由换行(&#39; \ n&#39;),回车(&#39; \ r&#39;)或回车后的任何一个终止通过换行。

因此,您必须在正在发送的String中添加其中一个。