I'm doing an app which needs to communicate with TCP sockets. I set up a Service which is my TCP Server and an activity which is my TCP Client.
I have a big delay from sending a message and receiving an answer from the server, like 10 or more seconds. After some days of researches, I found how to set timeout on the client request and all start to work fine.
So my question is, is it mandatory to set up timeout for a TCP connection, otherwise it doesn't work or something else is wrong with my implementation?
Here's my Client code:
public static void sendTCP(final InetAddress senderAddr, final String Msg, final int serverPort) {
Thread tclient = new Thread(){
public void run() {
boolean connected;
Socket socket = new Socket();
try {
Log.d("TCP", "Client: Connecting...");
socket.bind(null);
socket.connect((new InetSocketAddress(senderAddr, serverPort)), 1000);
connected = true;
try {
Log.d("TCP", "Client: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(Msg);
out.close();
}
catch (Exception e) {
Log.e("TCP", "Client: Error sending.", e);
}
} catch (Exception e) {
Log.e("TCP", "Client: Error connecting.", e);
connected = false;
}
finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
Log.d("TCP", "Client: Connection Closed.");
} catch (IOException e) {
Log.e("TCP", "Client: Error closing connection.", e);
}
}
}
}
}
};
tclient.start();
}
And Server's:
public void onStart(Intent intent, int startid) {
t = new Thread(){
public void run() {
try {
Boolean end = false;
Log.d("TCP", "Server: Creating server.");
ServerSocket ss = new ServerSocket(TCPPORT);
while(!end) {
//Server is waiting for client here, if needed
Log.d("TCP", "Server: Waiting on packet!");
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st = input.readLine();
Log.d("TCP", "Server: Message received from client: "+st);
InetAddress senderAddr = s.getInetAddress();
senderAddrString= senderAddr.getHostAddress();
myAddrString = GetLocalIpAddress();
myAddr = InetAddress.getByName(myAddrString);
if (senderAddr.equals(myAddr)) {
}
else {
//Estraggo dal pacchetto ricevuto
try {
StringTokenizer tokens = new StringTokenizer(st, "|");
flag = tokens.nextToken();
userid = tokens.nextToken();
payload = tokens.nextToken();
}
catch (Exception e) {
Log.e("TCP", "Server: Errore estrazione dati.");
}
if (flag.equals(Constants.flag_scan_answer)) {
Log.d("TCP", "Server: Flag answer");
//devo passare i dati ad un database ScanActivity
//database(senderAddrString,userid);
System.out.println("RISPOSTA RICEVUTA DAL SERVICE TCP");
System.out.println("FLAG " + flag);
System.out.println("USERID " + userid);
System.out.println("PAYLOAD " + payload);
announceReceivingPacket();
}
else {
Log.d("TCP", "Server: CASO NON PREVISTO");
}
}
s.close();
}
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
}
it's mandatory to set up timeout for a TCP connection
It isn't mandatory but it's a very good idea. Start by setting it to double or triple the expected service time and adjust so you don't get false positives. The default read timeout is infinity, and I have seen entire platforms fail in a way that wasn't detectable by the reader in any other way than a read timeout.
See here for relevant quotations.
The connect call
socket.connect((new InetSocketAddress(senderAddr, serverPort)), 1000);
can be BeginConnect with a callback that automatically reports failures and timeouts as well as successful connections. At this point you can use BeginSend and BeginReceive on the socket.
Related
In a simple Server Client communication I always get the "java.net.SocketException: Connection reset" on the serverside, if the client does not close the outputstream of it's socket directly after sending out the data via BufferedWriter.
If I close the client's outputstream everything works fine.
But obviously the client wants eventually to send more than one String (then the server needs to handle the connection in a new Thread). But I don't even get that far, because ofe the problem above...
Thanks for helping!!
Here is some condensed code to show the problem.
This is the server class:
public class TestServerCharacterStream {
public static void main(String[] args) {
System.out.println("### Started");
TestServerCharacterStream testServerCharacterStream = new TestServerCharacterStream(9498);
testServerCharacterStream.waitForData();
System.out.println("### Terminated");
}
private int port;
private ServerSocket serverSocket;
private BufferedReader in;
public TestServerCharacterStream(int port) {
this.port = port;
try {
this.serverSocket = new ServerSocket(port);
System.out.println("[SERVER] : Server started!");
} catch (IOException e) {
System.err.println("Cannot open new server socket!");
e.printStackTrace();
}
}
public void waitForData() {
Socket clientSocket = null;
try {
System.out.println("[SERVER] : Wait for data on port " + port + " ...");
clientSocket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String incoming = in.readLine();
System.out.println("[SERVER] : Incoming message: " + incoming);
} catch (IOException e) {
System.err.println("Error while accepting connection or reading input!");
closeStreamAndSocket(clientSocket, in);
closeServerSocket();
e.printStackTrace();
}
}
private void closeStreamAndSocket(Socket socket, BufferedReader in) {
try {
if (in != null) {
in.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
System.err.println("Cannot close stream or socket.");
e.printStackTrace();
}
}
private void closeServerSocket() {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
System.err.println("Cannot close serverSocket.");
e.printStackTrace();
}
}
}
This is the Client Class:
As mentioned, it works if closing the outputstream (see the comment). To use flush() or not does not make any difference. The only way to solve it is out.close();
But I want to use the BufferedWriter again, withot connect always again to the server.
public class TestClientCharacterStream {
public static void main(String[] args) {
System.out.println("### Started");
TestClientCharacterStream testClientCharacterStream = new TestClientCharacterStream("localhost", 9498);
testClientCharacterStream.sendData("Hello!!!");
System.out.println("### Terminated");
}
private InetSocketAddress adress;
private Socket clientSocket;
private BufferedWriter out;
public TestClientCharacterStream(String serverIp, int port) {
this.adress = new InetSocketAddress(serverIp, port);
try {
clientSocket = new Socket();
clientSocket.connect(adress, 10000);
this.out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
} catch (IOException e) {
System.err.println("Something went wrong on instantiating a new TestClientCharacterStream");
e.printStackTrace();
}
}
public void sendData(String string) {
try {
out.write(string);
System.out.println("[CLIENT] : Sent new message: " + string);
out.flush();
out.close(); // If I don't close the stream, I'm going to get a "java.net.SocketException: Connection reset" on the server
} catch (IOException e) {
closeStreamAndSocket(clientSocket, out);
e.printStackTrace();
}
}
private void closeStreamAndSocket(Socket socket, BufferedWriter out) {
try {
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
System.err.println("Cannot close stream or socket.");
e.printStackTrace();
}
}
}
The servers Output:
### Started
[SERVER] : Server started!
[SERVER] : Wait for data on port 9498 ...
Error while accepting connection or reading input!
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:270)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:313)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:177)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:162)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
at TestServerCharacterStream.waitForData(TestServerCharacterStream.java:40)
at TestServerCharacterStream.main(TestServerCharacterStream.java:12)
### Terminated
public class NewClass {
ServerSocket myServerSocket;
boolean ServerOn = true;
public NewClass() {
try {
myServerSocket = new ServerSocket(8888);
} catch (IOException ioe) {
System.out.println("Could not create server socket on port 8888. Quitting.");
System.exit(-1);
}
while (ServerOn) {
try {
Socket clientSocket = myServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket);
cliThread.start();
} catch (IOException ioe) {
System.out.println("Exception found on accept. Ignoring. Stack Trace :");
ioe.printStackTrace();
}
}
try {
myServerSocket.close();
System.out.println("Server Stopped");
} catch (Exception ioe) {
System.out.println("Error Found stopping server socket");
System.exit(-1);
}
}
public static void main(String[] args) {
new NewClass();
}
class ClientServiceThread extends Thread {
Socket myClientSocket;
boolean m_bRunThread = true;
public ClientServiceThread() {
super();
}
ClientServiceThread(Socket s) {
myClientSocket = s;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
System.out.println(
"Accepted Client Address - " + myClientSocket.getInetAddress().getHostName());
try {
in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(myClientSocket.getOutputStream()));
while (m_bRunThread) {
String clientCommand = in.readLine();
if (clientCommand != null) {
System.out.println("Client Says :" + clientCommand);
}
if (!ServerOn) {
System.out.print("Server has already stopped");
out.println("Server has already stopped");
out.flush();
m_bRunThread = false;
}
if (clientCommand.equalsIgnoreCase("quit")) {
m_bRunThread = false;
System.out.print("Stopping client thread for client : ");
} else if (clientCommand.equalsIgnoreCase("end")) {
m_bRunThread = false;
System.out.print("Stopping client thread for client : ");
ServerOn = false;
} else {
out.println("Server Says : " + clientCommand);
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
myClientSocket.close();
System.out.println("...Stopped");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
Client Code
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getLocalHost(), 8888);
PrintWriter out =new PrintWriter(s.getOutputStream(), true);
out.print("KKKKKKKKKKKKK \r\n");
out.flush();
out.close();
}
The purpose of the above code is to create server socket and client socket to pass data between server and client.When the client sends the data to server .server grab the message and print it on the screen but with following exception.The pop up from the String clientCommand = in.readLine(); line which appeared on server code.
java.net.SocketException: Connection reset
Your code is invalid. Your server code relies on the client implementing the protocol correctly, which this client doesn't. Bad habit. Defensive coding is required. If clientCommand == null you must exit this read loop and close the socket. Your present code will attempt to write to the closed connection, which produces exactly this exception ... later.
I have a server that accept more then one Client. Every Client is stored inside an ArrayList of Sockets. If a Client, for some reason, disconnects from my server, this should understand which client has been disconnected, close the client and delete it from the List.
Reading other question here I have understood that the only way to understand which client is disconnected is this: try to send data through all socket connected and the first one which throws Exception has to be closed.
The problem is that if the client is a simple Java-Application it works perfectly. But when the Client is an Android-Application, the data is sent through the (supposed) disconnected Socket. In this way my Server's algorithm doesn't throw Exceptions and it keeps sending data to all Sockets Causing a disaster. The code of clients (java and android) is Exactly the same but the results are different:
Server Code:
List<Socket> sList = new ArrayList<>();
Socket s;
int i = 0;
int whichSocket;
try
{
ServerSocket ss = new ServerSocket(7000);
while (true)
{
System.out.println("Server is Listening");
s = ss.accept();
sList.add(s);
System.out.println("Server Accepted Client --- " +s.toString());
Thread t2 = new Thread(new Runnable() {
public void run() {
try
{
DataInputStream dis = new DataInputStream(s.getInputStream());
while (true)
{
// For every message received from one client, it iterate through list sending that dat to ALL CLIENTS in the list of Socket
String test = dis.readUTF();
System.out.println("Message Sent By -- " + s.toString());
System.out.println(test);
while(i < sList.size()){
DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
dos.writeUTF(test);
System.out.println("Messaggio Sent To -- " + sList.get(i).toString());
dos.flush();
++i;
}
i=0;
}
} catch (IOException e)
{
System.out.println("First Exception");
e.printStackTrace();
} finally
{
try
{
// An exception has been thrown. This means that one client is disconnected.Which One? Let's send data to all Clients in the list.
whichSocket = -1;
for(Socket temp : sList)
{
System.out.println("PENEEEE inviato a -- " + temp.toString());
whichSocket ++;
DataOutputStream dosser = new DataOutputStream(temp.getOutputStream());
dosser.write(1);
System.out.println("Message Sent To -- " + temp.toString());
dosser.flush();
}
}
catch (IOException e)
{
System.out.println("Second Exception");
e.printStackTrace();
}finally
{
try
{
sList.get(whichSocket).close();
System.out.println("Socket Closed --- " + sList.get(whichSocket).toString());
sList.remove(whichSocket);
} catch (IOException e) {
System.out.println("Third Exception");
e.printStackTrace();
}
}
}
}
});
t2.start();
}
} catch (IOException e) {
System.out.println("General Exception");
e.printStackTrace();
}
Client Code:
while(flag) {
if(!isConnected) {
try {
s = new Socket("192.168.1.69", 7000);
isConnected = true;
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (flag) {
String result = dis.readUTF();
Log.d("InputStreammmm", result);
}
} catch (IOException e) {
Log.d("THIS IS", "THE EXCEPTIONN");
e.printStackTrace();
} finally {
isConnected = false;
}
}
}
I want to connect to a remote server from thread and keep sending strings. If the connection gets refused the thread should keep polling the port until the server is up again. How can I handle this exception and keep my thread fro crashing? The server may not be up for long time but thread should run indefinitely.
public void SendMessage(String message){
try {
socket = new Socket(actuatorAddress, destPort.get());
outToServer = socket.getOutputStream();
out = new DataOutputStream(outToServer);
out.flush();
out.write(message.getBytes());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
I changed some part of the code as below. For first time called Connect function and then subsequently called Send Message function through thread. The delay added to reconnecting helped reduce time lag recurred due to connecting to non existing server. Still think that there might be a better solution to the basic problem.
public boolean ConnectToActuator() {
try {
if(actuatorAddress.isReachable(2000)){
socket = new Socket();
socket.setPerformancePreferences(1, 2, 0);
socket.setTcpNoDelay(false);
socket.setSendBufferSize(32);
socket.connect(new InetSocketAddress(actuatorAddress, destPort.get()));
outToServer = socket.getOutputStream();
out = new DataOutputStream(outToServer);
connected = true;
disconnectedTimeout = 0;
}
}catch (ConnectException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}catch (IOException ex) {
connected = false;
System.out.println(ex.getMessage());
}
return connected;
}
public boolean SendToActuator(String message) {
if(connected == false){ //socket.isOutputShutdown()
disconnectedTimeout++;
if(disconnectedTimeout>20){
disconnectedTimeout = 0;
ConnectToActuator();
} else {
return connected;
}
}
try {
out.flush();
out.writeBytes(message);
disconnectedTimeout = 0;
connected = true;
} catch (UnknownHostException uhe) {
connected = false;
System.out.println(uhe.getMessage());
} catch (IOException ioe) {
connected = false;
System.out.println(ioe.getMessage());
}
return connected;
}
Given the following constraints in the comments:
Try to send the message to one of the 10 servers.
If none of the servers are available to receive the message, discard the message.
What you actually want to do is:
Iterate through a list of server addresses
Attempt to send a message to each of them
Break out of the loop right away if successful
Catch any errors on connection failure and try the next server
Here's an example class that will run through that scenario.
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MessageSender {
private static final Integer destPort = 1234;
private static final String[] serverAddresses = {
"address1",
"address2",
"address3" // Etc....
};
public Boolean SendMessage(String message) {
Boolean messageSentSuccessfully = false;
for (String addy : serverAddresses) {
messageSentSuccessfully = SendMessageToServer(addy, message);
if (messageSentSuccessfully) {
break;
}
}
return messageSentSuccessfully;
}
private Boolean SendMessageToServer(String serverAddress, String message) {
Boolean messageSent = false;
try {
Socket dataSocket = new Socket(serverAddress, destPort);
OutputStream outToServer = dataSocket.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.write(message.getBytes());
out.flush();
messageSent = true;
} catch (Exception e) {
System.out.println(e);
}
return messageSent;
}
}
Hope that helps.
Pseudo:
while(true){
if(connect()) DoClientConnectedStuff();
sleep(reconnectTimeout);
};
please try below changes. if your connection refuses it will wait for 2s(2000ms) and then again try to connect with server. if connection being successful it will take outputstream, write data in a while loop and flush the data.
public void createSocketConnection() throws IOException
{
socket = new Socket(actuatorAddress, destPort.get());
if(socket!=null)
{
outToServer = socket.getOutputStream();
out = new DataOutputStream(outToServer);
}
}
public void SendMessage(String message){
boolean isRunning=false;
try
{
createSocketConnection();
isRunning=true;
while(isRunning)
{
out.write(message.getBytes());
out.flush();
}
} catch (java.net.ConnectException conExcp) {
System.out.println(ex.getMessage());
try{
Thread.sleep(2000);
}catch(Exception ee){}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
I have this client class "T_Client" in a client-server implementation
T_Client:
public class T_Client{
private static final String TAG = "T_Client";
private static String serverIP = "192.168.2.5";
private static int port = 4444;
private InetAddress serverAddr = null;
private Socket sock = null;
private boolean running = false;
private ObjectInputStream in;
private ObjectOutputStream out;
Object objIn;
public void send(MessageCustom _msg) {
if (out != null) {
try {
out.writeObject(_msg);
out.flush();
// out.close();
Log.i("Send Method", "Outgoing : " + _msg.toString());
} catch (IOException ex) {
Log.e("Send Method", ex.toString());
}
}
}
public void stopClient() {
running = false;
}
public void run() {
running = true;
try {
// here you must put your computer's IP address.
serverAddr = InetAddress.getByName(serverIP);
Log.i("TCP Client", "C: Connecting...");
// create a socket to make the connection with the server
sock = new Socket(serverAddr, port);
try {
// send the message to the server
out = new ObjectOutputStream(sock.getOutputStream());
// receive the message which the server sends back
in = new ObjectInputStream(sock.getInputStream());
Log.i("TCP Client", "C: Connected.");
// in this while the client listens for the messages sent by the
// server
while (running) {
objIn = in.readObject();
Log.i("Object Read", objIn.toString());
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + objIn
+ "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
// the socket must be closed. It is not possible to reconnect to
// this socket
// after it is closed, which means a new socket instance has to
// be created.
out.close();
in.close();
sock.close();
Log.i(TAG, "Closing socket: " + sock);
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
}
And I want to get the Object from the server(objIn) and pass it to the UI thread so I can do some UI updates there. From searching around a bit, I think that I have to use a Handler, but can't really wrap my head around the implementation.
Could someone give me a simple example of how to implement a handler in my case, and make the call in a Main Activity? Or point me to a simple tutorial to get me started, cause I've tried to follow the one over at the Android Developers site but it's too complicated.