首页 > 代码库 > socket多线程方式案例

socket多线程方式案例

记下来,方便以后查看

 

User类

技术分享
 1 package com.xujingyang.ThreadSocket;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private String name;
 7     private int age;
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     public int getAge() {
18         return age;
19     }
20 
21     public void setAge(int age) {
22         this.age = age;
23     }
24 
25     public User(String name, int age) {
26         super();
27         this.name = name;
28         this.age = age;
29     }
30 
31     public User() {
32     }
33 
34     @Override
35     public int hashCode() {
36         final int prime = 31;
37         int result = 1;
38         result = prime * result + age;
39         result = prime * result + ((name == null) ? 0 : name.hashCode());
40         return result;
41     }
42 
43     @Override
44     public boolean equals(Object obj) {
45         if (this == obj)
46             return true;
47         if (obj == null)
48             return false;
49         if (getClass() != obj.getClass())
50             return false;
51         User other = (User) obj;
52         if (age != other.age)
53             return false;
54         if (name == null) {
55             if (other.name != null)
56                 return false;
57         } else if (!name.equals(other.name))
58             return false;
59         return true;
60     }
61 
62     @Override
63     public String toString() {
64         return "User [name=" + name + ", age=" + age + "]";
65     }
66 }
View Code

 

客户端类

技术分享
package com.xujingyang.ThreadSocket;

import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {
        Socket socket=new Socket("localhost", 9999);//监听端口
        
        ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());//包装对象流
        
        out.writeObject(new User("小明", 21));//写入对象
        socket.shutdownOutput();//关闭流的占用
        
        //获取服务器发过来的消息
        InputStream in = socket.getInputStream();
        byte[] b=new byte[1024];
        in.read(b);
        System.out.println(new String(b,0,b.length));
        
        //关闭流
        in.close();
        out.close();
        socket.close();
        
    }
}
View Code

服务器类

技术分享
package com.xujingyang.ThreadSocket;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket socket=new ServerSocket(9999);//监听端口
        
        //循环监听接收各个客户端连接
        while (true) {
            Socket client = socket.accept();
            
            //开启多线程接受客户端信息
            ThreadServer ts=new ThreadServer(client);
            new Thread(ts).start();
        }
        
    }
}
View Code

 

服务器多线程接收类

技术分享
package com.xujingyang.ThreadSocket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 多线程接受用户登陆,把客户端连接对象传入作为成员变量
 * @author 徐景洋
 */
public class ThreadServer implements Runnable {

    private Socket client;

    public ThreadServer(Socket client) {
        this.client=client;
    }

    @Override
    public void run() {
        try {
            //包装对象接收流
            ObjectInputStream in=new ObjectInputStream(client.getInputStream());
            User user = (User) in.readObject();//读取对象
            
            //比较是否符合标准.符合回答成功,否则失败
            OutputStream out = client.getOutputStream();
            if((new User("小明",21)).equals(user)){
                out.write("登陆成功".getBytes());
                System.out.println(user);
            }else{
                out.write("登陆失败".getBytes());
            }
            
            client.shutdownOutput();
            out.close();
            in.close();
            client.close();
            
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        
    }
    
}
View Code

 

socket多线程方式案例