📄 chatserver.java
字号:
import java.io.*;
import java.net.*;
public class chatserver{
public static void main(String[] args)
{
ServerSocket svrsoc=null;
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
DataInputStream sysin=null;
InetAddress clientIP=null;
String str=null;
String strout=null;
try
{
svrsoc=new ServerSocket(8000);
System.out.println("Server is listening at port 8000!");
soc=svrsoc.accept();
sysin=new DataInputStream(System.in);
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
clientIP=soc.getInetAddress();
System.out.println("Client's IP address:"+clientIP);
out.println("welcome!...");
//str=in.readLine();
while(true){
str=in.readLine();
System.out.println("Client said:"+str);
if (str.equals("quit"))break;
System.out.print("server said:");
strout=sysin.readLine();
out.println(strout);
System.out.println(" |Waiting for client's message");
}
System.out.println("Client want to leave");
in.close();
out.close();
soc.close();
svrsoc.close();
System.exit(0);
}
catch(IOException e)
{
System.out.println("error:"+e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -