java多客户聊天服务器有问题。客户无法接收消息?

时间:2015-11-19 07:26:43

标签: java multithreading sockets server client

客户端无法相互通信。只有服务器接收所有消息。第一个客户端只能从服务器接收消息。请帮忙!

以下是服务器和客户端代码。如下所示,我们使用套接字连接并正在实现线程。我们使用线程打印多行。问题在于接受连接的部分。即使连接被接受,我们也无法与客户通信。

    /*Server Code*/
    package gossipserver;
    import java.io.*;
    import java.net.*;
    public class GossipServer
    {
    public static void main(String[] args) throws Exception
    {
    try{
    ServerSocket sersock = new ServerSocket(4778);    //opening of a socket
    System.out.println("Server ready");
    boolean listeningSocket = true;
    while(listeningSocket){
    Socket sock = sersock.accept( );  
   System.out.println("Connection received from client on  port "+sock.getPort());
  //sock.getport() to return port number of client
  //create two threads to send and receive
  ReceiveThread ReadThread = new ReceiveThread(sock);
  Thread thread1 = new Thread(ReadThread);
  thread1.start();
  SendThread WriteThread = new SendThread(sock);
  Thread thread2 = new Thread(WriteThread);
  thread2.start();
  MClient thread3 = new MClient (sock);
  thread3.start();
  }
  sersock.close();        
  }  
  catch(Exception e)
  {
      System.out.println("Error" +e);
  }
 }
 }

    class ReceiveThread implements Runnable     //creating class thread as runnable
    {
Socket clientSocket=null;
BufferedReader keyRead = null;  // reading from keyboard (keyRead  object)
public ReceiveThread(Socket clientSocket)
{
    this.clientSocket = clientSocket;
}//end constructor
public void run() {
    try{    
    keyRead = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
            //get inputstream
    String receiveMessage;
    while(true){
    while((receiveMessage = keyRead.readLine())!= null){
          //assign message from client to receiveMessage
        if(receiveMessage.equals("Quit"))
        {
            break;//break to close socket if Quit
        }
        System.out.println("Client"+"("+clientSocket.getPort()+"):"+receiveMessage);
            //print the message from client
    }
    this.clientSocket.close();
}
}
catch(Exception ex){System.out.println(ex.getMessage());}
       }//end run
           }//end class ReceiveThread

    class SendThread implements Runnable
    {
   PrintWriter pwrite;
Socket clientSock = null;

public SendThread(Socket clientSock)
{
    this.clientSock = clientSock;
}
public void run() {
    try{
    pwrite = new PrintWriter(new OutputStreamWriter(this.clientSock.getOutputStream()));
            //get outputstream
    System.out.println("Press enter to send.");
    while(true)
    {
        String sendMessage = null;
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                    //get userinput         
        sendMessage = input.readLine();//get message to send to client
        pwrite.println(sendMessage);//send message to client with PrintWriter
        pwrite.flush();//flush the PrintWriter

    }//end while
    }
    catch(Exception ex){System.out.println(ex.getMessage());}   
}//end run
    }//end class SendThread

     class MClient extends Thread 
     {
    private Socket socket = null;
    public MClient (Socket socket) {
    super("MClient");         //invoking overridden method through the use of the keyword super
    socket = socket;
}
public void run(){
         //Read input and process
    }
          //implement methods
} 






    /*Client Code*/
    package client;
    import java.io.*;
    import java.net.*;
    public class Client
    {
    public static void main(String[] args) throws Exception
     {
       try
      {
     Socket sock = new Socket("127.0.0.1", 4778);   //opening of a socket
     System.out.println("Client is connected to server. Ready to chat");
     //create two threads to send and receive
     SendThread WriteThread = new SendThread(sock);
     Thread thread1 = new Thread(WriteThread);
     thread1.start();
     ReceiveThread ReadThread = new ReceiveThread(sock);
     Thread thread2 =new Thread(ReadThread);
     thread2.start();
      }
 catch(Exception e)
      {
          System.out.println("Error" + e);
      }
  }
}

class ReceiveThread implements Runnable    //creating class thread as runnable
{
    Socket sock=null;
    BufferedReader keyread=null;    // reading from keyboard (keyRead object)
    public ReceiveThread(Socket sock) {
        this.sock = sock;
    }//end constructor
    public void run() {
    try{
    keyread = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
            //get inputstream
        String receiveMessage = null;
        while((receiveMessage = keyread.readLine())!= null)
        {
            System.out.println("Server: " + receiveMessage);
        }
        }catch(Exception e){System.out.println(e.getMessage());}
         }//end run
}//end class ReceiveThread

class SendThread implements Runnable
{
    Socket sock=null;
    PrintWriter pwrite=null;
    BufferedReader receiveRead=null;

    public SendThread(Socket sock)
    {
        this.sock = sock;
    }//end constructor
    public void run(){
        try{
        if(sock.isConnected())
        {
    System.out.println("Client connected to server on port "+sock.getPort());
            //sock.getport() to return port number of server
    this.pwrite = new PrintWriter(sock.getOutputStream(), true);
            //get outputstream
        System.out.println("Press enter to send. Type 'Quit' to quit chat");
        while(true){
            receiveRead = new BufferedReader(new InputStreamReader(System.in));
            String sendMessage=null;
            sendMessage = receiveRead.readLine();
            this.pwrite.println(sendMessage);
            this.pwrite.flush();
            if(sendMessage.equals("Quit"))
            break;
            }//end while
        sock.close();}}catch(Exception e){System.out.println(e.getMessage());}
        }//end run method
}//end class SendThread

0 个答案:

没有答案