📄 simpleserver.java
字号:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
ServerSocket serverSkt=null;
Socket clientSkt=null;
BufferedReader in=null;
PrintStream out=null;
//构造方法
public SimpleServer(int port){
System.out.println("服务器代理正在监听,端口:"+port);
try{
serverSkt=new ServerSocket(port);
}catch(IOException e){
System.out.println("监听端口"+port+"失败!");
}
try{
clientSkt=serverSkt.accept();
}catch(IOException e){
System.out.println("连接失败");
}
try{
in=new BufferedReader(new InputStreamReader(clientSkt.getInputStream()));
out=new PrintStream(clientSkt.getOutputStream());
}catch(IOException e){
}
}
//收到客户端请求
public String getRequest(){
String frmClt=null;
try{
frmClt=in.readLine();
System.out.println("Server收到请求:"+frmClt);
}catch(Exception e){
System.out.println("无法读取端口.......");
System.exit(0);
}
return frmClt;
}
//发送响应给客户端
public void sendResponse(String response){
try{
out.println(response);
out.println();
System.out.println("Server响应请求 :"+response);
}catch(Exception ex){
System.out.println("写端口失败!。。。");
System.exit(0);
}
}
public static void main(String args[]) throws IOException{
SimpleServer sa=new SimpleServer(8888);
while(true){
sa.sendResponse(sa.getRequest());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -