public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8888);
final Scanner sc = new Scanner(System.in);
while(true) {
final Socket s = ss.accept();
new Thread(
new Runnable() {
public void run() {
try {
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int len = -1;
while((len=is.read(b))!=-1) {
System.out.println(new String(b,0,len));
}
OutputStream os = s.getOutputStream();
System.out.println("请输入回复:");
String str = sc.nextLine();
os.write(str.getBytes());
os.flush();
s.shutdownOutput();
is.close();
public static void main(String[] args) throws Exception {
InetAddress ip = InetAddress.getLocalHost();
while(true) {
Socket s = new Socket("192.168.106.29",6666);
OutputStream os = s.getOutputStream();
Scanner sc = new Scanner(System.in);
System.out.println("请输入聊天内容:");
String str = sc.nextLine();
os.write(str.getBytes());
os.flush();
s.shutdownOutput();
//os.close();
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int len = is.read(b);
System.out.println(new String(b,0,len));