package com.lei.chatroom.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketImpl;
import java.util.Scanner;
//服务器端不断的读取客户端写过来的数据,并且不断的反馈数据
public class ChatRoomServer {
public static void main(String[] args) {
try {
//创建服务器端Socket对象
ServerSocket ss=new ServerSocket(1111);
//监听客户端
Socket socket=ss.accept();
//获取字节输入和输出流
InputStream inputStream=socket.getInputStream();
OutputStream outputStream=socket.getOutputStream();
//创建键盘录入对象
Scanner sc=new Scanner(System.in);
while (true){
//读取消息 一次读取一个字节数组
byte[] bytes=new byte[1024];
int length=inputStream.read(bytes);//"读就完事了"
//展示数据
System.out.println(new String(bytes,0,length));
//不断反馈数据(不关闭)
System.out.println("请你回复消息:");
String leilei=sc.nextLine();
outputStream.write(leilei.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}