【问题标题】:How do i know which client is sending the message in a multi threaded server我如何知道哪个客户端在多线程服务器中发送消息
【发布时间】:2014-05-13 04:12:35
【问题描述】:

我正在创建一个客户端服务器架构 (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];
    }



}

【问题讨论】:

  • 好吧,我猜你可以尝试打印每个对象的“this”
  • @Leo 客户端正在向服务器传递一个字符串,你如何打印这个对象??
  • 我不确定这是否是您想要的,但是您可以使用类似的东西缓冲您的流并从两个不同的端点(一个到另一端,如预期的那样,另一个用于您的日志记录)使用它管道流(这不完全是一个示例,但我认为您可以理解 - cs.rtu.lv/PharePub/Java/Tutorial/essential/io/pipedstreams.html

标签: java multithreading client-server client


【解决方案1】:

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

【讨论】:

  • 我认为它不起作用,我的客户端和服务器都在本地主机上,我得到的结果是:127.0.0.1 for all clients
  • 那么唯一的出路是客户端应该在连接后发送它的ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 2015-01-05
  • 1970-01-01
相关资源
最近更新 更多