简体   繁体   English

Java客户端服务器聊天应用程序失去连接

[英]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. 我最近用Java编写了一个套接字通信程序,其中两个线程在每个服务器和客户端上同时运行,从而处理对套接字的读取和写入操作,从而允许双方连续地发送和接收消息。

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 . 只是我最初看到的代码在第一次while(true){}中运行时,就生成了一个调用start方法的线程。 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. 因为在您的第一个while(true)中有一个true,所以会再次产生一个新线程,并且该线程继续进行直到套接字关闭为止,在该套接字中,由于system.exit调用而导致程序终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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