![](/img/trans.png)
[英]Java Multi threaded socket client/server: sending and receiving Enummap objects
[英]How do i know which client is sending the message in a multi threaded server
我正在創建一個客戶端服務器體系結構(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];
}
}
在服務器端嘗試Socket.getInetAddress(),該方法返回此套接字連接到的遠程IP地址,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.