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.
Related
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.
I'm using java and sockets to comunicate a client/server application.
I want to send a message to server like this:
is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
pw = new PrintWriter(socket.getOutputStream());
pw.println("MESSAGE");
pw.flush();
And that worked well, but then after that I'm trying to send another message
pw.println("SECOND MESSAGE");
pw.flush();
And the second message is not sending! What can I do?
Ensure that your server side is consistently reading for more input. If you only have it say for example performing:
bufferedReader.readLine();
only once then this is the reason you think it isn't receiving it. It probably is if the connection isn't closed.
Another possibility since you said it isn't "sending" the second message, is ensure the socket connection remains open and that the reader is still open on the server side and that it wasn't closed after the first message was received.
use flush() when all the messages are sent.
I'm writing a Java client/server application. It should allow clients to send text data to the server. This kind of communication should be repeatable many times using the same connection.
I write it like this:
// On a server:
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
socket.setKeepAlive(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if (reader.ready()) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
// do something with line
}
}
// On a client:
Socket socket = new Socket(host, port);
socket.setKeepAlive(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write("Some data from client to server");
writer.flush();
The problem is: I can't read on a server before I close OutputStream on a client. Or I can't open OutputStream on a client again, if it was already closed. How can I do continuous sending and reading of data?
You need two threads at both ends, one for reading data and other one for writing data.
The problem is: I can't read on a server before I close OutputStream on a client.
Yes you can. You just can't get to the case where readLine() returns null. It isn't the same thing.
Or I can't open OutputStream on a client again, if it was already closed.
Of course not. You have to create a new Socket.
How can I do continuous sending and receiving of data?
I don't understand the question. The code you posted doesn't attempt that.
If your goal is to send many mesages over the same socket connection, these messages will have to be delimited by an application-level protocol. In other words, you won't be able to rely on any system calls like reader.ready() or reader.readLine() == null to detect the end of the message on te server.
One way to achieve this is to begin each message with its length in characters. The server will then read exactly that number of charecters, and then stop and wait for a new message. Another is to define a special character sequence which concludes each message. The server will react to reading that particular sequence by ending the reading of the current message and returning to the "wait for new message" state. You must ensure that this sequence never appears in the message itself.
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.
I am trying to send xml to an IP. I am doing that with following code:
String sMessage = "<SERVER><CONNECT><IP>192.168.10.14</IP><CLIENT_ID>123</CLIENT_ID></CONNECT></SERVER>";
Socket socket = new Socket("192.168.252.148", 34543);
System.out.println("socket connected---: "+socket.isConnected());
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
InputStream in = socket.getInputStream();
out.println(sMessage);
byte[] buffer = new byte[in.available()];
in.read(buffer);
String rMsg = new String(buffer);
System.out.println("rMsg: "+rMsg);
out.close();
in.close();
socket.close();
rMsg is always empty. socket connection is true. Why am i not getting response back. I tried to change InputStream to BufferedInputStream but it did not help. Any idea to solve this problem by either fixing this code or by having new idea? Thanks in advance.
I'm afraid I don't really understand what you are trying to do. Your sending an XML file to an address, fair enough, but why are you automatically assuming the destination knows how to understand and create an XML formatted reply? What is the server-side implementation?
If there is such an implementation and you are not receiving data, then there must be a problem on that end, could you post code from it?
What should the server send you back? The thing is that you send an XML to the server and want to receive input at the same time from the server. But the response from the server may take some time. But I guess at this point your inputstream is closed or you are not listening to that any more. One simple solution to check that will be to put everythin in a while loop so you will see if your server answers (a bit later)... you also can listen to the NIC of your server with wireshark. Perhaps your server doesn't send anything?