UPDATE I've found that this error is related to the GZIP streams. If I remove them and just use Object streams rather than GZIP wrapped with Object streams then my issues are resolved, no exception. I have no idea why this is, if anyone can help that would be great.
I also noted while using the GZIP that the error would be very random. E.g it seemed to depend a lot on the data within the object that I was sending across the socket. Alter the object data and it would sometimes solve the issue. For example I had a user object which contained String values such as, first name, surname, phone number. If they were all set I was getting the below issue, however if I cleared them all to be empty String values then I wouldn't get the below exception. It's very bizarre to be honest
I have a client/server socket which sends object via GZIP streams. This has worked without issue in the past. However now I am seeing connection reset when sending certain data from the server back to the client.
The client connects to the server and sends a request, to which the server will reply and send back some data. I believe the error is from the server as the connection reset exception is presented on the client side.
Here is a snippet of the exception
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:863)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:820)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
It's a very strange issue because the same socket implementation is used for all client / server communication and in our test environment everything works fine. Also the same code is used successfully before the exception occurs to collect some data from the server when the client loads the application on startup.
The above information leads me to believe there could be an issue with the specific data that is being passed, although the client & server both present no exception other than the connection reset.
Looking at netstat ( netstat -anp ) while debugging I can see successful actions result in a socket in the state of "CLOSE_WAIT", however unsuccessful ones the socket just seems to disappear all together, it's not there in any state. I also note that there is a lot of RST when the connections are finished, even for sucessful cases where I don't get connection reset exception. My TCP knowledge isn't great, but I would have thought a clean connection would end with FIN rather than getting RST?
Here is an example of the code
Client:
ObjectOutputStream out = null;
ObjectInputStream ois = null;
SSLSocketFactory sf = AdminClientApplet.getSSLContext().getSocketFactory();
Socket socket = null;
DataResponse dataRes = null;
try
{
socket = sf.createSocket( server.getHost(), server.getPort() );
// Need to connect to server and send request type,
// ie what we are requesting to be sent to us
GZIPOutputStream gZipOut = new GZIPOutputStream(socket.getOutputStream());
out = new ObjectOutputStream(gZipOut);
// write our request
out.writeObject( dataReq );
out.flush();
gZipOut.finish();
InputStream in = socket.getInputStream();
GZIPInputStream gZipIn = new GZIPInputStream(in, 65536);
ois = new ObjectInputStream(gZipIn);
// read our response
dataRes = ( DataResponse )ois.readObject();
Log.CSSO.debug("Read object " + dataRes );
}
catch (IOException ie)
{
Log.GUIP.error("IOException communicating with Admin Server on "+server, ie);
}
catch ( Exception e)
{
Log.GUIP.error("Unexpected exception communicating with Admin Server on "+server, e);
}
catch (Throwable t)
{
Log.GUIP.error("Unexpected exception communicating with Admin Server on "+server, t);
}
finally
{
// now close the connection gracefully
CSSO.debug("closing socket");
try
{
if ( out != null )
out.close();
if ( ois != null )
ois.close();
if ( socket != null )
socket.close();
}
catch ( Exception e )
{
Log.CSSO.error( "Error closing socket connection during getDataFromServer()", e );
}
}
The server has already read the 'request' at this point, so this is just the part that returns the requested data to the client.
Server:
Socket socket = null;
try
{
socket = request.getSocket();
socket.setTcpNoDelay(true);
Log.CSSO.debug("Opening a response output stream to socket "+socket );
gZipOut = new GZIPOutputStream(socket.getOutputStream(), 65536 );
out = new ObjectOutputStream(gZipOut);
// the actual 'data' we are going to return
obj = getObject();
Log.CSSO.debug("About to write " + obj + " to socket" + socket.getRemoteSocketAddress() );
out.writeObject( obj );
if (Log.CSSO.isDebugEnabled())
{
Log.CSSO.debug("Wrote DataResponse to socket " + obj );
}
out.flush();
gZipOut.finish();
}
catch (IOException ie)
{
Log.CSSO.error("IOException caught sending data to client", ie);
}
catch (Exception e)
{
Log.CSSO.error("Unexpected exception caught sending data to client", e);
}
finally
{
Log.CSSO.debug( "Closing socket to " + socket.getRemoteSocketAddress() );
try
{
if ( out != null ) out.close();
if ( socket != null ) socket.close();
}
catch ( IOException e )
{
Log.CSSO.error("Unexpected exception caught closing socket", e );
}
}
Log.FLOW.debug("< run");
When on debug logging the server runs all the way through to completion, without any exceptions ( so "< run" is written in the logs ). However the client errors at
ois = new ObjectInputStream(gZipIn);
The other item worth noting is that the live system, where the exception occours is running linux ( Centos ), where as I'm not able to replicate the exception on windows and it doesn't happen on Linux Mint either. I don't expect this would be the cause but just thought I should mention it.
Any help much appreciated as I'm lost as to the cause of the issue here.
Related
Hi and thanks in advance.
I am currently trying to create a client server program where the server responds to the client based on different requests coming in from the client.
Here is what I am trying to do for the first of the requests (the issue is the same for all of the clients different requests to the server, so fixing just this would help me fix all of them):
1) The client connects to the server
2) The client sends logon information to the server
3) The server checks the logon information for validity
4) The server sends a response saying that login was successful.
5) Client receives and displays response.
Here is the Client (the first try block is located in the client frame, the second within an action listener for a button on the client frame):
try
{
mySocket = new Socket("localhost", 2016);
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
try
{
DOS = new DataOutputStream(mySocket.getOutputStream());
DOS.writeUTF(txtName.getText() + " " + txtPassword.getText());
DOS.flush();
DOS.close();
DataInputStream DISLog = new DataInputStream(mySocket.getInputStream());
JOptionPane.showMessageDialog(null, DISLog.readLine());
}
catch(IOException e1)
{
e1.printStackTrace();
}
Server:
System.out.println("Waiting for client....");
ServerSocket myServerSocket = new ServerSocket(2016);
Socket mySocket = myServerSocket.accept();
myClientHandler = new EZFILEHandler(mySocket);
//Log in check
DataInputStream DISLog = new DataInputStream(mySocket.getInputStream());
StringTokenizer ST = new StringTokenizer(DISLog.readLine());
String Name = ST.nextToken();
String Password = ST.nextToken();
//DISLog.close();
boolean Found = myClientHandler.matchUser(Name,Password);
DataOutputStream DOS = new DataOutputStream(mySocket.getOutputStream());
if (Found == true)
{
DOS.writeUTF("You are logged in.");
DOS.flush();
LoggedIn = true;
}
if (Found == false)
{
DOS.writeUTF("You could not log in.");
DOS.flush();
}
Here is the code for the ClientHandler mentioned in the Server code:
public EZFILEHandler(Socket newConnectionToClient)
{
try
{
mySocket = new Socket("localhost", 2016);
}
catch(Exception e)
{
e.printStackTrace();
}
}
Running the above code will give a Socket is closed exception at this line in the client: DataInputStream DISLog = new DataInputStream(mySocket.getInputStream());, so I then tried commenting out the line: DOS.close(); in the client to keep the socket open, but that causes the button which calls the client code to freeze the program and points to this line if I terminate the program: JOptionPane.showMessageDialog(null, DISLog.readLine());.
So my question is this: How do I allow the client and server to respond to each other in different ways without the socket closing or the jswing freezing from not being allowed to finish processing?
Also, how would it be possible for me to allow multiple clients to logon to the server and all their requests still be processed safely?
Thank you.
If you are writing a Swing program, your socket communication should be on its own SwingWorker thread.
Let's ignore that, and pretend it is just debugging code, not intended to stay.
DISLog.readLine() will read until it reaches the newline character, or the end of the input stream. The server is executing (say) the following:
DOS.writeUTF("You are logged in.");
DOS.flush();
No newline there. And flush() does not close the stream; it simply ensures all the characters are sent, not left in the buffer to be combine with the next set of characters to send.
Catch-22? Yup! The client is waiting for \n, and the server is waiting for more commands from the client.
Change the client to use readUTF().
Update Looks like you need to change the server to use readUTF() as well. The server is probably not getting past the new StringTokenizer(DISLog.readLine()) call unless the client closes the socket.
Im telneting into a server and it will block the port if I do not disconnect properly. Im already using socket.close(); so I am not sure what I am doing wrong to disconnect completely from the server
//java socket client example
import java.io.*;
import java.net.*;
public class socket_client {
public static void main(String[] args) throws IOException {
Socket s = new Socket();
String host = "1.1.1.1";
PrintWriter s_out = null;
BufferedReader s_in = null;
try {
s.connect(new InetSocketAddress(host, 12656));
System.out.println("Connected");
// writer for socket
s_out = new PrintWriter(s.getOutputStream(), true);
// reader for socket
s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
// Host not found
catch (UnknownHostException e) {
System.err.println("Don't know about host : " + host);
System.exit(1);
}
// Send message to server
String message = "this is the msg";
s_out.println(message);
System.out.println("Message send");
// Get response from server
String response;
while ((response = s_in.readLine()) != null) {
System.out.println(response);
}
// close the socket
s.close();
// close the i/o streams
s_out.close();
s_in.close();
}
}
There is no disconnect sub-protocol in Telnet. All you have to do is close the socket.
I've never seen or heard of a Telnet server 'block a port if I do not disconnect properly'. I have a production Telnet client which does only that, and which has been working correctly for five or six years. And any server at all that doesn't handle unexpected disconnections properly has something very seriously wrong with it.
The problem is elsewhere, possibly in the (unspecified) server itself. To behave as you describe, it would have to completely ignore end of stream conditions, and ignore IOExceptions as well (or else treat them as completely fatal to the entire process). It would also have to be single-threaded. I'm finding it rather difficult to believe in the existence of such a server, or indeed this problem.
NB you only need to close 's_out', the outermost stream/writer you've wrapped around the socket output stream. If you must close the input stream and the socket, do so after closing the output stream/writer.
The socket is blocked since the server side is not handling unexpected socket closing. You have two alternatives - or rather, two steps, if you want to be thorough - to fixing this.
Handle the other end of the connection closing unexpectedly in an
exception handler, and closing the socket when needed.
Having the client send a message to the server when it wants to
close the connection, allowing the server to close the socket, and
then handling that closed socket as a successful operation.
This is an example of server socket code from O'Reilly that gracefully handles unexpected termination:
try {
ServerSocket server = new ServerSocket(5776);
while (true) {
Socket connection = server.accept( );
try {
OutputStreamWriter out
= new OutputStreamWriter(connection.getOutputStream( ));
out.write("You've connected to this server. Bye-bye now.\r\n");
connection.close( );
}
catch (IOException e) {
// This tends to be a transitory error for this one connection;
// e.g. the client broke the connection early. Consequently,
// we don't want to break the loop or print an error message.
// However, you might choose to log this exception in an error log.
}
finally {
// Most servers will want to guarantee that sockets are closed
// when complete.
try {
if (connection != null) connection.close( );
}
catch (IOException e) {}
}
}
catch (IOException e) {
System.err.println(e);
}
I'm writing a simple TCP client/server program pair in Java, and the server must disconnect if the client hasn't sent anything in 10 seconds. socket.setSoTimeout() gets me that, and the server disconnects just fine. The problem is - how can I get the client to determine if the server is closed? Currently I'm using DataOutputStream for writing to the server, and some answers here on SO suggest that writing to a closed socket will throw an IOException, but that doesn't happen.
What kind of writer object should I use to send arbitrary byte blocks to the server, that would throw an exception or otherwise indicate that the connection has been closed remotely?
Edit: here's the client code. This is a test function that reads one file from the file system and sends it to the server. It sends it in chunks, and pauses for some time between each chunk.
public static void sendFileWithTimeout(String file, String address, int dataPacketSize, int timeout) {
Socket connectionToServer = null;
DataOutputStream outStream = null;
FileInputStream inStream = null;
try {
connectionToServer = new Socket(address, 2233);
outStream = new DataOutputStream(connectionToServer.getOutputStream());
Path fileObject = Paths.get(file);
outStream.writeUTF(fileObject.getFileName().toString());
byte[] data = new byte[dataPacketSize];
inStream = new FileInputStream(fileObject.toFile());
boolean fileFinished = false;
while (!fileFinished) {
int bytesRead = inStream.read(data);
if (bytesRead == -1) {
fileFinished = true;
} else {
outStream.write(data, 0, bytesRead);
System.out.println("Thread " + Thread.currentThread().getName() + " wrote " + bytesRead + " bytes.");
Thread.sleep(timeout);
}
}
} catch (IOException | InterruptedException e) {
System.out.println("Something something.");
throw new RuntimeException("Problem sending data to server.", e);
} finally {
TCPUtil.silentCloseObject(inStream);
TCPUtil.silentCloseObject(outStream);
TCPUtil.silentCloseObject(connectionToServer);
}
}
I'd expect the outStream.write to throw an IOException when it tries to write to a closed server, but nothing.
I'd expect the outStream.write to throw an IOException when it tries to write to a closed server, but nothing.
It won't do that the first time, because of the socket send buffer. If you keep writing, it will eventually throw an IOException: 'connection reset'. If you don't have data to get to that point, you will never find out that the peer has closed.
I think you need to flush and close your stream after written like outStream.flush(); outStream.close(); inStream.close();
Remember ServerSocket.setSoTimeout() is different from client's function with same name.
For server, this function only throws SocketTimeoutException for you to catch it if timeout is expired, but the server socket still remains.
For client, setSoTimeout() relates to 'read timeout' for stream reading.
In your case, you must show your server code of closing the connected socket after catching SocketTimeoutException => ensure server closed the associated socket with a specified client. If done, at client side, your code line:
throw new RuntimeException("Problem sending data to server.", e);
will be called.
[Update]
I noticed that you stated to set timeout for the accepted socket at server side to 10 secs (=10,000 milliseconds); for that period, did your client complete all the file sending? if it did, never the exception occurs.
[Suggest]
for probing, just comment out your code of reading file content to send to server, and try replacing with several lines of writing to output stream:
outStream.writeUTF("ONE");
outStream.writeUTF("TWO");
outStream.writeUTF("TREE");
Then you can come to the conclusion.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java.net.SocketException: Software caused connection abort: recv failed
I'm trying to write a client server pair where the connection is live all day and the client waits for the server to send a message. The steps are:
Server opens port and listens for connection
Client connects in and waits for data
Some time later (maybe hours) the server sends data to the client
Client processes data and returns it to the server
Repeat steps 3 and 4
I can get steps 1-4 working, however if I try to repeat step 3 I get the error in the title.
This is my method on the client side:
private static void waitForInput(SSLSocket sslsocket) throws IOException {
do {
try {
ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
Object o = (Object) (ois.readObject());
// excluded code to process data
oos.flush();
oos.writeObject(o);
oos.reset();
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
} while ( true );
}
The code fails on the 4th line, The first time around it blocks and waits until I get the next bit of data, but it doesn't work twice. What am I doing wrong?
Connection abort IOException is thrown when you are waiting to read from a socket that has been closed at the other end, check to see your server side code , if you are not accidentally closing the socket.
Also the server side code could be uploaded for deeper analysis, also try posting the stack trace, it will help analyzing.
You could move the declaration for ois and oos out of the do ... while loop, since there is no need to redeclare them everytime, might need a try ... catch around that.
private static void waitForInput(SSLSocket sslsocket) throws IOException {
ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
do {
try {
Object o = (Object) (ois.readObject());
// excluded code to process data
oos.writeObject(o);
oos.flush();
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
} while ( true );
}
And I have removed the oos.reset(); and moved the oos.flush();
I believe that the problem is the oos.reset(); and i would never reset a connection that is supposed to be persistent for hours, or, at least part of it.
Besides there is already a ois for that connection and you don't need two of them.
InetAddress Address = InetAddress.getByName("172.24.3.154");
kkSocket = new Socket(Address, 2003);
out = new ObjectOutputStream(kkSocket.getOutputStream());
in = new ObjectInputStream(kkSocket.getInputStream());
public static <T> Object sendReceive(T obj) {
try {
out.writeObject(obj);
out.flush();
System.out.println("Client : " + obj.toString());
Object resp = in.readObject();
if (resp != null) {
System.out.println("Server : " + resp.toString());
}
return resp;
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
this is my Client method that i send a request to the Server.
out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
System.out.println("Server: S-a conectat :" + socket.getInetAddress());
Protocol protocol = new Protocol(server);
while (socket.isClosed() != true) {
Object response = protocol.processInput(in.readObject());
System.out.println("Server: message Received: " + getName());
if (response != null) {
out.writeObject(response);
out.flush();
} else {
out.writeObject(null);
out.flush();
}
}
this is what my server does. And it works too.my question is : Giving this setup for the sockets how can i make a separate listener for the client in order to send at a certain time one message to the client , while the client still functions normally ?
I tryied to create a new thread fo manage the input stream on the client side , but the app won`t start and it just gets stuck in the run method of the thread
Thx.
EDIT :
What I am doing is a multi client App whith sockets,using mutli threading.I have the code above and works for me, for calling the "sendReceive" method for making a request to the server and it returns something.What i am trying to do is when i receive a specific request i want to notify all the online clients.I applied the observer pattern like this: The server is Observable and the threads are Observers. when a specific request comes in i notify all the treads , but i cant get each threat to send to the clients immediately a message because the client doesnt listen.Maybe I'm going with this on the wrong way.Can someone Help pls?
Either you need to use two sockets (each on their own port - asynchronous communication) if you have no idea when the server will send a message to the client, or you need a better defined protocol that knows when to read, and when to write (one socket - synchronous communication).
If you can't predict when the client will need to read from the socket, then your app won't be able to figure it out either. :)
In the two socket approach, you could have one thread per socket, so you don't have to worry about I/O blocking.