gpt4 book ai didi

java - 在 java 中聊天不同步(套接字,线程)

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

我试图找到一种方法,通过在两个客户端之间创建一个聊天线程,让一个服务器实例在它们之间进行协商。

我创建了这个项目,它“几乎”可以工作......但似乎存在同步问题的缓冲区。

当在一侧(即 Client#1)写一行时,它不会传递到另一侧(即 Client#2),但只有在 Client#2 也尝试传递一行之后。

我知道可能有更好的方法来实现它,但我想了解我的代码有什么问题。

你的帮助会很棒!

代码:

服务器

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

public class Server
{
public static void main(String[] args)
{
int id = 1;

System.out.println();
System.out.println("Server");

try
{
ServerSocket serverSocket = new ServerSocket(4321);

while (true)
{
Socket client1Socket = serverSocket.accept();
Socket client2Socket = serverSocket.accept();

System.out.println("clients connected from ports: \n"
+ client1Socket.getPort() + ", " + client2Socket.getPort());

Thread client1Thread = new ServerThread(client1Socket, client2Socket, id);
client1Thread.start();
id++;

Thread client2Thread = new ServerThread(client2Socket, client1Socket, id);
client2Thread.start();
id++;
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

服务器线程

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

public class ServerThread extends Thread
{
Socket sourceSocket;
Socket destSocket;
int id;

public ServerThread(Socket src, Socket dst, int n)
{
sourceSocket = src;
destSocket = dst;
id = n;
}

public void run()
{
try
{
Scanner clientInput = new Scanner(sourceSocket.getInputStream());
PrintStream destOutput = new PrintStream(destSocket.getOutputStream());
destOutput.println("You are chatting with Client " + id);

boolean more = true;
while (more)
{
String input = clientInput.nextLine();
destOutput.println(input);

if (input.equals("Q"))
{
more = false;
}
}
sourceSocket.close();
destSocket.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

客户端

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

public class Client
{
public static void main(String[] args)
{
System.out.println();
System.out.println("Client");

try
{
Socket clientSocket = new Socket("localhost", 4321);

System.out.println("Connection Established");
Scanner input = new Scanner(clientSocket.getInputStream());
PrintStream output = new PrintStream(clientSocket.getOutputStream());
Scanner in = new Scanner(System.in);

System.out.println(input.nextLine());

boolean more = true;

while (more)
{
String text = in.nextLine();
output.println(text);
String nextInput = input.nextLine();

if (nextInput == null)
{
more = false;
}
else
{
System.out.println(nextInput);
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

最佳答案

在您的客户端代码中,行 String text = in.nextLine(); 将阻塞您的线程。这意味着如果您从不在客户端中输入任何内容,您将无法收到任何内容。所以解决办法就是把你的消息接收代码放在另外一个线程中。比如:

    Thread thread = new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
while (!Thread.interrupted()) {
System.out.println(input.nextLine());
}
}

});
thread.start();

while (true)
{
String text = in.nextLine();
output.println(text);
// String nextInput = input.nextLine();
......................
}

关于java - 在 java 中聊天不同步(套接字,线程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060994/

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