A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

      在eclipse中,多人访问服务器程序的时候,只能有一个人和服务器聊天,是为什么?如何改进?求解
服务器程序
package server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {

        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();
                                                               
                                                               
                                                               
                                                        } catch (IOException e) {
                                                                e.printStackTrace();
                                                        }
                                                }
                                               
                                        }
                                        ).start();
                }
        }

}

客户端程序:
package server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class SocketDemo {

        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));
               
                s.close();
               
        }
        }
}



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马