Socket only works when firewall turned off - java

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.

Related

Socket networking works only on localhost

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.

Android Java Socket - Connection timed out

I'm trying to create one simple app. I want to my cell phone be a server socket and I'm trying to send messages from my pc, my pc is the client is this case.
When they are in the same network it works fine but when I connect my cell phone in a 3G network I receive the error "Connection timed out" in my PC.
I'm using a host from no-ip (in both situation). When I do 'telnet mycellphonehost.org 8080' for example I have no problem, it is able to connect. I think the no-ip host is working fine because is give me the correct external IP.
I also use one app called FIREBIND for test if the port is open or not. The result is: "Firebind was successfully able to both transmit and receive data over this port using the TCP protocol."
I already read a lot questions about this subject, similar problems... but nothing help me solve this issue. I hope someone can help me. Thanks in advance!
Follow the codes:
Android Server
try{
ServerSocket server = new ServerSocket(port);
Socket s = server.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(input.readLine());
input.close();
s.close();
server.close();
}
catch(IOException e){
System.out.println("ERROR: " + e.getMessage());
}
PC Client
try{
Socket s = new Socket("myhostfromno-ip.org",port);
PrintStream output = new PrintStream(s.getOutputStream());
output.println("TEST MESSAGE");
output.flush();
s.close();
}
catch(IOException e){
System.out.println("ERROR: " + e.getMessage());
}
PS: They have to be in different network
I would recommend using GCM for the cell
Notification and afterwards making a server pull. It certainly is much faster and you could use a simple REST server for serving data.
You need to make your PC the server and your droid the client.

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.

Android check if remote server is online

I am new to Android programming and I have been facing problems that didn't exist in native Java. When I run this code in my computer, it runs correctly. But, when I run it in my device.I get nothing, I even tried to post the message to UI and there's no logcat for this. I am not sure what is wrong.
try{
Socket socket = new Socket(serverAddr, SERVER_PORT);
Log.i("TAG","Socket connected");
}catch(IOException e){
Log.i("TAG","Socket not connected");
}
Update 1: I just changed the code..nothing much and realized that after 2 minutes or so it does what it was supposed to do?? Is is anything to do with keep alive flags? Or is there anyway that I can run the code just for a second or two and stop it. Please understand that the code below the socket creation line executes only after 2 minutes if the server is dead. Here below is my code:
try{
InetAddress serverAddr = InetAddress.getByName(serverIP);
//Line below executes with no delay
postToUI("Trying to connect to standalone server" + "\n\n");
socket = new Socket(serverAddr, SERVER_PORT);
//Line below executes after 2 minutes
postToUI("Successfully connected to standalone server" + "\n\n");
}catch(ConnectException e){
postToUI("Socket not connected");
}catch(IOException e){
postToUI("Socket not connected");
}
I have done very few Andrioid development so don't bite me smiley.
Possible reasons why the host might not communicate with the client
Host has firewall which might be closing the connection (unlikely)
Host might have unexpectly shutdown e.g power failure (unlikely)
Host might not properly port forwared (possibly)
Your andriod app doesn't have the proper manifest that allows the use of sockets.
Your andrioid device you are testing on might have its internet disabled which can enabled easily.
Host's address could have changed if it is dynamic.
For logcat, I think that logcat only displays log messages for the main thread. But I am unsure.

Android Socket getting exception while sending data

I have tested the code below. It was working in emulator but not working in Android Mobile. Do I need to do any settings? Please help me.
Thank you.
try {
Socket socket = new Socket("192.168.0.54", 9083);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Testing");
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
String readObject = reader.readLine();
System.out.println(readObject);
}
catch (Exception e) {
e.printStackTrace();
}
"Connection reset by peer" means that someone between your phone and the server (inclusive) closed the connection while you were reading it.
First check if the server receives and sends anything. If not, then someone between your phone and the server is blocking the transfer. If you are on corporate WiFi, there may be firewalls prrotecting the server etc. If on 3G there definitely is one.
You should add the stack trace to your post. Without it our answers are just guesswork...
Edit: The IP address 192.168.x.x points to an internal network. Are you sure you can access the internal network from WiFi/3G?
'Connection reset by peer' is usually caused by writing to a connection that has already been closed by the other end. In other words, an application protocol error. It doesn't show up on that write, but on a subsequent I/O operation.

Categories