comserver.java
来自「一个简单而精彩的java聊天室小程序」· Java 代码 · 共 68 行
JAVA
68 行
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 + =
减小字号Ctrl + -
显示快捷键?