简体   繁体   中英

Java Client Server Chat application loses connection

I recently wrote a socket communication program in java where two threads run concurrent on each server and client side handling the read and write operations to the socket allowing continuous message sending and receiving on both sides.

The problem is either of the client or the server stops receiving communication from the other side and then after a while both of them stop working. I can't figure out what's wrong and how the connection is dropping :/

Code for server

  import java.net.*; 
  import java.io.*; 
  import java.util.Scanner; 

 public class Server 
{ 

private Socket          socket   = null;  
private ServerSocket    server   = null; 
private DataInputStream in       =  null; 
private DataOutputStream out       =  null;
private Scanner inp       =  null;
String line = "";
String iline = "";      

public Server(int port) 
{ 
    try
    { 
        server = new ServerSocket(port); 
        System.out.println("Server started"); 

        System.out.println("Waiting for a client ..."); 

        socket = server.accept(); 
        System.out.println("Client accepted"); 

        // takes input from the client socket   
        out=new DataOutputStream(socket.getOutputStream());         

        in = new DataInputStream(new 
  BufferedInputStream(socket.getInputStream()));
        inp = new Scanner(System.in);           



        while (true) 
        { 



                new Thread(new Runnable(){

                    public void run() 
                    {
                        try{
                        while(true){
                            line = in.readUTF(); 
                            System.out.println("Client : "+line);

  if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()) 
    {
                            System.out.println("DED");
                            System.exit(0);
                }

                        }
                        }
                        catch(Exception e){
                        System.out.println("Exception !!!");
                        }
                    }
                    }).start();
                        iline=inp.nextLine();
                        out.writeUTF(iline);

 if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
                    System.out.println("DED");
                    System.exit(0);
                }
        } 
    } 
    catch(IOException i) 
    { 
        System.out.println(i); 
    } 
} 

public static void main(String args[]) 
{ 
    Server server = new Server(5000); 
} 
} 

Code for Client

      import java.net.*; 
      import java.io.*;
      import java.util.Scanner; 
      class Client{

private Socket socket =null;
private DataInputStream inp=null;
private DataOutputStream out=null;
private Scanner in=null;
String line="";
String iline="";
Client(String address,int port)
{
    try{

        socket = new Socket(address,port);
        in= new Scanner(System.in);
        out = new DataOutputStream(socket.getOutputStream());
        inp = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

        while(true){

            line=in.nextLine();
            out.writeUTF(line);
            new Thread(new Runnable(){

                public void run() 
                {

                    try{
                    while(true){
                    iline=inp.readUTF();
                    System.out.println("Server : "+iline);
                    if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
                    System.out.println("DED");
                    System.exit(0);
                }                       
                    }
                    }
                    catch(Exception e){
                        System.out.println("Exception !!!");
                    }
                }

            }).start();
                    if(socket.isClosed()||socket.isOutputShutdown()||socket.isInputShutdown()){
                    System.out.println("DED");
                    System.exit(0);
                }
        }   

    }
    catch(UnknownHostException u) 
        { 
            System.out.println(u); 
        } 
    catch(Exception e){
        System.out.println("EXCEPTION!!!");
    }
}
 }


 class ClientSocket{

public static void main(String...args){
    Client obj = new Client("127.0.0.1", 5000);

   }

   }

Just an initial run through your code what I see is in the first while(true){} , you are spawning a thread calling start method on it . The moment you start the reading thread the main thread checks fo sockets conditions and moves ahead. Since there is a true in your first while(true) a new thread is again spawned and that goes on an on until the socket is closed where the programs terminates because of system.exit call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM