Socket编程

使用多线程实现一个服务器连接多个客户端传输数据   

/**
*用户类,实现可序列化接口
*/
public
class User implements Serializable { private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public User(String name, String pwd) { super(); this.name = name; this.pwd = pwd; } }
/**
   * 创建客户端类,继承Thread类,实现多线程
   */
public class ServerLogin extends Thread{
    private static ServerSocket serversocket;
    private static Socket socket;
    public static void main(String[] args) throws IOException {
        // 创建一个ServerSocket对象 指定端口
        serversocket = new ServerSocket(8000);
        // 持续监听
        while(true){
            socket = serversocket.accept();
        ServerLogin serverlogin = new ServerLogin();
        serverlogin.start();
        }        
    }
    public void run() {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = socket.getInputStream();
            os = socket.getOutputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            User user;
            try {
                user = (User)ois.readObject();
                System.out.println("我是服务器,客户端信息为:用户名"+user.getName()+"密码"+user.getPwd());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            socket.shutdownInput();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //服务器给客户端一个响应
        // 打开输出流,发送数据
        String info = "欢迎您,登录成功!";
        try {
            os.write(info.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            is.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}
/**
   * 客户端类,开启客户端,与服务器连接
   */
public class Client {
    public static void main(String[] args) throws UnknownHostException, IOException {
        // 创建一个socket对象(指定服务器和端口号)
        Socket socket = new Socket("127.0.0.1",8000);
        // 打开输出流,发送请求
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();
        User user = new User("TOM","1234");
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(user);
        socket.shutdownInput();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String info = "";
        while((info=reader.readLine())!=null){
            System.out.println("我是客户端,服务器的响应是"+info);
            
        }
        os.close();is.close();socket.close();
    }
}
/**
   * 创建一个新客户端,实现多个客户端与服务器端连接
   */
public class CopyOfClient {
    public static void main(String[] args) throws UnknownHostException, IOException {
        // 创建一个socket对象(指定服务器和端口号)
        Socket socket = new Socket("localhost",8000);
        // 打开输出流,发送请求
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();
        User user = new User("Lisa","1234");
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(user);
        socket.shutdownInput();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String info = "";
        while((info=reader.readLine())!=null){
            System.out.println("我是客户端,服务器的响应是"+info);
            
        }
        os.close();is.close();socket.close();
    }
}

 

posted @ 2017-05-20 10:10  AlexanderTheGreat  阅读(118)  评论(0编辑  收藏  举报