构造器做的下一件事是初始化至服务器的连接,如下所示:
// Connect to the server
try { // Initiate the connection
socket = new Socket( host, port ); // We got a connection! Tell the world
System.out.println( "connected to "+socket ); // Let's grab the streams and create DataInput/Output streams
// from them
din = new DataInputStream( socket.getInputStream() );
dout = new DataOutputStream( socket.getOutputStream() ); // Start a background thread for receiving messages
new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
}请注意,我们创建了一个单独的线程来处理进入的消息。我们将在下一章这样做。
----------------------------------------
以上是client的部分代码,client implement runable
我想知道客户端使用线程有什么用处?我觉得不用线程也应该可以的,毕竟它只负责一个连接!
请高手帮忙分析一下!使用的好处在哪里!谢谢
// Connect to the server
try { // Initiate the connection
socket = new Socket( host, port ); // We got a connection! Tell the world
System.out.println( "connected to "+socket ); // Let's grab the streams and create DataInput/Output streams
// from them
din = new DataInputStream( socket.getInputStream() );
dout = new DataOutputStream( socket.getOutputStream() ); // Start a background thread for receiving messages
new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
}请注意,我们创建了一个单独的线程来处理进入的消息。我们将在下一章这样做。
----------------------------------------
以上是client的部分代码,client implement runable
我想知道客户端使用线程有什么用处?我觉得不用线程也应该可以的,毕竟它只负责一个连接!
请高手帮忙分析一下!使用的好处在哪里!谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货