📄 commserver.java
字号:
//【代码18-1-5】
//CommServer.java
import java.net.*;
import java.io.*;
public class CommServer
{
public static void main(String[] args) throws IOException
{
//申请服务器ServerSocket
ServerSocket serverSocket = null;
try
{
//创建ServerSocket
serverSocket = new ServerSocket(1800);
}
catch (IOException e)
{
System.err.println("Could not listen on port.");
System.exit(1);
}
//申请用于连接的Socket
Socket clientSocket = null;
try
{
//获取用于连接的Socket
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
//获取输出流
PrintStream out = new PrintStream(
new BufferedOutputStream(
clientSocket.getOutputStream(), 1024), false);
//获取输入流
DataInputStream in = new DataInputStream(
new BufferedInputStream(
clientSocket.getInputStream()));
String inputLine, outputLine;
//设置信息交换协议
GreetingsProtocol greetings = new GreetingsProtocol();
//取得当前协议
outputLine = greetings.processInput(null);
out.println(outputLine);
out.flush();
//接收客户机的服务
while ((inputLine = in.readLine()) != null)
{
//处理客户机信息
outputLine = greetings.processInput(inputLine);
out.println(outputLine);
out.flush();
if (outputLine.equals("Bye."))
break;
}
//关闭输入输出流
out.close();
in.close();
//关闭套接字
clientSocket.close();
serverSocket.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -