我如何知道哪个客户端在多线程服务器中发送消息

时间:2014-05-13 04:12:35

标签: java multithreading client-server client

我正在创建客户端服务器体系结构(JAVA),每次客户端连接到服务器时,都会为客户端生成一个新线程。我的问题是会有多个客户端向我的服务器发送消息,我不知道哪个客户端正在发送消息。客户端和服务器托管在同一台计算机上

我有一个函数:ReadFromClient,它从客户端

读取输入

我如何知道哪个客户端正在发送消息?

将会产生的线程代码

package javaassignment3;

import java.net.*;
import java.io.*;

public class NetConnectClientHandler implements Runnable
{

    public  Socket clientSocket;
    public  Thread t;
    public  int serverNumber;

    public  PrintWriter out;
    public  BufferedReader in;

    public String msg;

    boolean cont;
    boolean cont2;

    public NetConnectClientHandler(Socket s,int serverNumber)
    {
        this.cont = false;
        this.clientSocket = s;
        this.serverNumber = serverNumber;
        t = new Thread(this);


        //setup the server
        try
        {
             out = new PrintWriter(clientSocket.getOutputStream(), true);
             in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
         catch(IOException e)
        {
            System.out.println("Read or write to socket failed.");
            System.exit(2);    
        }

        t.start();
    }

    public  void WriteToOneClient(String inputLine,int index)
    {
        NetConnectThreadedServer.clientList.get(index).out.println(inputLine);
        //out.println(inputLine);
    }

    public void WriteToAllClient(String inputLine)
    {
        int i =0;
        for (NetConnectClientHandler client : NetConnectThreadedServer.clientList)
               {
                   client.WriteToOneClient(inputLine,i);
                   i++;
               }
    }

    public String ReadFromClient()
    {
         String inputLine;
        try
        {
               inputLine=in.readLine();
               return inputLine;
        }
           catch(IOException e)
        {
            System.out.println("Read or write to socket failed.");
            System.exit(2);    
        }


        return "willneverreachhere";

    }

    public void run()
    {
        System.out.println("Threaded Server number " + serverNumber + " started");

       for (int i=0;i<4;i++)
       {
           String r = ReadFromClient();
             if (r.equals("123_3"))
           {
               cont2 = true;
               System.out.println("HeHe");
           }


       }



        System.out.println("Threaded Server "+serverNumber+" socket closed");


    }
}

客户代码

package javaassignment4;

import java.awt.Dimension;
import java.net.*;
import java.io.*;
import javax.swing.JFrame;

public class NetConnectClient 
{
     public static JFrame jframe;

     public static String hostName;
     public static int port;
     public static InetAddress ina;
     public static Socket s;
     public static DataOutputStream writeToSocket;
     public static DataInputStream readFromSocket;

      public static BufferedReader d;
      public static byte[] inputBuffer;



     public static void main(String [] args)
    {
        jframe = new JFrame();
        jframe.setPreferredSize(new Dimension(1250,750));
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        new NetConnectClient().run();

        System.out.println();
    }



    public void run()
    {

        Utility.SetUpConnection();
    }

    public static void SetUpConnection()
    {
        //make sure the server is started before running the client
        NetConnectClient.hostName = "localhost";
        NetConnectClient.port = 4445;

        InetAddress ina = null;
        try
        {
            //internet address , hostname
            ina = InetAddress.getByName(NetConnectClient.hostName);
        }
        catch(UnknownHostException u)
        {
            System.out.print("Cannot find host name");
            System.exit(0);
        }

         NetConnectClient.s = null;
        try
        {
            //try to connect to host
            NetConnectClient.s = new Socket(ina,NetConnectClient.port);    

        }
        catch(IOException ex)
        {
            System.out.print("Cannot connect to host");
            System.exit(1);
        }

         NetConnectClient.writeToSocket = null;
         NetConnectClient.readFromSocket = null;

        try
        {
            //create the read,write socket
            NetConnectClient.writeToSocket = new DataOutputStream(NetConnectClient.s.getOutputStream());


            NetConnectClient.readFromSocket = new DataInputStream(NetConnectClient.s.getInputStream());
        }
        catch(IOException io)
        {
            System.out.print("Cannot setup read write");
            System.exit(2);            
        }

        //variable to store user input
        NetConnectClient.d = new BufferedReader(new InputStreamReader(System.in));

        //variable to read whatever server has written
        NetConnectClient.inputBuffer = new byte[2048];
    }



}

1 个答案:

答案 0 :(得分:0)

在服务器端尝试Socket.getInetAddress(),它返回此套接字所连接的远程IP地址,