[英]Java Socket Chat, some messages not sending
I recently started learning out Sockets and other Networking Stuff like that works, I made a simple Chat program with a GUI, it works somewhat, the problem is that If order the message to send to the server the client must put the same message twice, and if the server sends a message, the client must reply once. 我最近开始学习类似的Sockets和其他Networking Stuff,我用GUI编写了一个简单的Chat程序,它有些起作用,问题是,如果要命令将消息发送到服务器,客户端必须将相同的消息两次发送,如果服务器发送消息,则客户端必须回复一次。
Heres my Client/Server Class: 这是我的客户端/服务器类:
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);
}
}
And a picture: 和图片:
Thanks in advance. 提前致谢。
DataOutputStream
, use a BufferedWriter.
DataOutputStream
,而要使用BufferedWriter.
modifiedSentence?
modifiedSentence?
创建新的套接字modifiedSentence?
: use a single Socket
for the life of the client, and loop reading lines, and close the socket when you get null. Socket
,并循环读取行,并在获取null时关闭套接字。 accept():
start a new thread to read lines like the client does now. accept():
从客户端读取一行:像客户端现在那样开始一个新线程来读取行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.