📄 complete16_4.java
字号:
package questions.c16;
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class Complete16_4 {
private static final int PORT = 8189;
private static boolean keepGoing = true;
public static class Server extends Thread {
public void run() {
try {
System.out.println( "Server starting..." );
ServerSocket ss = new ServerSocket( PORT );
Socket incoming = ss.accept();
InputStream is = incoming.getInputStream();
InputStreamReader isr = new InputStreamReader( is );
BufferedReader in = new BufferedReader( isr );
OutputStream os = incoming.getOutputStream();
PrintWriter out = new PrintWriter( os );
String str = in.readLine();
while( str != null ) {
System.out.println( "Received> " + str );
str = in.readLine();
}
incoming.close();
}
catch( IOException iox ) {
System.out.println( iox );
}
}
}
public static class Client extends Thread {
public void run() {
PrintWriter out = null;
try {
System.out.println( "Client starting..." );
InetAddress target = InetAddress.getLocalHost();
Socket client = new Socket( target, PORT );
OutputStream os = client.getOutputStream();
out = new PrintWriter( os );
int count = 1;
while ( keepGoing ) {
out.println( "Message " + count++ );
out.flush();
Thread.currentThread().sleep( 1000 );
}
} catch( Exception x ) {
System.out.println( x );
} finally {
if ( out != null ) {
out.close();
}
}
}
}
public static void requestStop() {
keepGoing = false;
}
public static void main( String[] args ) {
// Your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -