D
D
Dmitry Belousov2016-04-30 20:11:20
Java
Dmitry Belousov, 2016-04-30 20:11:20

What's wrong with the server?

I wrote a chat where there is a client and a server. And such a thing, when I start the server on my computer, and then the client and my friend run the same client on his computer, I can connect to the server, but my friend cannot. What's the matter? Here is the server code

public class Server {

    private JFrame frame = new JFrame();
    private JTextArea dialogs = new JTextArea();
    private static Socket socket;
    private static ServerSocket serverSocket;
    private static ArrayList<PrintWriter> streams = new ArrayList();

    public static void main(String[] args) throws IOException {
        Server server = new Server();
    }

    public Server(){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Server");
        frame.setSize(250, 350);

        dialogs.setEditable(false);
        dialogs.setLineWrap(true);
        dialogs.setWrapStyleWord(true);
        dialogs.setBounds(15, 15, 216, 240);
        DefaultCaret caret = (DefaultCaret)dialogs.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        JScrollPane scrollPane = new JScrollPane(dialogs);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(15, 15, 216, 240);
        frame.add(scrollPane);


        frame.setVisible(true);

        try {
            serverSocket = new ServerSocket(1234);
            dialogs.append("Wait client...\n");
            while (true) {
                Server.socket = Server.serverSocket.accept();
                dialogs.append("Client connected\n");

                PrintWriter writer = new PrintWriter(socket.getOutputStream());
                streams.add(writer);

                Thread t = new Thread(new checkMessageFromClient(socket));
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class checkMessageFromClient implements Runnable{

        BufferedReader reader;

        checkMessageFromClient(Socket socket) {
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            String newMessage;
            try {
                while ((newMessage = reader.readLine()) != null) {
                    java.util.Iterator<PrintWriter> it = streams.iterator();
                    while (it.hasNext()) {
                        PrintWriter writer = it.next();
                        writer.println(newMessage);
                        writer.flush();
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                dialogs.append("Client disconnected");
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kuznetsov, 2016-04-30
@dreven

The point is most likely that the "server" uses ports that are closed.

K
Konstantin Malyarov, 2016-04-30
@Konstantin18ko

Have you forwarded ports through the router?
You are looking from local, he is from global.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question