62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* connect to server
* @param the hostname, the server's listening port
* */
private boolean connectTo(String hostName, int port) throws IOException
{
boolean result = false;
//displayMessage( "Attempting connection" );
//create socket to make connection to server
applet = new Socket();
try
{
//InetSocketAddress is the subclass of abstruct class SocketAddress
InetSocketAddress inetSocketAddress = new InetSocketAddress(
InetAddress.getByName( hostName ), port );
//if > 60 seconds, then close
applet.connect( inetSocketAddress, 60000 );
//display connection information
//displayMessage( "Connect to: " + applet.getInetAddress().getHostName() );
result = true;
}
catch(SocketTimeoutException e)
{
System.out.println( "Cannot connect ..." );
}
catch(Exception e)
{
System.out.println( e );
}
return result;
}