使用套接字读取 .accept() 时,我的服务器停止工作
请原谅我的写作错误... 我正在使用 NetBeans 运行自制服务器和客户端,一切正常。正如我之前所说,我在客户端上使用“Socket”,在 sv 上使用“ServerSocket”。我还在服务器中使用 JDBC Mysql。 当我在其可分发文件夹中生成两个 java 文件并使用它们时,问题就开始了。客户端向服务器发送信息(以 .getOutputStream() 和 .getInputStream() 开头,然后是 .flush() ),但服务器没有收到任何消息。我试着看看它停在哪里,它在哪里
clients[i] = new Client (server.accept(), i);
当我尝试从 NetBeans 执行服务器并从桌面执行客户端时,疯狂的事情发生了......它有效!所以肯定是服务器的问题。我还使用一个开放的UDP端口,并且我正在寻找192.168.0.10(这是我的计算机,在LAN中)上的服务器的IP。
我希望有人能帮助我,提前致谢!
我在这里粘贴我的代码,很抱歉有些变量是西班牙语:
公共ServidorMultiCliente() { super("多客户端服务器"); initComponents();
<前><代码>尝试{ server = new ServerSocket( 500, maxNumberClients); }catch(IOException异常){ 异常.printStackTrace(); 系统.退出(1); } serverWriteBox.append("服务器启动,等待连接..."); }
我从服务器运行这些:
公共无效ejecutar(){ clientAcceptor.start(); messagesListener.start(); }
clientsAcceptor是:
私有类 aceptadorClientes 扩展了 Thread{
public void run(){ for( int i = 1; i < maxNumberClients; i++ ){ 尝试{ clientes[i] = new Cliente (servidor.accept(), i); // **到这里就停止了** // 它永远不会到达这里...(不使用 NetBeans) clientes[i].start(); clientes[i].aceptado = true; }catch(IOException异常){ 异常.printStackTrace(); } }
这就是我在不同线程中接受客户端的方式。我用 messageListener 做了同样的事情,它是每个新客户端的新线程。它处于循环状态,始终在监听。在这里,我粘贴了可执行的 Client,它与我在 ServidorMultiCliente 中使用的 Cliente 类不同:
公共客户(){ }
公共客户端(字符串主机){ this.host = 主机; this.executeConnection(); } 公共无效执行连接(){ int 连接 = 0; 尝试 { cliente = new Socket(InetAddress.getByName(主机), 500); 连接= 1; } catch (IOException ex) { 连接= 0; this.ejecutarConexion(); } 如果(连接== 1){ 获取流量(); } }
私有无效obtainFlows(){
<前><代码>尝试{ 输出=新的ObjectOutputStream(cliente.getOutputStream()); 输出.flush(); // 这里我应该“连接” 输入 = new ObjectInputStream(cliente.getInputStream()); } catch(IOException 前){ this.initiateDisconnection(); } sendFlows("我已连接!"); 新的 messageListener().start(); // 这是一个线程 }
please excuse my writing errors...
I'm using NetBeans to run a homemade server and a client, and it all works fine. As I said before, I'm using "Socket" on my client, and "ServerSocket" on my sv. I'm also using JDBC Mysql in the server.
The problem starts when I generate both java files in their distributable folders and use them. The client sends information to the server (it starts with .getOutputStream() and .getInputStream(), then .flush() ), but the server doesn't receive any message. I tried seeing where it stops and it's in
clients[i] = new Client (server.accept(), i);
The crazy thing happens when I try executing my server from NetBeans and the client from my desktop... It works! So the server must be the problem. I'm also using an opened UDP port, and i'm looking for the IP of the server on 192.168.0.10 (which is my computer, in LAN).
I hope someone can help me, thanks in advance!
Here I paste my code, i'm sorry some variables are in spanish:
public ServidorMultiCliente() {
super("Servidor MultiCliente");
initComponents();try{ servidor = new ServerSocket( 500, maxNumberClients); }catch(IOException excepcion){ excepcion.printStackTrace(); System.exit(1); } serverWriteBox.append("Server iniciated, waiting for connections..."); }
I run these, from the Server:
public void ejecutar(){
clientsAcceptor.start();
messagesListener.start(); }
Where clientsAcceptor is:
private class aceptadorClientes extends Thread{
public void run(){ for( int i = 1; i < maxNumberClients; i++ ){ try{ clientes[i] = new Cliente (servidor.accept(), i); // **Here it stops** // It never reaches here... (without using NetBeans) clientes[i].start(); clientes[i].aceptado = true; }catch(IOException excepcion){ excepcion.printStackTrace(); } }
That's how I accept clients in different threads. I make the same thing with messageListener, which is a new thread for every new client. It's in a loop, always listening. And here I paste my executable Client, which is different from the Cliente class I was using in ServidorMultiCliente:
public Cliente(){
}public Cliente(String host){ this.host = host; this.executeConnection(); } public void executeConnection(){ int connect = 0; try { cliente = new Socket(InetAddress.getByName(host), 500); conectar = 1; } catch (IOException ex) { conectar = 0; this.ejecutarConexion(); } if(conectar == 1){ obtainFlows(); } }
private void obtainFlows(){
try{ output= new ObjectOutputStream( cliente.getOutputStream()); output.flush(); // Here I should be "connected" input = new ObjectInputStream(cliente.getInputStream()); } catch(IOException ex){ this.initiateDisconnection(); } sendFlows("I'm connected!"); new messageListener().start(); // This is a thread }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ServerSocket#accept
是一个阻塞调用。它监听端口并在客户端连接时返回Socket
。您没有显示太多服务器逻辑,但似乎您将客户端放在一个数组中,因此您显然希望支持多个客户端。您不会显示您的Client
类是否启动线程并立即返回。您应该有一个服务器循环,它仅监听服务器套接字并在检索客户端套接字后创建客户端。即使您在
Client
构造函数中执行此操作(如果没有代码我也无法判断),这也不是一个很好的地方,并且会严重阻碍调试。如果您不为客户端启动线程,这将解释服务器“停止”(如果“停止”意味着“阻塞”而不是“崩溃”)。请参阅 Java 教程中的“编写套接字的服务器端” 详细解释。
我不明白为什么它在通过 Netbeans 启动时表现不同。需要更多的代码上下文。
ServerSocket#accept
is a blocking call. It listens to a port and returns aSocket
when a client connects. You don't show very much of your server logic but it seems you put clients in an array so you obviously want to support more than one client. You don't show if yourClient
class starts a thread and returns immediatly.You should have a server loop that just listens to a server socket and creates clients after it retrieved a client socket. Even if you do this in your
Client
constructor (I can't tell without the code) it is not a very good place for this and seriously hinders debugging.If you don't start threads for your clients this would explain a server that "stops" (if "stops" means "blocks" and not "crashes"). See "Writing the Server Side of a Socket" in the Java Tutorial for a detailed explanation.
I can't think of why it behaves different when started via Netbeans. A little bit more of code context is needed.