Question

hello guys I've created a simple chat room app but I can't connect to server to send and receive data, here is part of my code:

server side:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.regex.Pattern;

public class Listener {

public static void start() {
    try {
        final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(true);
        serverSocketChannel.socket().bind(new InetSocketAddress(GetConfigs.getServerListenerPort()));
        final List<Operator> operators = new ArrayList<>();
        final int nOperators = 64;
        for (int i = 0; i < nOperators; i++) {
            operators.add(i, new Operator());
        }
        final int parallelism = Runtime.getRuntime().availableProcessors();
        ForkJoinPool pool = new ForkJoinPool(parallelism);
        pool.submit(() -> new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    operators.get(i).newOperation(serverSocketChannel.accept());
                    i++;
                    if (i == nOperators) {
                        i = 0;
                    }
                } catch (IOException ignored) {
                }
            }
        })).fork().invoke().start();
    } catch (IOException e) {
        Logging.log.error(e);
    }
}

client side :

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.regex.Pattern;

public class ConnectServer {

private static final String SERVER_ADDRESS = GetConfigs.getServerIPAddress();
private static final int SERVER_LISTENER_PORT = GetConfigs.getServerListenerPort();

private StringBuilder result = new StringBuilder();

private void connect() {
    result.delete(0, result.length());
    try {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(true);
        if (socketChannel.connect(new InetSocketAddress(InetAddress.getByName(SERVER_ADDRESS), SERVER_LISTENER_PORT))) {
            DataOutputStream dos = new DataOutputStream(socketChannel.socket().getOutputStream());
            DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());
            dos.writeUTF("...");
            result.append(String.valueOf(dis.readUTF()));
            dos.close();
            dis.close();
            socketChannel.close();
        }
    } catch (IOException ex) {
        //...
    }
}

}

It does work on a LAN connection.

please help me...

Was it helpful?

Solution

Here is a simple test. Ssh to the computer outside of your LAN, a computer where you cannot connect from. Try two things:

ping <ip address you connect to>

and

telnet <ip address you connect to> <port you connect to>

If ping fails, chances are you're using your "inside" IP address, something like 192.168.x.y (or other such) . Try using http://www.whatismyip.com/ from your server machine.

If ping succeeds and telnet fails, you may have a firewall problem: please set up port forwarding on your router, poke a hole in your firewall and do whatever else is needed. What exactly is needed depends on your exact setup.

OTHER TIPS

My guess: I had the same problem today, my client-server worked on my wifi, but not with clients from the outside. I had the port forwarded on my router, but the problem was that it didn't forward to the computer I ran the server program on. It worked after I changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top