我刚刚开始使用套接字,对于我当前的项目,我需要能够从客户端控制我的程序,但是如果我的项目合作伙伴想要同时使用他的客户端,服务器就不会发送他是"你是连接的"消息,如连接类中所示。所以我假设服务器不能同时接受多个客户端。我已经尝试过使用Connection类的Thread,但是它也没有发送消息"你已连接"到第二个客户。我在这做错了什么?
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Listener extends Thread{
private ServerSocket server;
private int PORT;
public boolean running;
public Listener(int port){
try{
this.PORT = port;
this.server = new ServerSocket(PORT,10);
}catch (IOException e) {
System.out.println("Could not create serverSocket...");
}
}
@Override
public void run() {
this.running = true;
try{
waitForConnection();
}catch(IOException e){
System.out.println("Could not accept connection request..");
run();
}
}
public void dispose(){
try{
System.out.println("DISPOSE");
running = false;
server.close();
} catch (IOException i) {
System.out.println("Could not close ServerSocket");
}
}
private void waitForConnection() throws IOException{
while(running){
System.out.println("Waiting for connection");
Socket client = server.accept();
Runnable connection = new Connection(client);
new Thread(connection).start();
}
}
}
这是我用来让多个用户同时连接的线程:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Connection extends Thread {
Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
private boolean running;
public Connection(Socket connect){
this.connection = connect;
try {
setupStreams();
whileListening();
} catch (IOException e) {
System.out.println("could not connect to: "+ connection.getInetAddress().getHostName());
}
}
public void dispose(){
try{
output.close();
input.close();
connection.close();
running = false;
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void whileListening(){
String message = "You are connected! ";
sendMessage(message);
do{
try{
message = (String) input.readObject();
checkMessage(message);
}catch(ClassNotFoundException classNotFoundException){
sendMessage("tf did you send? ");
}catch (IOException e) {
dispose();
run();
}
}while(!message.equals("Client - END") && running == true);
}
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
private void sendMessage(String message){
try {
output.writeObject("Server - " + message+"\n");
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void checkMessage(String text){
//check the message
}
}
编辑:添加信息
在第一个客户端连接之前,服务器控制台说"等待连接",然后当第一个客户端连接时,客户端控制台说"您已连接"并且,当第二个客户端连接时,控制台是黑色的,当我关闭第一个客户端时,第二个客户端控制台说"你已连接"并且服务器控制台说"等待连接",然后如果我关闭第二个客户端,服务器控制台说"等待连接"试。
答案 0 :(得分:2)
在你public class Connection extends Thread
的构造函数中执行此whileListening()
内容,因此构造函数永远不会结束,您需要覆盖run()
函数并在那里执行
@Override
public void run() {
while(true) {
try {
whileListening();
} catch(Exception e) {}
}
}
像这样,它应该可以解决问题。
答案 1 :(得分:1)
我认为你必须首先接受,之后你就开始了。
例如,让我们假设像这样的东西
以下是来自SSLServerSocket的示例,它几乎是相同的逻辑(将其视为伪代码)
SSLContext sc = SSLContext.getInstance("TLS");
(...)
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(portNumber);
while (listening) {
SSLSocket c = (SSLSocket) s.accept();
log.info("Serving");
new SimpleSSLServerSocketThread(c).start();
}