0
votes

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);
   ​}
}
}
3
Dumping the whole code and asking us to solve it is not a good way to ask questions here. Post only the relevant code.Narendra Pathai

3 Answers

5
votes

Closing the "Buffered reader" in listenThread and "PrintStream" in clientMenu causes the issue. When you close the returned Input / Output Stream, the underlying socket also gets closed.

Refer to the documentation here

I hope this solves the problem.

2
votes

'Socket closed' means that you closed the socket and then continued to use it. It's a bug in your code.

Note that it does not mean that the peer closed the connection.

There are numerous problems with your code, too many to review here. It is very poor quality. I suggest you get hold of the Custom Networking trail of the Java Tutorial and study it, and also look around for best practices in exception handling. At present you just catch certain exceptions, log them, and continue as though they hadn't happened. Code after a 'catch' block should not rely on things that happened in the 'try' block.

0
votes

try to remove this line in run method of listenthread class.

intosockb.close();

that is closing the stream, which is closing the socket.