gpt4 book ai didi

Java Socket Chat,某些消息未发送

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:48 24 4
gpt4 key购买 nike

我最近开始学习套接字和其他类似的网络东西,我用GUI制作了一个简单的聊天程序,它有点工作,问题是如果命令消息发送到服务器,客户端必须将相同的消息发送两次,如果服务器发送消息,客户端必须回复一次。

这是我的客户端/服务器类:

package org.codingllamas.Chat;

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

import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;

public class SC {

static Socket clientSocket;
static BufferedReader inFromServer;
static DataOutputStream outToServer;

public static void clientSetup(int port,String ip) throws IOException, BadLocationException {
String modifiedSentence;
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Connected to </b>" + ip + ":" + port + "<br>",0,0,HTML.Tag.B);
while (true) {
///BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
clientSocket = new Socket(ip,port);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
modifiedSentence = inFromServer.readLine();
//System.out.println(modifiedSentence);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server: </b>" + modifiedSentence + "<br>",0,0,HTML.Tag.B);
//clientSocket.close();
}
}

public static void clientSend(String msg) throws IOException, BadLocationException{
outToServer.writeBytes(msg + "\r");
outToServer.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}

static ServerSocket welcomeSocket;
static Socket connectionSocket;
static BufferedReader inFromClient;
static DataOutputStream outToClient;

public static void serverSetup(int port) throws Exception {
String clientSentence;
welcomeSocket = new ServerSocket(port);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server Started on port: " + port + "</b><br>",0,0,HTML.Tag.B);
while(true){
connectionSocket = welcomeSocket.accept();
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
outToClient = new DataOutputStream(connectionSocket.getOutputStream());

Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Parther: </b>" + inFromClient.readLine() + "<br>",0,0,HTML.Tag.B);
}
}

public static void serverSend(String msg) throws IOException, BadLocationException {
//DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(msg + "\r");
outToClient.flush();

Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}
}

还有一张图片:

enter image description here

提前致谢。

最佳答案

  1. 不要使用DataOutputStream,使用BufferedWriter。
  2. 不要为每个 modifiedSentence 创建一个新的套接字?:在客户端的生命周期中使用单个 Socket,并循环读取行,并在获取 null 时关闭套接字。
  3. 每次 accept() 时不要从客户端读取一行: 启动一个新线程来像客户端现在那样读取行。

关于Java Socket Chat,某些消息未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623929/

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