Java Socket聊天,一些消息未发送
我最近开始学习Sockets和其他类似的网络功能,我用GUI制作了一个简单的聊天程序,它在某种程度上起作用,问题是如果要将消息发送到服务器,客户端必须将同一消息发送两次,如果服务器发送消息,客户端必须回复一次 这是我的客户机/服务器类:Java Socket聊天,一些消息未发送,java,sockets,chat,Java,Sockets,Chat,我最近开始学习Sockets和其他类似的网络功能,我用GUI制作了一个简单的聊天程序,它在某种程度上起作用,问题是如果要将消息发送到服务器,客户端必须将同一消息发送两次,如果服务器发送消息,客户端必须回复一次 这是我的客户机/服务器类: package org.codingllamas.Chat; import java.io.*; import java.net.*; import javax.swing.text.BadLocationException; import javax.sw
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);
}
}
package org.codingllamas.Chat;
导入java.io.*;
导入java.net。*;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.html.html;
公开课理学士{
静态套接字clientSocket;
静态缓冲阅读器信息服务器;
静态DataOutputStream outToServer;
publicstaticvoidclientsetup(int-port,stringip)抛出IOException、BadLocationException{
字符串修饰符;
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“连接到”+ip+:“+port+”
”,0,0,HTML.Tag.B);
while(true){
///BufferedReader INFOROMUSER=新的BufferedReader(新的InputStreamReader(System.in));
clientSocket=新套接字(ip,端口);
outToServer=新的DataOutputStream(clientSocket.getOutputStream());
INFOROMSERVER=new BufferedReader(新的InputStreamReader(clientSocket.getInputStream());
ModifiedEntence=INFOROMSERVER.readLine();
//System.out.println(修改后的插入);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“服务器:”+ModifiedEntence+“
”,0,0,HTML.Tag.B);
//clientSocket.close();
}
}
公共静态void clientSend(字符串msg)引发IOException、BadLocationException{
outToServer.writeBytes(msg+“\r”);
outToServer.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“You:”+msg+“
”,0,0,HTML.Tag.B);
}
静态服务器插座;
静态插座连接插座;
静态缓冲阅读器;
静态数据输出流输出到客户端;
公共静态void服务器设置(int端口)引发异常{
字符串介词句;
welcomeSocket=新服务器套接字(端口);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“服务器在端口上启动:“+port+”
”,0,0,HTML.Tag.B);
while(true){
connectionSocket=welcomeSocket.accept();
INFOROMCLIENT=new BufferedReader(新的InputStreamReader(connectionSocket.getInputStream());
ClientSession=inFromClient.readLine();
outToClient=新的DataOutputStream(connectionSocket.getOutputStream());
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“Parther:”+INFOROMCLIENT.readLine()+“
”,0,0,HTML.Tag.B);
}
}
公共静态void serverSend(字符串msg)引发IOException、BadLocationException{
//DataOutputStream outToClient=新的DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(msg+“\r”);
outToClient.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),“You:”+msg+“
”,0,0,HTML.Tag.B);
}
}
还有一张照片:
提前谢谢
DataOutputStream
,请使用BufferedWriter。
ModifiedEntence?
创建新套接字:在客户端的生命周期中使用单个socket
,循环读取行,并在获取null时关闭套接字accept():
启动一个新线程,像客户机现在这样从客户机读取一行