客户端/服务器程序无法通过套接字连接
package server;
import java.net.*; // for network
import java.util.*; // for utilities
import java.io.*; // for streams
import server.ex12ClientThread;
public class ex12Server implements ex12Constants
{
public static void main(String args[])
{
int well_known_port = SERVERPORT; // default port value
ServerSocket serverSock = null;
ex12ClientThread thread = null;
try { // to get a port number
if ( args.length > 0 )
{
well_known_port = Integer.parseInt(args[0]);
//initialises port number to connect to
}
} catch (NumberFormatException e ) {} // do nothing accept default.
try
{
serverSock = new ServerSocket( well_known_port, 10 ) ;
// information to log file (ie screen)
System.out.println("ServerSocket " + serverSock.toString());
System.out.println("Entering server loop");
while( true ) // Main Server loop
{
Socket clientSocket = serverSock.accept();
thread = new ex12ClientThread(clientSocket);
thread.setDaemon(true);
thread.start();
}
} catch( Exception e )
{
System.err.println( "Socket Error!!!." ) ;
System.exit(1) ;
}
finally
{
try
{
serverSock.close();
}
catch (IOException e) {}
}
}
}
package server;
import java.net.*; // for network
import java.util.*; // for utilities
import java.io.*; // for streams
import server.ex12ClientThread;
public class ex12Server implements ex12Constants
{
public static void main(String args[])
{
int well_known_port = SERVERPORT; // default port value
ServerSocket serverSock = null;
ex12ClientThread thread = null;
try { // to get a port number
if ( args.length > 0 )
{
well_known_port = Integer.parseInt(args[0]);
//initialises port number to connect to
}
} catch (NumberFormatException e ) {} // do nothing accept default.
try
{
serverSock = new ServerSocket( well_known_port, 10 ) ;
// information to log file (ie screen)
System.out.println("ServerSocket " + serverSock.toString());
System.out.println("Entering server loop");
while( true ) // Main Server loop
{
Socket clientSocket = serverSock.accept();
thread = new ex12ClientThread(clientSocket);
thread.setDaemon(true);
thread.start();
}
} catch( Exception e )
{
System.err.println( "Socket Error!!!." ) ;
System.exit(1) ;
}
finally
{
try
{
serverSock.close();
}
catch (IOException e) {}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少在这一点上,如果您要对要丢弃的某些异常进行 printStackTrace ,这可能会有所帮助。我知道在我的个人开发盒上,我通常在 8080 这样的端口上运行至少 1 个应用程序服务器,并且我通常在端口 80 上运行 Apache 或 IIS,因此,如果该端口上已经绑定了其他内容,那么异常将被抛出并被第一个 catch 捕获(异常 e)。您可能也会在关闭执行之前退出应用程序,正如我认为的那样,Javadoc 说调用不会返回,所以虽然这可能不是问题,因为您要抛出线程,所以您可能会抛出自己的线程那里阻塞了其他连接,尽管我真的怀疑这一点,但这可能是“由实现者决定的行为”类型的事情之一。
It might help, at this point at least, if you were to printStackTrace on some of those exceptions you're discarding. I know on my personal development box I'm usually running at least 1 app server on a port like 8080, and I've got either Apache or IIS usually running on port 80, so if something else is already bound on the port so an exception will be thrown and caught by that first catch(exception e). You might be exiting the app before you close executes as well, as I think, the Javadoc says the call does not return, so while that probably isn't a problem, since you're tossing threads out you might have your own threads out there blocking other connections, though I really doubt that, it probably is one of those "behavior determined by the implementer" types of things.