Socket networking works only on localhost - java

I have created two separate programs, a client and a server. I tried testing them both within eclipse and running as runnable jars on the same (Windows) machine (localhost). It worked exactly as it should.
However, when I sent clinet (and later server) to a friend of mine to test it out, it didn't work. We made sure that ports were open (even on clients side), but to no avail. It didn't work. I would just get a timeout ConnectException.
The sockets I used were 50178-50180.
I have no idea what to think of it. Any ideas what might be going wrong?
This is the socket code:
(serverside)
serverSocket = new ServerSocket(50178);
while (true)
{
clientSocket = serverSocket.accept();
streamOut = clientSocket.getOutputStream();
objectOut = new ObjectOutputStream(streamOut);
streamIn = clientSocket.getInputStream();
objectIn = new ObjectInputStream(streamIn);
(stuff)
}
(clientside)
Socket socket = new Socket(ipAddress, 50178);
OutputStream streamOut = socket.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(streamOut);
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
I didn't include rest of the code since it I/O stuff (which does indeed work because it works over localhost).
EDIT:
I added requested code. I also tried it over two computer that were on the same network (witch local ip 192.168.1.*). It worked.

You may already be doing this, but I believe that a call to serverSocket.accept() must be done. This will tell the server that it needs to wait for a connection.
If you are doing that, my best answer to you would be to perform network troubleshooting. The computers must be able to see each other on the local area network in order to be able to use just the ip address. Make sure that there is not a firewall or something like that that is preventing the connection.
If you could add the code of where the client tries to connect and where the server accepts the connection, that may be helpful.

Related

Socket only works when firewall turned off

I have a simple client-server script setup which allows me to send a message from my android device to my computer. The computer server script is something like this -
ServerSocket server = new ServerSocket(9000);
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert objectinputstream to string
String message = (String)ois.readObject();
System.out.println("Message received: " + message);
ois.close();
socket.close();
And the android code is something like this (Note that this code runs as a async task) -
socket = new Socket( "10.69.23.11",9000);
//write to socket using Objectouputstream
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(msg);
oos.close();
This code works and I tested it a few weeks ago. But starting from last week after I upgraded from java 1.7 to 1.8, this code no longer works. If I turn my firewall completely off this code starts working again. I explicitly added inbound and outbound rules to let port 9000 through as a tcp but it still doesn't work with the firewall on. Can anyone please help me?
It turned out firewall was somehow blocking my eclipse. I went into windows control panel and let microsoft diagnose me a solution. Never expected it but they correctly identified it and fixed the problem automatically by bypassing firewall for eclipse.

Java DatagramSocket freezes on initialization

I'm writing a piece of UDP networking program (client - server), and I've run into some trouble.
I want to use streams to I/O data, so I googled "udp inputstream" and found UDPInputStream and UDPOutputStream. When I try to use these, however, the program gets stuck when trying to initialize the UDPOutputStream.
This is the line in my code that freezes:
outStream = new UDPOutputStream(InetAddress.getByName("127.0.0.1"), port);
System.out.println("UDP output stream initialized."); // <-- doesn't get called
I checked out the source of the UDPOutputStream, the code gets stuck on this line:
dsock = new DatagramSocket();
Why does the execution hang up on this line? On the server side, I still use my "old", non-stream version of a simple UDP code, and it works. The socket is initialized the same way and it doesn't hang up. I tried to put a port number to the initialization, but it doesn't solve the problem.
Host machines have more than one network interface (for example, 127.0.0.1 for the loopback interface and some other address for the network card; there may be more than one network card).
If you bind to the loopback address 127.0.0.1 then you'll only be able to receive packets sent locally. If want to receive packets sent over the network from a remote machine you must bind to the local IP address (e.g. 192.168.1.100).
Try following:
InetAddress addr = InetAddress.InetAddress.getLocalHost();
outStream = new UDPOutputStream(addr, port);

Sockets in Java...?

I need to build an application which can receive data from over a network and use this data to do some unrelevant things with.
Here's a piece of code to make clear what I'm doing.
On the server side:
static Socket client = null;
static ServerSocket ss = null;
if (ss != null) {
ss.close();
}
ss = new ServerSocket(5513);
isrunning = true;
System.out.println("Waiting for client...");
client = ss.accept();
System.out.println("Client accepted.");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
And the client side:
Socket client = null;
PrintWriter out = null;
try {
client = new Socket("hostname", 5513);
out = new PrintWriter(client.getOutputStream(), true);
}
Please note that this is just a piece of the code. There are no errors in the code.
After running the server-sided piece of code, it correctly waits for the client to connect.
Now here comes the problem. As soon as I try to connect from the client side, I'm getting a "connection refused"-error.
HOWEVER, I found something on the internet whoch told me to try telnetting from the client side. For example, let the server-sided IP be 192.168.1.1. So, after using this command:
telnet 192.168.1.1 5513
I actually get a connection with the server. The command will launch an empty screen, and everything I manually type in the command line will be sent to the server-side after pressing enter (checked with debugging).
So, I can manually connect to the server-side and send some data, but my code refuses to connect.
Anyone who knows what I am doing wrong?
Is this the code you're actually using?
client = new Socket("hostname", 5513);
Try changing it to:
client = new Socket("192.168.1.1", 5513);
client = new Socket("hostname", 5513);
Hostname needs to represent the IP Address you're connecting to. If you're trying to connect to yourself, it would be "localhost"
Also, the server is not listening for the client AT ALL TIMES, there must be a while loop so the server listens and accepts connections.
while (true) {
client = ss.accept();
out = new PrintWriter(client.getOutputStream(), true);
//You should probably assign it to a seperate thread to handle stuff for this client
}
And I should explain on why you're getting that particular error. When something says that the connection is refused, it usually means that the IP Address you want to connect to knows your sending a connection and is blocking it because it was not listening for that connection. Basically, when the server closed, you stopped listening for the client, so anything that came in on that port would be blocked. Of course, the other case could be that Java was blocked on your firewall and an exception should be made for it. Although this is rarely the case if what you're trying to accomplish is over a LAN.
You're not actually using "hostname" in your Socket object in the client are you?
It should the 192.168.1.1.
Are you on Windows? and If so have you added java.exe and javaw.exe to Firewall with inbound and outbound enabled? and have you added a rule for 5513 to your Firewall?
If yes Windows but no Firewall settings, that's your answer, open up your Firewall.

How long does it take for the OS to close the socket after calling close() method in java?

I'm doing some network programming in Java. in a loop I open a socket , send some data and close it. but I get the exception of "connection already in use" . I guess that happens since I use a same port at each repetition of the loop. how long does it take for the OS (Ubuntu 11.10) to close the socket and free the port? thanks
the simplified code look like this
while(true){
Socket clientSocket = new Socket("localhost", 5000);
PrintWriter outToServer = new PrintWriter( clientSocket.getOutputStream(),true);
outToServer.println ("Hi") ;
clientSocket.close();
}
It can take quite a while (e.g. up to 3 minutes if there is unsent data)
In your case you are using a different local port (same remote port doesn't matter) each time so it shouldn't matter.
BTW: Creating a new connection is pretty expensive, I would try to re-use a connection rather than opening a new one each time. In the example above it could be as much as 1000x faster. ;)

Why it cannot find getInputStream?

I have this code:
ServerSocket serverSideSocket = new ServerSocket(1234);
serverSideSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));
And compiler writes me that it cannot find "getInputStream". I do not understand why. In the beginning of my code I do import java.net.*.
Calling of accept returns instance of Socket which has required method getInputStream.
The code might look like this:
ServerSocket serverSideSocket = new ServerSocket(1234);
Socket socket = serverSideSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Great tutorial how to work with sockets in java: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
This because conceptually a ServerSocket doesn't provide a direct connection object that can be used to send and receive data. A ServerSocket is a tool that you can use with the .accept() method to let it listen on the choosen port and generate a new real connection when a client tries to connect.
That's why you can't get an InputStream from a ServerSocket. Since many clients can connect to the same server, every client will make the server socket generate a new Socket (that is an opened TCP connection) that is returned from .accept() through which you can send and receive using its InputStream and OutputStream.

Categories