gpt4 book ai didi

java - 如何实现客户端到客户端的消息通过服务器传递(聊天)?

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:45 30 4
gpt4 key购买 nike

我的任务是通过服务器构建一个客户端到客户端的聊天程序。客户端 A 将向服务器发送一条消息,然后服务器将消息转发给客户端 B,反之亦然。所有这一切都将同时发生,直到其中一个将其关闭。我有以下程序。

服务器:

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

public class chatServer {

public static Socket s1 = new Socket();
public static Socket s2 = new Socket();
private final static int port1 = 5561;
private final static int port2 = 5562;

public static void main(String[] args) {


try{
ServerSocket se = new ServerSocket(port1);
ServerSocket sa = new ServerSocket(port2);
s1 = se.accept();
s2 = sa.accept();
}catch(IOException ioe) {
System.out.println(ioe);
}
System.out.println("##");
chatServer c = new chatServer();
chatServer.handler1 h1 = c.new handler1();
chatServer.handler2 h2 = c.new handler2();

h1.start();
h2.start();
}

public class handler1 extends Thread {

public void run() {


try{
InputStream in = s1.getInputStream();
System.out.println(s1.isConnected());
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(s2.getOutputStream(),true);
while(true) {
if(buff!=null) {
System.out.println(buff.readLine().toString());
pout.println(buff.readLine().toString());
}
}
}catch(IOException i){
System.out.println(i);
}

}
}

public class handler2 extends Thread {

public void run() {

try{
InputStream in = s2.getInputStream();
System.out.println(s2.isConnected());
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(s1.getOutputStream(),true);
while(true) {
if(buff!=null) {
pout.println(buff.readLine().toString());
System.out.println(buff.readLine().toString());
}
}
}catch(IOException i) {
System.out.println(i);
}

}
}
}

客户端1:

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



public class client1 {

private final static String IPAddress = "127.0.0.1";
private final static int port = 5561;


public static void main(String[] args) {
try{
Socket s = new Socket(IPAddress,port);
client2 c = new client2();
client2.listener l = c.new listener(s);
client2.publisher p = c.new publisher(s);
l.start();
p.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}

public class listener extends Thread {

Socket s1 = new Socket();
listener(Socket c){
s1=c;
}
public void run() {
try{
InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));

while(true) {

String l = b.readLine();
if(l!=null)
System.out.println(l);

}
}catch(IOException ioe) {
System.out.println(ioe);
}

}
}

public class publisher extends Thread {

Socket s2 = new Socket();
publisher(Socket c){
s2=c;
}
public void run() {
try {
InputStream in = s2.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s2.getOutputStream(),true);
while(true) {

String str = b.readLine();
if(str!=null)
out.println(str);

}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
}

客户端2:

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



public class client2 {

private final static String IPAddress = "127.0.0.1";
private final static int port = 5562;

public static void main(String[] args) {

try{
Socket s = new Socket(IPAddress,port);
client2 c = new client2();
client2.listener l = c.new listener(s);
client2.publisher p = c.new publisher(s);
l.start();
p.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}

public class listener extends Thread {

Socket s1 = new Socket();
listener(Socket c){
s1=c;
}
public void run() {
try{

InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));
PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
while(true) {
String l = b.readLine();
if(l!=null)
out.println(l);
}

}catch(IOException ioe) {
System.out.println(ioe);
}
}
}

public class publisher extends Thread {
Socket s1 = new Socket();
publisher(Socket c){
s1=c;
}
public void run() {

try {

InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));
BufferedReader b2 = new BufferedReader(new InputStreamReader(in));
while(true) {

String st = b2.readLine();
if(st!=null )
System.out.println(st);
}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
}

以上代码运行时没有任何编译错误。但他们没有传递消息。传递的消息显示在各自的客户端上。连接正常。但沟通并没有发生。请指导我完成这个任务。

最佳答案

每次调用 buff.readLine() 时,您实际上消耗流中的一行。下次调用它时,如果有的话,您将读取一个新行,否则如果底层流阻塞,调用可能会阻塞。

参见 BufferedReader 的 javaDocs .

因此,当您在循环中调用 readLine 两次时,只有当您的客户端发送至少两行时,您才会看到任何输出,并且转发到另一个客户端的行将是第一行,而第二行将打印到 System.out

关于java - 如何实现客户端到客户端的消息通过服务器传递(聊天)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38967217/

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