📄 netipaddressdemo5.java
字号:
//Example 5 of Chapter 9
import java.net.*;
import java.io.*;
public class NetIpAddressDemo5 extends Thread
{
private static Socket socket;
public static void main( String[] args )
{
String hostname = "localhost";
int port = 65;
String s;
if( args.length > 1 )
{
hostname = args[0];
port = Integer.parseInt( args[1] );
}
try{
InetAddress hostaddress = InetAddress.getByName( hostname );
try{
socket = new Socket( hostaddress, port );
BufferedReader buf = new BufferedReader(
new InputStreamReader( socket.getInputStream( ) ) );
NetIpAddressDemo5 th = new NetIpAddressDemo5( );
th.start( );
while( ( s = buf.readLine( ) ) != null )System.out.println( s );
socket.close( );
}
catch( IOException e )
{
System.out.println( e.toString( ) );
}
}
catch( UnknownHostException e )
{
System.out.println( e.toString( ) );
}
}
public void run( )
{
String userInput;
BufferedReader buf;
PrintStream pstream;
try{
buf = new BufferedReader( new InputStreamReader( System.in ) );
pstream = new PrintStream( socket.getOutputStream( ) );
while( true )
{
if( socket.isClosed( ) ) break;
userInput = buf.readLine( );
pstream.println( userInput );
}
}
catch( IOException e )
{
System.out.println( e.toString( ) );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -