📄 socketserver.java
字号:
/*
* SocketServer.java E.L. 2001-08-25
*/
import java.io.*;
import java.net.*;
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1250);
System.out.println("Log for the server side. Now we wait...");
Socket connection = server.accept(); // waits until someone contacts us
/* Opens communication streams to the client side program */
InputStreamReader readConnection
= new InputStreamReader(connection.getInputStream());
BufferedReader fromClientReader = new BufferedReader(readConnection);
PrintWriter toClientWriter = new PrintWriter(connection.getOutputStream(), true);
/* Sends introductory text to the client side program */
toClientWriter.println("***Hello, you are connected to the server!");
toClientWriter.println("***Write what you want, then I will repeat it. " +
"End with a carriage return.");
/* Receives data from the client side program */
String aLine = fromClientReader.readLine(); // receives one line with text
while (aLine != null) { // the client side program has shut down the connection
System.out.println("A client wrote: " + aLine);
toClientWriter.println("***You wrote: " + aLine); // sends answer to the client
aLine = fromClientReader.readLine();
}
connection.close(); // shuts down
} catch (Exception e) {
System.out.println("Error at the server side: " + e);
}
}
}
/* Example Run:
Output at the server side:
Log for the server side. Now we wait...
A client wrote: Hello, this is a test!
A client wrote: Yes, I did! It works!
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -