Socket implementation Object Transfer "Java implementation"

Source: Internet
Author: User

Tag:socket    object transfer    java   

/* Client */import person.*;import java.net.inetaddress;import java.net.socket;import  java.io.objectoutput;import java.io.objectoutputstream;import java.io.outputstream;import  Java.io.serializable; public class client {    public static  void main (String[] args)     {         try        {             socket soc = new socket (Inetaddress.getlocalhost (), 8020);             OutputStream o =  Soc.getoutputstream ();             objectoutput  s = new objectoutputstream (o);             person person = new peRson ("Tony", "Female", "Han");             S.writeobject (person);             s.flush ();             s.close ();             }catch (exception e)          {            system.out.println (e.getMessage ());             system.out.println ("Error  During serialization ");             System.exit (1);         }    }}
/* Service Side */import java.io.inputstream;import java.io.objectinput;import java.io.objectinputstream; Import java.net.serversocket;import java.net.socket;import person. Person;public class server {    public static void main ( String[] args)  {    ServerSocket ser = null;     Socket soc = null;             //Accept client Connections     Person person = null;    try{         ser = new serversocket (8020);         soc = ser.accept ();         Inputstream o = soc.getinputstream ();         Objectinput s = new objectinputstream (o);         person =  (person) s.readobject ();         s.close ();         system.out.println (person);     }catch (exception e)     {    system.out.println ( E.getmessage ());     system.out.println ("Errior during serialization");     system.exit (1);     }    }}
/* The object to transfer */import java.io.serializable;public class person implements serializable{     public String name;    public int age;     public String sex;    public String nationlity;         public person (string name,int age,string  sex,string nationlity)     {        this.name  = name;        this.age = age;         this.sex = sex;         This.nationlity = nationlity;    }    public string  tostring ()     {        return  "Name:"  + name +  "\nage: "+ age + " \nsex: " + sex + " \nnationality: " +         nationlity +  "\ n";     }}


This article is from the "Black4yl" blog, make sure to keep this source http://black4yl.blog.51cto.com/4222963/1589686

Socket implementation Object Transfer "Java implementation"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.