Tcp实现对象传输

来源:互联网 发布:能看回放的网络电视 编辑:程序博客网 时间:2024/05/29 03:46

服务器处理用户连接线程类:

public class ServerThread extends Thread {    private Socket socket=null;    public ServerThread(Socket socket){        this.socket=socket;    }    @Override    public void run() {        //3、获取输入流,并读取客户端信息        InputStream is= null;        ObjectInputStream ois=null;        ObjectOutputStream oos=null;        OutputStream os= null;        try {                is = socket.getInputStream();                ois=new ObjectInputStream(is);                Student stu=(Student)ois.readObject();                //ois.close();//不能加,可能会造成socket过早关闭                System.out.println(stu);                //socket.shutdownInput();//关闭输入流                //4、获取输出流,响应客户端的请求                os = socket.getOutputStream();                oos=new ObjectOutputStream(os);                Student student=new Student(1,"服务器","中",20);                oos.writeObject(student);                oos.flush();                oos.close();                TimeUnit.MILLISECONDS.sleep(100);            //socket.shutdownOutput();//关闭输出流        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }catch (InterruptedException e) {            e.printStackTrace();        }finally {            //5、关闭资源            try {                if(is!=null)                    is.close();                if(os!=null)                    os.close();                socket.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
服务端:

public class Server {    public static void main(String[] args){        try {            //1、创建一个服务器端Socket,即ServerSocket,指定绑定的端口            ServerSocket serverSocket=new ServerSocket(8989);            int num=0;//统计客户端的数量            //2、调用accept()方法开始坚挺,等待客户端的连接            System.out.println("***服务器即将启动,等待客户端的连接***");            Socket socket=null;            while (true){                socket=serverSocket.accept();                ServerThread serverThread=new ServerThread(socket);                serverThread.start();                num++;                System.out.println("当前客户端的数量:"+num);                InetAddress inetAddress=socket.getInetAddress();                System.out.println("客户端的IP地址为:"+inetAddress.getHostAddress());            }        } catch (IOException e) {            e.printStackTrace();        }    }}
客户端:

public class Client {    public static void main(String[] args){        try {                //1.创建客户端Socket,指定服务器地址和端口                Socket socket=new Socket("localhost",8989);                //2.获取输入流,像服务器发送信息                OutputStream os=socket.getOutputStream();//字节输入流                ObjectOutputStream oos=new ObjectOutputStream(os);                Student student=new Student(2,"张三","男",12);                oos.writeObject(student);                oos.flush();                //oos.close();                //socket.shutdownOutput();//关闭输出流                //3.获取输入流,并读取服务器端的响应信息                InputStream is=socket.getInputStream();                if(is!=null) {                    ObjectInputStream ois = new ObjectInputStream(is);                    student = (Student) ois.readObject();                    System.out.println(student);                    ois.close();                }                //socket.shutdownInput();//关闭输入流                //4/关闭资源                oos.close();                is.close();                os.close();                socket.close();            } catch (ClassNotFoundException e) {                e.printStackTrace();            }catch (IOException e) {            e.printStackTrace();        }    }}
***服务器即将启动,等待客户端的连接***
当前客户端的数量:1
客户端的IP地址为:127.0.0.1
[2,张三,男,12]
当前客户端的数量:2
客户端的IP地址为:127.0.0.1
[2,张三,男,12]
当前客户端的数量:3
客户端的IP地址为:127.0.0.1
[2,张三,男,12]

0 0
原创粉丝点击