Connection problem

Hey i am quite new to network programming

so i picked some example code from my books but i get always errors :s

I have a small client and server class
but when i want to make a new clientsocket with a addess and port i get an connection refused error

i can create a serversocket but when i ask a socket of it (by usijng accept) i get a cannot except error

i am behind a router but the port i work on is open so i cant seem to find the solution
any help?

my code:
import java.net.;;
import java.io.
;

public class TestClient {
public TestClient() throws IOException {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;

	try {
		socket = new Socket(InetAddress.getLocalHost(), 4444);
		System.out.println("Connected with server "
				+ socket.getInetAddress() + ":" + socket.getPort());
		out = new PrintWriter(socket.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(socket
				.getInputStream()));
	} catch (UnknownHostException e) {
		System.err.println("Don't know about host.");
		System.exit(1);
	} catch (Exception e) {
		System.err
				.println("Couldn't get I/O for the connection to.");
		System.exit(1);
	}

	BufferedReader stdIn = new BufferedReader(new InputStreamReader(
			System.in));
	String fromServer;
	String fromUser;

	while ((fromServer = in.readLine()) != null) {
		System.out.println("Server: " + fromServer);
		if (fromServer.equals("Bye."))
			break;

		fromUser = stdIn.readLine();
		if (fromUser != null) {
			System.out.println("Client: " + fromUser);
			out.println(fromUser);
		}
	}

	out.close();
	in.close();
	stdIn.close();
	socket.close();
}

}

import java.net.Socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.InetAddress;

public class TestServer {
public TestServer() throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println(“Could not listen on port: 4444.”);
System.exit(1);
}

	Socket clientSocket = null;
	try {
		clientSocket = serverSocket.accept();
	} catch (IOException e) {
		System.err.println("Accept failed.");
		System.exit(1);
	}

	clientSocket.close();
	serverSocket.close();
}

}

Is this on one computer or two?

this line…


socket = new Socket(InetAddress.getLocalHost(), 4444);

Will make a connection on port 4444 to the machine that this code is running on. If your servre is running ona differnt machine, you wont get there this way.

If this is all on the same machine, how many internet connections does the machine have? if it has two or more then you may have to do more work to insure that the client and server are talkign on the same connection. The ebst thing to do in that case is to explicitly state “127.0.0.1” for both the IP for both server socket and the client socket.

Finally, turn off your local firewall. Under windows this will be in your security control panel. It gets in the way of a lot of networking. if this solves it, you can then go into the settings for the firewall and open the necessary prots so you can trun it back on.

these are two standalone classes/apps
i run then on 1 pc with only 1 connection
port 4444 is open in my router so it should work, dont see why
maybe windows firewall will try it later

If its on a single machine your router is irelllevent.

What about my other questions? How many NICs does this machine have?

i got it to make a connection with clientside
instead of the getlocalhost i used the standard gateway adress of my router and then it worked

anyone have an example of a lobby room?

Thats very odd.

I would say that (a) You have the port on the router mapped back to your box or it woudl never work that way and
(b)you have your networking configured badly at home.

regards

JK

dont know about that
will test same code in linux as well

when i use localhose (127.0.0.1) is can now open socket on a port
i can also open a serversocket on an open port
but when i then try to accept() from it i get accept failed :s

Thats weird, can you show me the whole exception trace?

for running testClient:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)

for running testServer

java.net.SocketException: select failed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)

anyone still has no dieas why this doenst work :frowning:
just cant seem to find it

try this:

Server:


ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();
System.out.println("Client connected successfully to server!");

Client:


Socket s = new Socket(InetAddress.getLocalHost(), 4444);

See if you get a connection to occur. If that doesn’t work give the examples in JGN a try. :wink:

-Matt Hicks

errors but i guess got something to do with my router although the port is opened
what is JGN? Should i use Jini?

JGN = JavaGameNetworking = http://javagamenetworking.dev.java.net

Wow, as much advertising as I’ve been doing for it you’d think I wrote it or something. :-p

-Matt Hicks

I can almost assure you your networking is confugred badly if you cannot even make a conenction explictly to 127.0.0.1 on the same machine.

Check to see if you have a firewall runnign on the amchine and if so disable it. See if that helps.

ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();

I can execute the first line so i can make a connection on that port
but trying to get a socket on it fails. I only have the xp firewall but disabling it doesnt make a difference

Just for kicks try downloading JGN and running the basic test. If that doesn’t work then you’ve got serious problems. If it does work then you’ve just got mental problems. :wink:

-Matt Hicks