欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Socket实现的传输对象功能示例

程序员文章站 2023-12-17 14:50:52
本文实例讲述了java socket实现的传输对象功能。分享给大家供大家参考,具体如下: 前面两篇文章介绍了怎样建立java socket通信,这里说一下怎样使用java...

本文实例讲述了java socket实现的传输对象功能。分享给大家供大家参考,具体如下:

前面两篇文章介绍了怎样建立java socket通信,这里说一下怎样使用java socket来传输对象。

首先需要一个普通的对象类,由于需要序列化这个对象以便在网络上传输,所以实现java.io.serializable接口就是必不可少的了,如下:

package com.googlecode.garbagecan.test.socket.sample3;
public class user implements java.io.serializable {
  private static final long serialversionuid = 1l;
  private string name;
  private string password;
  public user() {
  }
  public user(string name, string password) {
    this.name = name;
    this.password = password;
  }
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public string getpassword() {
    return password;
  }
  public void setpassword(string password) {
    this.password = password;
  }
}

对于server端的代码,代码中分别使用了objectinputstream和objectoutputstream来接收和发送socket中的inputstream和outputstream,然后转换成java对象,如下:

package com.googlecode.garbagecan.test.socket.sample3;
import java.io.*;
import java.net.serversocket;
import java.net.socket;
import java.util.logging.level;
import java.util.logging.logger;
public class myserver {
  private final static logger logger = logger.getlogger(myserver.class.getname());
  public static void main(string[] args) throws ioexception {
    serversocket server = new serversocket(10000);
    while (true) {
      socket socket = server.accept();
      invoke(socket);
    }
  }
  private static void invoke(final socket socket) throws ioexception {
    new thread(new runnable() {
      public void run() {
        objectinputstream is = null;
        objectoutputstream os = null;
        try {
          is = new objectinputstream(new bufferedinputstream(socket.getinputstream()));
          os = new objectoutputstream(socket.getoutputstream());
          object obj = is.readobject();
          user user = (user)obj;
          system.out.println("user: " + user.getname() + "/" + user.getpassword());
          user.setname(user.getname() + "_new");
          user.setpassword(user.getpassword() + "_new");
          os.writeobject(user);
          os.flush();
        } catch (ioexception ex) {
          logger.log(level.severe, null, ex);
        } catch(classnotfoundexception ex) {
          logger.log(level.severe, null, ex);
        } finally {
          try {
            is.close();
          } catch(exception ex) {}
          try {
            os.close();
          } catch(exception ex) {}
          try {
            socket.close();
          } catch(exception ex) {}
        }
      }
    }).start();
  }
}

client也和server端类似,同样使用objectoutputstream和objectinputstream来处理,如下:

package com.googlecode.garbagecan.test.socket.sample3;
import java.io.bufferedinputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.net.socket;
import java.util.logging.level;
import java.util.logging.logger;
public class myclient {
  private final static logger logger = logger.getlogger(myclient.class.getname());
  public static void main(string[] args) throws exception {
    for (int i = 0; i < 100; i++) {
      socket socket = null;
      objectoutputstream os = null;
      objectinputstream is = null;
      try {
        socket = new socket("localhost", 10000);
        os = new objectoutputstream(socket.getoutputstream());
        user user = new user("user_" + i, "password_" + i);
        os.writeobject(user);
        os.flush();
        is = new objectinputstream(new bufferedinputstream(socket.getinputstream()));
        object obj = is.readobject();
        if (obj != null) {
          user = (user)obj;
          system.out.println("user: " + user.getname() + "/" + user.getpassword());
        }
      } catch(ioexception ex) {
        logger.log(level.severe, null, ex);
      } finally {
        try {
          is.close();
        } catch(exception ex) {}
        try {
          os.close();
        } catch(exception ex) {}
        try {
          socket.close();
        } catch(exception ex) {}
      }
    }
  }
}

最后测试上面的代码,首先运行server类,然后运行client类,就可以分别在server端和client端控制台看到接收到的user对象实例了。

更多关于java相关内容感兴趣的读者可查看本站专题:《java socket编程技巧总结》、《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: