欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

chat 服务器

程序员文章站 2022-03-15 21:37:55
...
import java.io.*;

import java.net.*;

public class newserver {
	ServerSocket serverSocket;
	
	public newserver() {
		try {
			
			serverSocket=new ServerSocket(1123);
			System.out.println("it is successful to creat a port");
			System.out.println("等待客户端请求连接......");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void start() {
		Socket socket;
		
		
			try {
			System.out.println("接受中");
			socket = serverSocket.accept();
			System.out.println("一个客户端·连接成功");
			InputStream in=socket.getInputStream();
			InputStreamReader isr=new InputStreamReader(in,"UTF-8");
			BufferedReader bfr=new BufferedReader(isr);
			while (true) {
				System.out.println(bfr.readLine());
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new newserver().start();
	}

}

相关标签: tcp