📄 additionserver.java
字号:
package examples.network;
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
/** An example class that uses the server socket class
*/
public class AdditionServer {
private int port;
// This is not a reserved port number
static final int DEFAULT_PORT = 8189;
/** Constructor
* @param port The port where the server
* will listen for requests
*/
AdditionServer( int port ) {
this.port = port;
}
/** The method that does the work for the class */
public void run() {
try {
ServerSocket ss = new ServerSocket( port );
Socket incoming = ss.accept();
BufferedReader in;
in = new BufferedReader(
new InputStreamReader(
incoming.getInputStream() ) );
PrintWriter out
= new PrintWriter(
incoming.getOutputStream(), true );
String str;
while ( !(str = in.readLine()).equals("") ) {
double result = 0;
StringTokenizer st
= new StringTokenizer( str );
try {
while( st.hasMoreTokens() ) {
Double d = new Double( st.nextToken() );
result += d.doubleValue();
}
out.println( "The result is " + result );
}
catch( NumberFormatException nfe ) {
out.println( "Sorry, your list "
+ "contains an "
+ "invalid number" );
}
}
incoming.close();
}
catch( IOException iox ) {
System.out.println( iox );
iox.printStackTrace();
}
}
/** The test method for the class
* @param args[0] Optional port number in place of
* the default
*/
public static void main( String[] args ) {
int port = DEFAULT_PORT;
if (args.length > 0 ) {
port = Integer.parseInt( args[0] );
}
AdditionServer addServe
= new AdditionServer( port );
addServe.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -