chatserver.java

来自「java的UDP聊天程序」· Java 代码 · 共 50 行

JAVA
50
字号
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 + =
减小字号Ctrl + -
显示快捷键?