Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Server Socket is closed on client closed

Server Socket is closed on client closed

Scheduled Pinned Locked Moved Java
questionsysadmin
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin k Rajput
    wrote on last edited by
    #1

    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?

    L J G 3 Replies Last reply
    0
    • S Sachin k Rajput

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Trap the exception and dispose of it.

      Use the best guess

      S 1 Reply Last reply
      0
      • L Lost User

        Trap the exception and dispose of it.

        Use the best guess

        S Offline
        S Offline
        Sachin k Rajput
        wrote on last edited by
        #3

        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.

        L M 2 Replies Last reply
        0
        • S Sachin k Rajput

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Well, you need to catch it first.

          Use the best guess

          1 Reply Last reply
          0
          • S Sachin k Rajput

            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?

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • J jschell

              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.

              S Offline
              S Offline
              Sachin k Rajput
              wrote on last edited by
              #6

              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)?

              L 1 Reply Last reply
              0
              • S Sachin k Rajput

                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)?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I already told you twice what to do: catch the exception and dispose it, so the server continues to listen in an idle state. How do you think the rest of the internet works?

                Use the best guess

                1 Reply Last reply
                0
                • S Sachin k Rajput

                  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?

                  G Offline
                  G Offline
                  gongxufan china java
                  wrote on last edited by
                  #8

                  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 !

                  S 1 Reply Last reply
                  0
                  • G gongxufan china java

                    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 !

                    S Offline
                    S Offline
                    Sachin k Rajput
                    wrote on last edited by
                    #9

                    Thanks! This is the thing, I was looking for. Let me try this in my project.

                    1 Reply Last reply
                    0
                    • S Sachin k Rajput

                      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.

                      M Offline
                      M Offline
                      MarlBermudoNights
                      wrote on last edited by
                      #10

                      try{
                      //code that causes the exception
                      }catch(SocketException e){
                      Sytem.err.println(e);
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups