gpt4 book ai didi

java - 多线程服务器仅从一个客户端 JAVA 接收数据

转载 作者:行者123 更新时间:2023-11-30 10:49:34 25 4
gpt4 key购买 nike

我已经创建了一个简单的多线程服务器。每次它接受客户端时,都会创建一个 DataInputStream 和 DataOutputStream 并开始通信

服务器:

public class Connection implements Runnable{
boolean isAlreadyOpened = false;

@Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(7000);
while(true){
System.out.println("Il Server sta cercando Connessioni");
Socket s = ss.accept();
System.out.println("Il Server ha accettato un Client");

Thread t2 = new Thread(new Runnable(){
public void run(){
try {

DataInputStream dis = new DataInputStream(s.getInputStream());
isAlreadyOpened = true;
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while(true){
String test = dis.readUTF();
dos.writeUTF(test);
System.out.println(test);
dos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
isAlreadyOpened = false;
}
}
});
t2.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

我已经尝试连接一个 Android 应用程序和一个 JavaApp;每 40 秒这两个程序都会通过 writeUTF 发送一个字符串。两个客户端都正确建立了连接,但服务器只从最后一个连接到服务器的客户端接收数据。我怎样才能让服务器从/向所有客户端接收/发送数据?

编辑:

我试过这样设置:

public class Connection implements Runnable {

@Override
public void run() {

try {
ServerSocket ss = new ServerSocket(7000);
while (true) {
System.out.println("Server is listening");
Socket s = ss.accept();
System.out.println("Client Accepted");

Thread t2 = new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true) {
String test = dis.readUTF();
dos.writeUTF(test);
System.out.println(test);
dos.flush();
}
} catch (IOException
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
t2.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

但是结果是一样的

编辑:

Andorid 客户端代码

public class Connection implements Runnable {
@Override
public void run() {
try {
Socket s = new Socket("127.0.0.1", 7000);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());

while(true){
dos.writeUTF("FromAndroid");
Log.d("InputStreammmm", dis.readUTF());
Thread.sleep(10000)
}

} catch (IOException e) {
e.printStackTrace();
}

}

JAVA 应用客户端代码

@Override
public void run() {
try {

Socket socket = new Socket("127.0.0.1", 7000);
System.out.println("Connessooo");
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

while(true){
dos.writeUTF("Invioooooooooooooooooooooooooo");
result = dis.readUTF();
System.out.println(result);
Thread.sleep(10000);
}

} catch (IOException e) {

e.printStackTrace();
} catch (InterruptedException e) {

e.printStackTrace();
}
}

使用套接字列表进行编辑:

public class Connection implements Runnable {
List<Socket> sList = new ArrayList<>();
Socket s;
int i = 0;

@Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(7000);
while (true) {
System.out.println("Server Listening");
s = ss.accept();
sList.add(s);

System.out.println("Accepted Client --- " +s.toString());
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());

while (true)
{
String test = dis.readUTF();
System.out.println("Message sent from -- " + sList.get(i).toString());
System.out.println(test);

while(i < sList.size()){
DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
dos.writeUTF(test);
System.out.println("Message Sent to -- " + sList.get(i).toString());
dos.flush();
++i;
}
i=0;
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
System.out.println("Closing Socket --- " + sList.get(i).toString());
sList.get(i).close();
sList.remove(i);

}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
t2.start();
}
} catch (IOException e) {
e.printStackTrace();
}

}

按照 EJP 的建议,以这种方式解决了我的问题...

最佳答案

无法复制。

测试程序,从您编辑的代码中复制粘贴并添加 finalSocket s 因为我使用的是 Java 7,并添加了客户端代码:

public class Connection implements Runnable
{

@Override
public void run()
{

try
{
ServerSocket ss = new ServerSocket(7000);
while (true)
{
System.out.println("Server is listening");
final Socket s = ss.accept();
System.out.println("Client Accepted");

Thread t2 = new Thread(new Runnable()
{
public void run()
{
try
{
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true)
{
String test = dis.readUTF();
dos.writeUTF(test);
System.out.println(test);
dos.flush();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
s.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
t2.start();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void main(String[] args) throws InterruptedException
{
Connection c = new Connection();
Thread t1 = new Thread(c);
t1.setDaemon(true);
t1.start();
Runnable r = new Runnable()
{
public void run()
{
try (Socket s = new Socket("localhost", 7000))
{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
for (int i = 0; i < 10; i++)
{
dos.writeUTF("Hello from "+Thread.currentThread().getName());
String reply = dis.readUTF();
Thread.sleep(10*1000);
}
}
catch (IOException|InterruptedException exc)
{
exc.printStackTrace();
}
}
};
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t2.start();
t3.start();
t2.join();
t3.join();
}
}

输出:

Server is listening
Client Accepted
Server is listening
Client Accepted
Server is listening
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1

关于java - 多线程服务器仅从一个客户端 JAVA 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350949/

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