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 6 of 6

Thread: Java Socket Exception Socket Closed

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Socket Exception Socket Closed

    I am trying to write a chat application using java sockets... The Concept is Server listens on a port for clients when client comes in as the first step the server will store as username and client's link socket as sockettuple(An object which has username as string and linksocket as socket) .. then after storing it sends the client as welcome you are connected ..Server listens in listenthread and communications happen in separate thread.. for client creation of socket happens in main thread and writing to server happens in separate thread In this the storing of socket tuple happens fine but on sending the message from server to client Socketclosed exception happens on both server write and client's read threads......
        // mainserver.java
        import java.net.*;
        import java.io.*;
        class listenthread extends Thread{
        ​ServerSocket mserver;
        ​Socket link,targetsocket;
        ​String user,targetuser;
        ​sockettuple targettuple,starray[]= new sockettuple[10];
        ​
        ​
        ​int i=0;
        ​
        ​public listenthread(ServerSocket mserver){
        ​try{
        ​System.out.println("Inside Listenthread constructor");
        ​ this.mserver=mserver;
        ​
        ​
        ​}
        ​catch (Exception e){
        ​System.out.println("Exception inside listenthread constructor"+e);
        ​}
        ​
        ​}
        ​public void run(){
        ​try{
        ​
        ​ while(true){
        ​
        ​ System.out.println("Listening.....");
        ​ link=mserver.accept();
        ​
        ​ System.out.println("Accepted");
        ​ BufferedReader intosockb = new BufferedReader(new InputStreamReader(link.getInputStream()));
        ​ user= intosockb.readLine();
        ​
        ​
        ​ System.out.println(user + "Accepted");
        ​
        ​ starray[i] =new sockettuple(link,user);
        ​ i++;
        ​ System.out.println(starray[i-1].username);
        ​
        ​ ​new servwrite(link).start();
        ​ ​
        ​ ​intosockb.close();
        ​ ​
        ​ }
        ​
        ​}
        ​catch(Exception e){
        ​System.out.println("Exception inside listenthread run "+e);
        ​e.printStackTrace();
        ​}
        ​}
        ​}
        class servwrite extends Thread{
        ​Socket link;
        ​
        ​public servwrite(Socket link){
        ​this.link=link;
        ​}
        ​public void run(){
        ​try{
        ​PrintStream fromserv=new PrintStream(link.getOutputStream());
        ​ fromserv.print("You are connected...Enter the target user:");
        ​ ​
        ​}
        ​catch(Exception e){
        ​System.out.println("Exception inside run of serv write thread..."+e);
        ​}
        ​}
        }
        ​
        public class mainserver {
        ​
        ​public static void main(String args[]){
        ​try{
        ​
        ​ServerSocket Servermain= new ServerSocket(4579);
        ​
        ​System.out.println(" Server main Socket created");
        ​
        ​new listenthread(Servermain).start();
        ​
        ​
        ​
        ​
        ​}
        ​catch(Exception e){
        ​System.out.println("Exception inside main "+e);
        ​}
        ​
        }
        }
        //Clientmenu.java
        import java.net.*;
        import java.io.*;
        class clientread extends Thread{
        ​Socket client;
        ​BufferedReader key=null;
        ​public clientread(Socket client){
        ​try{
        ​this.client=client;
        ​ key= new BufferedReader(new InputStreamReader(client.getInputStream()));
        ​}
        ​catch(Exception e){
        ​System.out.println("Exception inside client read thread's constructor");
        ​e.printStackTrace();
        ​}
        ​
        ​}
        ​public void run(){
        ​try{
        ​System.out.println("First line inside run of client read thread before cmsg readline");
        ​String cmsg= key.readLine();
        ​System.out.println(cmsg);
        ​}
        ​catch(Exception e){
        ​System.out.println("Exception inside run of client read thread..."+e);
        ​e.printStackTrace();
        ​}
        ​}
        }
        public class Clientmenu {
        ​
        public static void main(String args[]){
        ​String user=" ";
        ​String targetuser="";
        ​Socket newclient;
        ​BufferedReader fromtheserv;
        ​try{
        ​
        ​newclient = new Socket("127.0.0.1",4579);
        ​System.out.print("Enter username:");
        ​BufferedReader key= new BufferedReader(new InputStreamReader(System.in));
        ​user=key.readLine();
        ​
        ​PrintStream fromclientp=new PrintStream(newclient.getOutputStream());
        ​ fromclientp.print(user);
        ​
        ​new clientread(newclient).start();
        ​
        ​
        ​
        ​
        ​
        ​fromclientp.close();
        ​
        ​
        ​
        ​
        ​
        }
        ​
        ​catch(Exception e){
        ​System.out.println("Exception inside CLientmenu "+e);
        ​}
        }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Socket Exception Socket Closed

    Welcome to the forum, and thanks for taking the time to learn to post code correctly, although your code formatting has been lost, and your code is difficult to read. Even so, I can see that you've closed the socket(s) in the while( true ) loops that do the polling and responding. Don't do that.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Socket Exception Socket Closed

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum, and thanks for taking the time to learn to post code correctly, although your code formatting has been lost, and your code is difficult to read. Even so, I can see that you've closed the socket(s) in the while( true ) loops that do the polling and responding. Don't do that.
    Thank you Sir , I will try that out and see

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Socket Exception Socket Closed

    Let us know how it works out.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Socket Exception Socket Closed

    Sir I declared intosockb in class declaration
    initalized it inside run
    closed intosockb after while loop

    but still same exception occurs

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Socket Exception Socket Closed

    Somewhere the socket is being closed before it should be or it has never been properly opened. Find it and fix it. We can't help with code we can't see. Post the shortest code example possible that demonstrates the problem.

Similar Threads

  1. Replies: 12
    Last Post: October 22nd, 2013, 09:11 PM
  2. socket closed
    By tyler_long_sim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 05:52 PM
  3. getting closed server socket in client thread
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2013, 06:18 PM
  4. Replies: 0
    Last Post: October 5th, 2012, 07:27 AM
  5. How to detect if Socket is closed by remote peer?
    By anis_huq in forum Java Networking
    Replies: 1
    Last Post: July 13th, 2012, 09:36 AM