Java to send Java object instance using socket _java

Source: Internet
Author: User
Tags flush

The previous write chat program is to use a string plus a flag directly sent to the server, and then forwarded.
Now ask me to send the object directly with the object stream, test it, and record it.
In fact, I am more inclined to use JSON to send objects, JSON is also a string, and so free to study.
Main function: The client sends an object to the server, and the server receives the print. In turn, it's almost over, not written.

Above is the overall architecture.
The user class has two properties.

Copy Code code as follows:

Package Com.qiantu.bean;

Import java.io.Serializable;

public class User implements Serializable {
Private static final long serialversionuid = 1L;
private String name;
private String 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 side: The user object sent by the receiving client:

Copy Code code as follows:

Package test;

Import Java.io.BufferedInputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;

Import Com.qiantu.bean.User;

public class TestServer {
public void Start () {
try {
ServerSocket ss = new ServerSocket (7777);
System.out.println ("Start to accept ...");
Socket socket = ss.accept ();

Create an input stream
ObjectInputStream ois = new ObjectInputStream (
New Bufferedinputstream (Socket.getinputstream ()));
Object obj = Ois.readobject ();
if (obj!= null) {
The User user = (user) obj;//converts the received object to user
System.out.println ("User:" + user.getname ());
System.out.println ("Password:" + User.getpassword ());
}
Ois.close ();
Socket.close ();
Ss.close ();
catch (IOException e) {
E.printstacktrace ();
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}

public static void Main (string[] args) {
New TestServer (). Start ();
}
}

Client: Sends the user object to the server side:

Copy Code code as follows:

Package client;

Import java.io.IOException;
Import Java.io.ObjectOutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;

Import Com.qiantu.bean.User;

public class TestClient {
public static void Main (string[] args) {
New TestClient (). Start ();
}

public void Start () {
try {
Socket socket = new Socket ("127.0.0.1", 7777);
Create an input stream
ObjectOutputStream oos = new ObjectOutputStream (socket
. Getoutputstream ());
User user = new user ();
User.setname ("Liang Qiao");
User.setpassword ("123456");
Enter the object, be sure to flush ()
Oos.writeobject (user);
Oos.flush ();

Oos.close ();
Socket.close ();
catch (Unknownhostexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
}


Run Result:

What to note:
1 entity classes to implement the Serializable class, add identity Serialversionuid.
"2" after sending the object to flush ();
"3" This is more important, I did not know what went wrong, say this. The
server-side and client-side entity classes are identical, with the same class name as the package name. I've been doing it for a long time because of the name of the bag.

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.