📄 tcpserver.java
字号:
package com.demo.struts.else_test;
//服务器端源程序
import java.io.*;
import java.net.*;
public class TcpServer
{
@SuppressWarnings("deprecation")
public static void main(String args[]) throws IOException{
ServerSocket svrsoc = null;
Socket soc = null;
InputStream is = null;
OutputStream os = null;
//DataInputStream 为InputStream的子类
DataInputStream in = null;
//PrintStream为OutputStream的子类
PrintStream out = null;
try{
svrsoc = new ServerSocket(8000);
soc = svrsoc.accept();
is = soc.getInputStream();
in = new DataInputStream(is);
os = soc.getOutputStream();
out = new PrintStream(os);
InetAddress clientIP = soc.getInetAddress();
System.out.println("Client`s IP address:" + clientIP);
int port ;
port = soc.getPort();
System.out.println("Client`s port :" + port);
out.println("Welcome! Happy newyear!");
String str = in.readLine();
while(! str.equals("quit")){
System.out.println("Client said:" + str);
str = in.readLine();
}
System.out.println("Client want to leave.");
}catch(Exception e){
System.out.println("Error:" + e);
}
finally{
is.close();
os.close();
soc.close();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -