gpt4 book ai didi

java - Socket服务器构建问题

转载 作者:行者123 更新时间:2023-12-01 15:32:04 24 4
gpt4 key购买 nike

步骤 3 中列出的套接字服务器 http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html干净地构建(java版本“1.7.0_02”)并且运行时没有错误,但它退出时没有错误,而不是等待接受客户端。

更新了 ChatServer,缺少参数代码:

聊天服务器:

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

public class ChatServer implements Runnable
{ private ServerSocket server = null;
private Thread thread = null;
private ChatServerThread client = null;

public ChatServer(int port)
{ try
{ System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
start();
}
catch(IOException ioe)
{ System.out.println(ioe); }
}
public void run()
{ while (thread != null)
{ try
{ System.out.println("Waiting for a client ...");
addThread(server.accept());
}
catch(IOException ie)
{ System.out.println("Acceptance Error: " + ie); }
}
}
public void addThread(Socket socket)
{ System.out.println("Client accepted: " + socket);
client = new ChatServerThread(this, socket);
try
{ client.open();
client.start();
}
catch(IOException ioe)
{ System.out.println("Error opening thread: " + ioe); }
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void stop() { /* no change */ }
public static void main(String args[]) {
ChatServer server = null;
if (args.length != 1)
System.out.println("Usage: java ChatServer port");
else
server = new ChatServer(Integer.parseInt(args[0]));
}
}

聊天服务器线程:

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

public class ChatServerThread extends Thread
{ private Socket socket = null;
private ChatServer server = null;
private int ID = -1;
private DataInputStream streamIn = null;

public ChatServerThread(ChatServer _server, Socket _socket)
{ server = _server; socket = _socket; ID = socket.getPort();
}
public void run()
{ System.out.println("Server Thread " + ID + " running.");
while (true)
{ try
{ System.out.println(streamIn.readUTF());
}
catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
public void open() throws IOException
{ streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
}
}

最佳答案

编辑:用可行的解决方案更新我的答案。

在您的 ChatServer 类中更改这些方法,以便像这样

public void start() {
thread = new Thread(this);
thread.start();
}

public void stop() {
// You should implement this too
}

public static void main(String args[]) {
// Instantiate a CharServer with the listening port 9191
ChatServer chatServer = new ChatServer(9191);
// CharServer.start() should not be confused with Thread.start();
// This calls our custom method up above, which includes a call to
// Thread(ChatServer).start();
chatServer.start();

}

其中9191是我编造的端口号。

执行 CharServer#main 方法会产生以下输出并保持 Activity 状态

Binding to port 9191, please wait  ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=9191]
Waiting for a client ...
Waiting for a client ...

出于功能考虑,您还应该实现 stop() 方法。

关于java - Socket服务器构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450510/

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