Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: getting closed server socket in client thread

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question unable to get server socket in client thread

    Hi ,
    I am developing client server based application.
    After reading the data from the server clients open the thread and starts it in the thread im unable to get data from server when i do it says socket is already closed even the server program is running.
    Following is my server THREAD code : which send data to client :
    [code]
    ServerSocket main = ser.Socket();
    while(true){
    clientsocket = main.accept();

    rdr= new BufferedReader( new InputStreamReader(clientsocket.getInputStream()));
    String t="";
    String name=rdr.readLine();
    System.out.println("Client connected '"+ name +"'");
    LstClient.AddClient(name, clientsocket);
    ser.clientsNames = LstClient.ClientNames();
    objwriter = new ObjectOutputStream(clientsocket.getOutputStream()) ;
    objwriter.writeObject(ser.clientsNames);
    objwriter.flush();
    cam = new Camera();
    System.out.println("Turning Camera ON");
    cam.cam().open();
    System.out.println("Camera live , sending thread on");
    Thread.sleep(1000);
    wr = new BufferedWriter(new OutputStreamWriter(clientsocket.getOutputStream()) );
    wr.write("abc"+'\n');**client thread will read this**

    [/code/
    following the the client when receives data from server till this line before "TURING CAM ON" and than it donot receive data , it opens a thread and there client need to receive abc from server. Following is the client class :
     public Client(String serverIp,int port,String name) throws Exception{
     
            try{
                //CloseSocket();
                mainSock= new Socket(serverIp, port);
                writer= new BufferedWriter(new OutputStreamWriter(mainSock.getOutputStream()));
                writer.write(name);
                writer.write('\n');
                writer.flush();
                objin = new ObjectInputStream(mainSock.getInputStream());
                ClientsName = (String[])objin.readObject();
                System.out.println("Client no.'"+ ClientsName.length );
            }
            catch(Exception ex){
                 CloseSocket();
                throw new Exception("Error : "+ ex.getMessage());
     
            }
            finally{
                if(writer!=null){
                    writer.close();
                }
                if(objin!=null){
                    objin.close();
                }
     
     
            }
     
        }
        public void ReadVideo(){
     
            try{
            if(mainSock!=null){
                System.out.println("Starting reading video in 7 sec");
                camrdr = new TCameraReader(mainSock);
                Thread.sleep(7500);
                System.out.println("Starting reading, thread started");
                camrdr.start();
            }
            else {
                System.out.println("Server not found");
     
            }

    and this is the thread clients opens but it says socket is closed.

          private ObjectInputStream objin=null;
        private Socket serverSocket=null;
        private Webcam cam=null;
        public TCameraReader(Socket c)
        {
            serverSocket = c;
     
        }
     
     
        @Override
        public void run(){
            try{
     
                    try{
                        System.out.println("Reading video");
                        BufferedReader rdr= new BufferedReader( new InputStreamReader(serverSocket.getInputStream()));
                        System.out.println(rdr.readLine());
    [B]                    **here is says socket is closed - here it need to read abc from server**
    [/B]                    
                    }
                    catch(Exception ex1){
                        throw new InterruptedException(ex1.getMessage());
                    }
     
                    finally{
                        try{
                            if(objin!=null)
                            {
                                objin.close();
                            }
                        }
                        catch(Exception ex3){
                            throw new InterruptedException(ex3.getMessage());
                        }
     
                    }
     
     
            }
            catch(InterruptedException ex){
                throw new RuntimeException(ex.getMessage());
            }
     
     
        }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question getting closed server socket in client thread

    Hi ,
    I am developing client server based application.
    After reading the data from the server clients open the thread and starts it in the thread im unable to get data from server when i do it says socket is already closed even the server program is running.
    Following is my server THREAD code : which send data to client :

    ServerSocket main = ser.Socket();
    while(true){
    clientsocket = main.accept();

    rdr= new BufferedReader( new InputStreamReader(clientsocket.getInputStream()));
    String t="";
    String name=rdr.readLine();
    System.out.println("Client connected '"+ name +"'");
    LstClient.AddClient(name, clientsocket);
    ser.clientsNames = LstClient.ClientNames();
    objwriter = new ObjectOutputStream(clientsocket.getOutputStream()) ;
    objwriter.writeObject(ser.clientsNames);
    objwriter.flush();
    cam = new Camera();
    System.out.println("Turning Camera ON");
    cam.cam().open();
    System.out.println("Camera live , sending thread on");
    Thread.sleep(1000);
    wr = new BufferedWriter(new OutputStreamWriter(clientsocket.getOutputStream()) );
    wr.write("abc"+'\n');**client thread will read this**


    following the the client when receives data from server till this line before "TURING CAM ON" and than it donot receive data , it opens a thread and there client need to receive abc from server. Following is the client class :

    public Client(String serverIp,int port,String name) throws Exception{

    try{
    //CloseSocket();
    mainSock= new Socket(serverIp, port);
    writer= new BufferedWriter(new OutputStreamWriter(mainSock.getOutputStream()));
    writer.write(name);
    writer.write('\n');
    writer.flush();
    objin = new ObjectInputStream(mainSock.getInputStream());
    ClientsName = (String[])objin.readObject();
    System.out.println("Client no.'"+ ClientsName.length );
    }
    catch(Exception ex){
    CloseSocket();
    throw new Exception("Error : "+ ex.getMessage());

    }
    finally{
    if(writer!=null){
    writer.close();
    }
    if(objin!=null){
    objin.close();
    }


    }

    }
    public void ReadVideo(){

    try{
    if(mainSock!=null){
    System.out.println("Starting reading video in 7 sec");
    camrdr = new TCameraReader(mainSock);
    Thread.sleep(7500);
    System.out.println("Starting reading, thread started");
    camrdr.start();
    }
    else {
    System.out.println("Server not found");

    }



    and this is the thread clients opens but it says socket is closed.

    private ObjectInputStream objin=null;
    private Socket serverSocket=null;
    private Webcam cam=null;
    public TCameraReader(Socket c)
    {
    serverSocket = c;

    }


    @Override
    public void run(){
    try{

    try{
    System.out.println("Reading video");
    BufferedReader rdr= new BufferedReader( new InputStreamReader(serverSocket.getInputStream()));
    System.out.println(rdr.readLine());
    **here is says socket is closed - here it need to read abc from server**

    }
    catch(Exception ex1){
    throw new InterruptedException(ex1.getMessage());
    }

    finally{
    try{
    if(objin!=null)
    {
    objin.close();
    }
    }
    catch(Exception ex3){
    throw new InterruptedException(ex3.getMessage());
    }

    }


    }
    catch(InterruptedException ex){
    throw new RuntimeException(ex.getMessage());
    }


    }

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: unable to get server socket in client thread

    Possible duplicate. merging.

    @OP: please edit your post to make it readable. Put [code] at the start of a section of code and [/code] at the end: that wy the code will be formatted. You may have to check that the indentation is correct.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unable to get server socket in client thread

    Hi ,
    I am developing client server based application.
    After reading the data from the server clients open the thread and starts it in the thread im unable to get data from server when i do it says socket is already closed even the server program is running.
    Following is my server THREAD code : which send data to client :
      ServerSocket main = ser.Socket();
                while(true){
                clientsocket = main.accept();
     
                rdr= new BufferedReader( new InputStreamReader(clientsocket.getInputStream()));
                String t="";
                String name=rdr.readLine();
                System.out.println("Client connected '"+ name +"'");
                LstClient.AddClient(name, clientsocket);
                ser.clientsNames = LstClient.ClientNames();
                objwriter = new ObjectOutputStream(clientsocket.getOutputStream());
                objwriter.writeObject(ser.clientsNames);
                objwriter.flush(); 
                cam = new Camera();
                System.out.println("Turning Camera ON");
                cam.cam().open();
                System.out.println("Camera live , sending thread on");
                Thread.sleep(1000);
                wr = new BufferedWriter(new OutputStreamWriter(clientsocket.getOutputStream()));
                    wr.write("abc"+'\n');[B]**client thread will read this**[/B]
    following the the client when receives data from server till this line before "TURING CAM ON" and than it donot receive data , it opens a thread and there client need to receive abc from server. Following is the client class :

     public Client(String serverIp,int port,String name) throws Exception{
     
            try{
                //CloseSocket();
                mainSock= new Socket(serverIp, port);
                writer= new BufferedWriter(new OutputStreamWriter(mainSock.getOutputStream()));
                writer.write(name);
                writer.write('\n');
                writer.flush();
                objin = new ObjectInputStream(mainSock.getInputStream());
                ClientsName = (String[])objin.readObject();
                System.out.println("Client no.'"+ ClientsName.length );
            }
            catch(Exception ex){
                 CloseSocket();
                throw new Exception("Error : "+ ex.getMessage());
     
            }
            finally{
                if(writer!=null){
                    writer.close();
                }
                if(objin!=null){
                    objin.close();
                }
     
     
            }
     
        }
        public void ReadVideo(){
     
            try{
            if(mainSock!=null){
                System.out.println("Starting reading video in 7 sec");
                camrdr = new TCameraReader(mainSock);
                Thread.sleep(7500);
                System.out.println("Starting reading, thread started");
                camrdr.start();
            }
            else {
                System.out.println("Server not found");
     
            }

    and this is the thread clients opens but it says socket is closed.

          private ObjectInputStream objin=null;
        private Socket serverSocket=null;
        private Webcam cam=null;
        public TCameraReader(Socket c)
        {
            serverSocket = c;
     
        }
     
     
        @Override
        public void run(){
            try{
     
                    try{
                        System.out.println("Reading video");
                        BufferedReader rdr= new BufferedReader( new InputStreamReader(serverSocket.getInputStream()));
                        System.out.println(rdr.readLine());
    [B][B]                    **here is says socket is closed - here it need to read abc from server**[/B][/B]
     
                    }
                    catch(Exception ex1){
                        throw new InterruptedException(ex1.getMessage());
                    }
     
                    finally{
                        try{
                            if(objin!=null)
                            {
                                objin.close();
                            }
                        }
                        catch(Exception ex3){
                            throw new InterruptedException(ex3.getMessage());
                        }
     
                    }
     
     
            }
            catch(InterruptedException ex){
                throw new RuntimeException(ex.getMessage());
            }
     
     
        }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getting closed server socket in client thread

    Can you make a small complete program the compiles, executes and shows the problem.
    It is not possible to compile and execute what you have posted for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting closed server socket in client thread

    Quote Originally Posted by Norm View Post
    Can you make a small complete program the compiles, executes and shows the problem.
    It is not possible to compile and execute what you have posted for testing.
    in first code : there is server thread waiting for client to connect. the second part is client main class which connects with ser and do write name , server update the clients name and send all the client name to client as String[] after receiving clientnames array from server for further receiving client opens the thread as third code snippet but in the
    BufferedReader rdr= new BufferedReader( new InputStreamReader(serverSocket.getInputStream()));

    it says server socket is closed on the other hand server is writing abc there which should be received from this thread.
    SHould i post all related code here ?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getting closed server socket in client thread

    SHould i post all related code here ?
    Can you make a small complete program the compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getting closed server socket in client thread

    i got error actually closing the streams close the socket , is ther any way i can close stream without closing the socket and reopen the stream
    thanks

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getting closed server socket in client thread

    Why close the stream?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. unable to get server socket in client thread
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 02:47 PM
  2. what's wrong with my code ? " server client socket
    By Wish.. in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 27th, 2012, 01:33 PM
  3. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  4. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  5. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM