corejava第10版
This commit is contained in:
1 parent
2021c3c227
commit
6165543dd3
1088 files changed
+94411
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package server;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* This program implements a simple server that listens to port 8189 and echoes back all client
|
||||
* input.
|
||||
* @version 1.21 2012-05-19
|
||||
* @author Cay Horstmann
|
||||
*/
|
||||
public class EchoServer
|
||||
{
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
// establish server socket
|
||||
try (ServerSocket s = new ServerSocket(8189))
|
||||
{
|
||||
// wait for client connection
|
||||
try (Socket incoming = s.accept())
|
||||
{
|
||||
InputStream inStream = incoming.getInputStream();
|
||||
OutputStream outStream = incoming.getOutputStream();
|
||||
|
||||
try (Scanner in = new Scanner(inStream, "UTF-8"))
|
||||
{
|
||||
PrintWriter out = new PrintWriter(
|
||||
new OutputStreamWriter(outStream, "UTF-8"),
|
||||
true /* autoFlush */);
|
||||
|
||||
out.println("Hello! Enter BYE to exit.");
|
||||
|
||||
// echo client input
|
||||
boolean done = false;
|
||||
while (!done && in.hasNextLine())
|
||||
{
|
||||
String line = in.nextLine();
|
||||
out.println("Echo: " + line);
|
||||
if (line.trim().equals("BYE")) done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user