📄 comserver.java
字号:
package server;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class comServer {
public ServerSocket svrSkt = null;
public Socket cltSkt = null;
public DataInputStream input = null;
public PrintStream output = null;
public comServer(int port) {
System.out.println("listening, port: " + port);
try {
svrSkt = new ServerSocket(port);
}catch(IOException e) {
System.out.println("listen port " + port);
}
try {
cltSkt = svrSkt.accept();
}catch(IOException e) {
System.out.println("connect faild");
}
try {
input = new DataInputStream(cltSkt.getInputStream());
output = new PrintStream(cltSkt.getOutputStream());
}catch(IOException e) {
}
output.println("Welcome......");
}
@SuppressWarnings("deprecation")
public String getRequest() {
String frmClt = null;
try {
frmClt = input.readLine();
}catch(Exception e) {
System.out.println("can't readed from port...");
System.exit(0);
}
return frmClt;
}
public void sendResponse(String response) {
try {
output.println(response);
}catch(Exception e) {
System.out.println("write to port faild...");
System.exit(0);
}
}
public static void main(String[] args) throws IOException {
comServer sa = new comServer(1001);
while (true) {
sa.sendResponse(sa.getRequest());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -