Server Socket is closed on client closed
-
I want to create a server socket(TCP) in which I want that server shouldn't be closed if client is closed. What I'm doing: I'm able to connect to client but when client is closed then server is throwing exception : Connection Reset and crashes How can I do it?
-
I want to create a server socket(TCP) in which I want that server shouldn't be closed if client is closed. What I'm doing: I'm able to connect to client but when client is closed then server is throwing exception : Connection Reset and crashes How can I do it?
-
I'm getting this exception: Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.readLine(BufferedReader.java:317) at java.io.BufferedReader.readLine(BufferedReader.java:382) at combined.Combined.dostuff(Combined.java:64) at combined.Combined.main(Combined.java:30) But not getting the way to dispose it.
-
I'm getting this exception: Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.readLine(BufferedReader.java:317) at java.io.BufferedReader.readLine(BufferedReader.java:382) at combined.Combined.dostuff(Combined.java:64) at combined.Combined.main(Combined.java:30) But not getting the way to dispose it.
-
I want to create a server socket(TCP) in which I want that server shouldn't be closed if client is closed. What I'm doing: I'm able to connect to client but when client is closed then server is throwing exception : Connection Reset and crashes How can I do it?
Indian Coder1989 wrote:
I want to create a server socket(TCP) in which I want that server shouldn't be closed if
client is closed.As stated that of course is illogical. Once the client terminates, regardless of how it happened, the server side connection can no longer be used.
-
Indian Coder1989 wrote:
I want to create a server socket(TCP) in which I want that server shouldn't be closed if
client is closed.As stated that of course is illogical. Once the client terminates, regardless of how it happened, the server side connection can no longer be used.
OfCourse the server will be disconnected and connection will be closed but program shouldn't be crashed. Isn't it possible that next time when client will again connected there will not be a need to start the server again(means server shouldn't be crashed)?
-
OfCourse the server will be disconnected and connection will be closed but program shouldn't be crashed. Isn't it possible that next time when client will again connected there will not be a need to start the server again(means server shouldn't be crashed)?
-
I want to create a server socket(TCP) in which I want that server shouldn't be closed if client is closed. What I'm doing: I'm able to connect to client but when client is closed then server is throwing exception : Connection Reset and crashes How can I do it?
I think you should make a loop in your server code. Just like the test code below.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class Test {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(888);
while(true) {
Socket s = server.accept();
Processer p = new Processer(s);
Thread t = new Thread(p);
t.start();
}
}
}class Processer implements Runnable {
private Socket socket;public Processer(Socket s) { // TODO Auto-generated constructor stub this.socket = s; } @Override public void run() { try { PrintWriter out=new PrintWriter(socket.getOutputStream(),true); out.println("HTTP/1.0 200 OK"); out.println("Content-Type:text/html;charset=utf-8"); out.println(); out.println("
web service test sucess!
");
out.close();
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
}
To test the result,using http://127.0.0.1:888 in yor brower :) Try it !
-
I think you should make a loop in your server code. Just like the test code below.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class Test {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(888);
while(true) {
Socket s = server.accept();
Processer p = new Processer(s);
Thread t = new Thread(p);
t.start();
}
}
}class Processer implements Runnable {
private Socket socket;public Processer(Socket s) { // TODO Auto-generated constructor stub this.socket = s; } @Override public void run() { try { PrintWriter out=new PrintWriter(socket.getOutputStream(),true); out.println("HTTP/1.0 200 OK"); out.println("Content-Type:text/html;charset=utf-8"); out.println(); out.println("
web service test sucess!
");
out.close();
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
}
To test the result,using http://127.0.0.1:888 in yor brower :) Try it !
Thanks! This is the thing, I was looking for. Let me try this in my project.
-
I'm getting this exception: Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.readLine(BufferedReader.java:317) at java.io.BufferedReader.readLine(BufferedReader.java:382) at combined.Combined.dostuff(Combined.java:64) at combined.Combined.main(Combined.java:30) But not getting the way to dispose it.
try{
//code that causes the exception
}catch(SocketException e){
Sytem.err.println(e);
}