Java Socket实战之三:传输对象

Java Socket实战之三:传输对象

 

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

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

  1. packagecom.googlecode.garbagecan.test.socket.sample3;
  2. publicclassUserimplementsjava.io.Serializable{
  3. privatestaticfinallongserialVersionUID=1L;
  4. privateStringname;
  5. privateStringpassword;
  6. publicUser(){
  7. }
  8. publicUser(Stringname,Stringpassword){
  9. this.name=name;
  10. this.password=password;
  11. }
  12. publicStringgetName(){
  13. returnname;
  14. }
  15. publicvoidsetName(Stringname){
  16. this.name=name;
  17. }
  18. publicStringgetPassword(){
  19. returnpassword;
  20. }
  21. publicvoidsetPassword(Stringpassword){
  22. this.password=password;
  23. }
  24. }

对于Server端的代码,代码中分别使用了ObjectInputStream和ObjectOutputStream来接收和发送socket中的InputStream和OutputStream,然后转换成Java对象,如下:

  1. packagecom.googlecode.garbagecan.test.socket.sample3;
  2. importjava.io.*;
  3. importjava.net.ServerSocket;
  4. importjava.net.Socket;
  5. importjava.util.logging.Level;
  6. importjava.util.logging.Logger;
  7. publicclassMyServer{
  8. privatefinalstaticLoggerlogger=Logger.getLogger(MyServer.class.getName());
  9. publicstaticvoidmain(String[]args)throwsIOException{
  10. ServerSocketserver=newServerSocket(10000);
  11. while(true){
  12. Socketsocket=server.accept();
  13. invoke(socket);
  14. }
  15. }
  16. privatestaticvoidinvoke(finalSocketsocket)throwsIOException{
  17. newThread(newRunnable(){
  18. publicvoidrun(){
  19. ObjectInputStreamis=null;
  20. ObjectOutputStreamos=null;
  21. try{
  22. is=newObjectInputStream(newBufferedInputStream(socket.getInputStream()));
  23. os=newObjectOutputStream(socket.getOutputStream());
  24. Objectobj=is.readObject();
  25. Useruser=(User)obj;
  26. System.out.println("user:"+user.getName()+"/"+user.getPassword());
  27. user.setName(user.getName()+"_new");
  28. user.setPassword(user.getPassword()+"_new");
  29. os.writeObject(user);
  30. os.flush();
  31. }catch(IOExceptionex){
  32. logger.log(Level.SEVERE,null,ex);
  33. }catch(ClassNotFoundExceptionex){
  34. logger.log(Level.SEVERE,null,ex);
  35. }finally{
  36. try{
  37. is.close();
  38. }catch(Exceptionex){}
  39. try{
  40. os.close();
  41. }catch(Exceptionex){}
  42. try{
  43. socket.close();
  44. }catch(Exceptionex){}
  45. }
  46. }
  47. }).start();
  48. }
  49. }

Client也和Server端类似,同样使用ObjectOutputStream和ObjectInputStream来处理,如下:

  1. packagecom.googlecode.garbagecan.test.socket.sample3;
  2. importjava.io.BufferedInputStream;
  3. importjava.io.IOException;
  4. importjava.io.ObjectInputStream;
  5. importjava.io.ObjectOutputStream;
  6. importjava.net.Socket;
  7. importjava.util.logging.Level;
  8. importjava.util.logging.Logger;
  9. publicclassMyClient{
  10. privatefinalstaticLoggerlogger=Logger.getLogger(MyClient.class.getName());
  11. publicstaticvoidmain(String[]args)throwsException{
  12. for(inti=0;i<100;i++){
  13. Socketsocket=null;
  14. ObjectOutputStreamos=null;
  15. ObjectInputStreamis=null;
  16. try{
  17. socket=newSocket("localhost",10000);
  18. os=newObjectOutputStream(socket.getOutputStream());
  19. Useruser=newUser("user_"+i,"password_"+i);
  20. os.writeObject(user);
  21. os.flush();
  22. is=newObjectInputStream(newBufferedInputStream(socket.getInputStream()));
  23. Objectobj=is.readObject();
  24. if(obj!=null){
  25. user=(User)obj;
  26. System.out.println("user:"+user.getName()+"/"+user.getPassword());
  27. }
  28. }catch(IOExceptionex){
  29. logger.log(Level.SEVERE,null,ex);
  30. }finally{
  31. try{
  32. is.close();
  33. }catch(Exceptionex){}
  34. try{
  35. os.close();
  36. }catch(Exceptionex){}
  37. try{
  38. socket.close();
  39. }catch(Exceptionex){}
  40. }
  41. }
  42. }
  43. }

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

原文链接:http://blog.csdn.net/kongxx/article/details/7259827

猜你喜欢

转载自xiaoxiong0309.iteye.com/blog/2231494