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

时间:2018-10-11 14:53:51

标签: java multithreading sockets networking

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

问题是客户端或服务器停止从另一端接收通信,然后过了一会儿它们都停止工作。我不知道出了什么问题以及连接断开的方式:/

服务器代码

  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); 
} 
} 

客户代码

      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);

   }

   }

1 个答案:

答案 0 :(得分:0)

仅在您的代码中进行最初的运行(我在第一时间while(true){}中看到的)时,您就生成了一个调用start方法的线程。启动读取线程的那一刻,主线程将检查套接字条件并继续前进。由于在您的第一个while(true)中存在一个true,因此会再次产生一个新线程,该线程一直持续进行直到套接字关闭为止,该套接字由于system.exit调用而终止了程序。