📄 additionclient.java
字号:
package examples.network;
import java.io.*;
import java.net.*;
/** A client-side class that uses a TCP/IP socket
*/
public class AdditionClient {
private InetAddress host;
private int port;
// This is not a reserved port number
static final int DEFAULT_PORT = 8189;
/** Constructor
* @param host Internet address of the host
* where the server is located
* @param port Port number on the host where
* the server is listening
*/
public AdditionClient( InetAddress host, int port ) {
this.host = host;
this.port = port;
}
/** The method used to start a client object
*/
public void run() {
try {
Socket client = new Socket( host, port );
BufferedReader socketIn;
socketIn
= new BufferedReader(
new InputStreamReader(
client.getInputStream() ) );
PrintWriter socketOut
= new PrintWriter(
client.getOutputStream(), true );
String numbers = "1.2 3.4 5.6";
System.out.println( "Adding the numbers "
+ numbers
+ " together" );
socketOut.println( numbers );
System.out.println( socketIn.readLine() );
socketOut.println ( "" );
}
catch( IOException iox ) {
System.out.println( iox );
iox.printStackTrace();
}
}
/** The test method for the class
* @param args Optional port number
* and host name
*/
public static void main( String[] args ) {
try {
InetAddress host = InetAddress.getLocalHost();
int port = DEFAULT_PORT;
if ( args.length > 0 ) {
port = Integer.parseInt( args[0] );
}
if ( args.length > 1 ) {
host = InetAddress.getByName( args[1] );
}
AdditionClient addClient
= new AdditionClient( host, port );
addClient.run();
}
catch ( UnknownHostException uhx ) {
System.out.println( uhx );
uhx.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -