java网络编程、多线程实现多人即时聊天、Mina即时通讯框架

一、socket编程
1、建立服务端程序

ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("服务器已经启动,等待客户端连接请求。。。");
/**调用accept方法,等待连接,将会阻塞程序执行*/
Socket accept = serverSocket.accept();
System.out.println("客户端已连接:"+accept.getRemoteSocketAddress());
System.out.println("服务端接收到数据:");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
String inputString = bufferedReader.readLine();
System.out.println(inputString);

System.out.println("服务端返回数据到客户端:");
PrintStream printStream = new PrintStream(accept.getOutputStream());
printStream.println("echo:"+inputString);
printStream.flush();

printStream.close();
bufferedReader.close();

2、建立客户端程序

Socket socket = new Socket("127.0.0.1",6666);
System.out.println("客户端已经连接服务端,即将写入数据:hello,I am sunkeliang");
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println("hello,I am sunkeliang");
printStream.flus

你可能感兴趣的:(后端技术)