简体   繁体   English

Java客户端服务器应用程序-地址已在使用中:connect

[英]Java Client Server Application - Address already in use: connect


I am currently working on a simple multiplayer game where serveral clients need to connect to a server. 我目前正在开发一个简单的多人游戏,其中服务器客户端需要连接到服务器。

My server consits of a single serverSocket. 我的服务器由单个serverSocket组成。 This serverSocket accepts incoming connections and hands them over to connection object that starts a separate thread. 此serverSocket接受传入的连接,并将它们移交给启动单独线程的连接对象。

ServerSocket seso = new ServerSocket(12345);

while(true){
    Socket toClient = seso.accept();                
    new Connection(toClient); //creates a thread that opens streams etc
}

Clients open a new Socket and connect to this server. 客户端打开一个新的套接字并连接到该服务器。

Socket toServer = new Socket();
toServer.setReuseAddress(true);
toServer.bind(new InetSocketAddress(65432)); //always using the same port   
toServer.connect(new InetSocketAddress(serverIP,12345));

Now if i close the connection to the server using toServer.close(); 现在,如果我使用toServer.close();关闭与服务器的连接toServer.close(); and try to connect again to the server, i get an "address already in use: connect" exception. 并尝试再次连接到服务器,我得到一个“地址已在使用中:连接”异常。

Using TCPView i can see that the state of the client procress changes to TIME_WAIT. 使用TCPView,我可以看到客户端的状态更改为TIME_WAIT。 But shouldn't i be able to use this port again because of setReuseAddress(true) ? 但是由于setReuseAddress(true)我不应该再次使用此端口吗? Am i using it wrong or is it an server problem? 我使用的是错误的还是服务器问题?

I do always call .close() on toClient and toServer. 我总是在toClient和toServer上调用.close()。 Nevertheless i always have to wait until the socket is completely closed (after TIME_WAIT) before this client can connect again to the server. 但是,我总是必须等到套接字完全关闭(在TIME_WAIT之后),此客户端才能再次连接到服务器。

When i close the entire application, the socket is immediately closed (not in state TIME_WAIT) and this client can connect to my server. 当我关闭整个应用程序时,套接字立即关闭(不在TIME_WAIT状态),此客户端可以连接到我的服务器。 (And ofc there is a connection reset exception in my server) (而且我的服务器中确实有一个连接重置异常)

How can I do that without always closing the application ? 在不总是关闭应用程序的情况下该怎么做?
Thanks for your help. 谢谢你的帮助。

To expand on my comment, a client / server protocol requires the server to listen on a port known to or discoverable by the client -- that can be considered the definition of "server" -- but it does not ordinarily require clients to connect from a specific port. 为了进一步说明我的观点,客户端/服务器协议要求服务器侦听客户端已知或可发现的端口(可以视为“服务器”的定义),但通常不要求客户端一个特定的端口。 If you do not bind the client socket to a particular port, then the underlying system will choose an available (source) port automatically and transparently. 如果未将客户端套接字绑定到特定端口,则底层系统将自动透明地选择可用的(源)端口。

If the server depends for some reason on clients connecting from a particular port, then you should re-evaluate that aspect of your design. 如果服务器由于某种原因依赖于从特定端口连接的客户端,那么您应该重新评估设计的这一方面。 If it does not, then you are making your own trouble by having clients connect that way. 如果不是这样,那么通过让客户端以这种方式进行连接就给自己制造了麻烦。 This should be all you need to do: 这应该是您需要做的所有事情:

Socket toServer = new Socket();
toServer.connect(new InetSocketAddress(serverIP, 12345));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中的服务器客户端聊天室程序显示地址已在使用中 - server client chatroom program in java shows Address already in use udp服务器上的Java中已经使用的地址错误 - address already in use error in java on udp server java.net.BindException:已使用的地址:JVM_Bind多客户机服务器 - java.net.BindException: Address already in use: JVM_Bind Multi Client Server 遇到java.net.BindException:服务器客户端套接字应用程序上已在使用的地址(绑定失败) - Running into an java.net.BindException: Address already in use (Bind failed) on server- client socket app Java线程绑定异常与已使用的地址结合使用错误(使用套接字的客户端服务器) - Java Thread Bind Exception combined with address already in use error (client server using sockets) 将MySQL连接到Java客户端/服务器应用程序 - Connect MySQL to a Java Client/Server Application Apache DefaultHttpClient - java.net.BindException:地址已在使用:连接 - Apache DefaultHttpClient - java.net.BindException: Address already in use: connect Webdriver:java.net.BindException:地址已在使用中:connect - Webdriver: java.net.BindException: Address already in use: connect 多线程服务器客户端地址已在使用中 - Multi-threaded Server Client Address already in use Java:地址已在使用中 - Java: Address already in use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM