所以我还是Java的网络新手,其中大部分都是令人困惑的,特别是Threaded Servers。我在两周前制作了一个简单的套接字聊天程序,但是我想扩展它,但在局域网上有多个客户端和自动服务器检测(通过广播或手动ping每个ip /端口)。但本周我主要关注多个客户端,即拥有多线程服务器。这是我到目前为止的代码,但它的错误,服务器只向第二个客户端发送/接收消息,第一个客户端只能在服务器忽略它之前发送一条消息。我的目标是让这个服务器支持大约10-20个客户端。
package org.codingllamas.Chat;
import java.awt.Toolkit;
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 BufferedWriter outToServer;
public static void clientSetup(int port,String ip) throws IOException, BadLocationException {
String incomingSentence;
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Connected to </b>" + ip + ":" + port + "<br>",0,0,HTML.Tag.B);
clientSocket = new Socket(ip,port);
while (true) {
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
incomingSentence = inFromServer.readLine();
Toolkit.getDefaultToolkit().beep();
Start.window.requestFocus();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server: </b>" + incomingSentence + "<br>",0,0,HTML.Tag.B);
}
}
public static void clientSend(String msg) throws IOException, BadLocationException{
outToServer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
outToServer.write(msg + "\r\n");
outToServer.flush();
outToServer.write(msg + "\r\n");
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 BufferedWriter outToClient;
public static void secondClient() throws IOException{
connectionSocket = welcomeSocket.accept();
}
public static void serverSetup(int port) throws Exception {
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);
connectionSocket = welcomeSocket.accept();
new Thread(new Runnable(){
public void run(){
try {
secondClient();
} catch (IOException e) {
System.out.println("Cannot send message");
e.printStackTrace();
}
}
}).start();
String clientSentence;
while(true){
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
Toolkit.getDefaultToolkit().beep();
Start.window.requestFocus();
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 {
outToClient = new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
outToClient.write(msg + "\r\n");
outToClient.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}
}
答案 0 :(得分:0)
由于您希望能够拥有多个客户端,因此您应该创建一个主要目的是等待和接受连接。
接下来你应该为客户端提供一个对象,这样他们就可以轻松地存储他们的套接字并放入一个数组中。在此客户端对象中,您可以创建用于发送和接收数据的线程,以及客户端单独的输入/输出流。
这将使您的程序可扩展为多个客户端,并使处理所有客户端变得更容易。