1

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...

AvB
  • 171
  • 1
  • 2
  • 12
  • possible duplicate of [java.net.ConnectException](http://stackoverflow.com/questions/13719666/java-net-connectexception) -- even though linked question refers to a JDBC connection, the problem is the same and the diagnostic steps in the answer apply – kdgregory Apr 02 '14 at 19:25
  • `serverSocket = new ServerSocket(GetConfigs.getServerListenerPort());` is the port open on the server? – j.con Apr 02 '14 at 19:26
  • 1
    I thought you just asked this question and it was determined that a network configuration was nixing the connection attempt. Did you think that asking it again would fix the network settings? http://stackoverflow.com/questions/22820601/how-to-send-data-over-internet-using-socket – Mark W Apr 02 '14 at 19:26
  • @j.con I said it does work in LAN, so I think the problem is something else... – AvB Apr 02 '14 at 19:30
  • 1
    You have the answer, something on the network where this doesnt work is blocking the connection attempt. Configure that network to allow traffic of the correct protocol, on the correct port and its fixed. But you should seriously consider the comment I made on the first post you made for this question. ARM WILL close the client socket immediately after sending that first bit of data. That means that you wont be able to send any more data after that without reestablishing the connection. In addition, the client wont be able to read or send anything to the server. – Mark W Apr 02 '14 at 19:33
  • 1
    Or a router, or a domain controller, or a firewall.... thats part of the reason why we cant give you an exact answer to what specifically on the network is causing the problem. What we know is that it works on a LAN (presumably with simple network settings), but it doesnt work in your context. – Mark W Apr 02 '14 at 19:38
  • I dont think you understand AvB. The code you have works, at least with respect to establishing connections. The problem isnt this code (well at least not establishing a connection), the problem is the network settings where it doesn't work. We don't know anything about your network. So we can't tell you whats wrong there, only that its the network, not the code. – Mark W Apr 02 '14 at 19:47

2 Answers2

2

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.

0

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.

Jakkra
  • 641
  • 8
  • 25