📄 tcpserver.java
字号:
package tcp;
import java.io.*;
import java.net.*;
/**
* tcp socket接收
* @author Administrator
*
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket svrsoc = null;
Socket soc = null;
DataInputStream in = null;
try {
svrsoc = new ServerSocket(50000);
//会阻塞在这,等待接收
soc = svrsoc.accept();
in = new DataInputStream(soc.getInputStream());
String str = in.readLine();
while (str != null) {
System.out.println("receive str: " + str);
str = in.readLine();
}
//收到信息后,给与回应
PrintStream out = new PrintStream(soc.getOutputStream());
out.println("server:收到!");
out.close();
} catch (Exception e) {
System.out.println("error:" + e);
} finally {
in.close();
soc.close();
svrsoc.close();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -