gpt4 book ai didi

java 服务器仅从 1 个客户端接收数据,而不是从第 2 个客户端接收数据

转载 作者:行者123 更新时间:2023-12-01 10:54:36 25 4
gpt4 key购买 nike

美好的一天,我正在尝试创建一个客户端/服务器聊天,第一个客户端将消息发送到服务器,服务器将其发送到客户端 2,反之亦然,但服务器仅从第一个客户端接收消息,而不是从第一个客户端接收消息。从2号开始。另外,我如何从客户端 1 向 2 发送消息,反之亦然

package s;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client
{

public static void main(String[] args) throws IOException, Throwable
{
Socket s;
BufferedReader in;
PrintWriter out;
Scanner sc = new Scanner(System.in);
s = new Socket(InetAddress.getLocalHost(),9000);
System.out.println("Connection pending");
in = new BufferedReader (new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
String msg = in.readLine();
System.out.println(msg);
while(msg!="")
{
msg = sc.nextLine();
out.println(msg+"\n");
out.flush();
}
s.close();
sc.close();
}
}

================================================

package s;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TClient extends Thread
{
private int num;
private Socket s;
private BufferedReader in;
private PrintWriter out;

public Socket getS()
{
return s;
}

public BufferedReader getIn()
{
return in;
}

public PrintWriter getOut()
{
return out;
}

public TClient(Socket s,int num) throws IOException
{
this.s = s;
this.setNum(num);
System.out.println("Client"+num);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader (new InputStreamReader(s.getInputStream()));
out.println("Connected"+num+"\n");
out.flush();
}

public void run()
{
while(true)
{

try
{
String msg="";
msg = in.readLine();
System.out.println(msg);
if (msg.equals(".")) break;
}
catch (IOException e)
{

}
}
try
{
s.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public int getNum()
{
return num;
}

public void setNum(int num)
{
this.num = num;
}
}

================================================== =========

package s;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
public static void main(String[] args)
{
ServerSocket ss ;
Socket s = null ;
int nb_clients = 0;
String msg = "";
TClient[] connexions = new TClient[2];
try
{
ss = new ServerSocket(9000);
System.out.println("Server is listening in:"+ss.getLocalPort());
boolean continu = false;
while(!continu)
{
s = ss.accept();
connexions[nb_clients] = new TClient(s,nb_clients+1);
connexions[nb_clients].start();
nb_clients++;
if (nb_clients>=2) continu=true;
}
System.out.println("Clients connected");
s.close();
ss.close();
}
catch(IOException e)
{

}
}

}

==========================================每个终端的输出是:

Server is listening in:9000
Client1
Client2
Clients connected

两个客户端的输出是:

Connection pending

Connected1

Connection pending

Connected2

如果我从 1 和 2 向服务器写入一条消息,服务器输出将如下所示:

Server is listening in:9000

Client1

Client2

Clients connected

111111111111111111111111111111111111

================================================== =================更新:

我更改了服务器中的条件如果 (nb_clients>2) 继续=true;现在我可以从两个客户那里收到信息,现在我必须知道如何让他们在客户之间进行通信

最佳答案

针对两个客户端的解决方案。将消息从一个客户端发送到另一客户端。

服务器必须整理客户端之间的消息。

public class Server
{
static TClient[] connexions = new TClient[2];

public static void send(int clientNum, String message) {
TClient t = connexions[clientNum];
if(t != null) {
t.getOut().println(message);
t.getOut().flush();
}
}

}

在 TClient 中添加:

public void sendMessage(String message) {
int client = (getNum()-1)==1 ? 0:1;
Server.send(client, message);
System.out.printf("Sending message(%s) to client:%d from client:%d%n",message,client,getNum());
}

我在 while 循环内部添加了一个调用。

在客户端中,我使用另一个线程从服务器接收消息,称为 ReceiveMessageThread:

public class Client
{
public static void main(String[] args) throws IOException, Throwable
{

Socket s;
BufferedReader in;
PrintWriter out;
Scanner sc = new Scanner(System.in);
s = new Socket(InetAddress.getLocalHost(), 9000);
System.out.println("Connection pending");
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());

ReceiveMessageThread thread = new ReceiveMessageThread(in);
String msg = in.readLine();
System.out.println(msg);

thread.start();
while (msg != "")
{
msg = sc.nextLine();
out.println(msg + "\n");
out.flush();

}
s.close();
sc.close();
}

public static class ReceiveMessageThread extends Thread
{
private BufferedReader in;

public ReceiveMessageThread(BufferedReader in)
{
this.in = in;
}

public void run()
{
while (true)
{
try
{
String message = readMessage(in);
if (message != null && !message.equals(""))
System.out.println(message);
} catch (IOException ie)
{
ie.printStackTrace();
}
}
}

public String readMessage(BufferedReader in) throws IOException
{
String msgreceived = "";
String readValue = "";
while (in.ready())
{
readValue = in.readLine();
if (readValue != null)
msgreceived += readValue;
}
return msgreceived;
}
}


}

关于java 服务器仅从 1 个客户端接收数据,而不是从第 2 个客户端接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33690022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com