In the past, I wrote a chat program and sent a sign to the server directly with a strings, and then forwarded it.
Now I ask me to send the object directly with the object flow, test it, and record it.
In fact, I am more inclined to send objects with JSON. JSON said that it is also a string. When I have time, I have been able to study it.
Main functions: The client sends an object to the server, and the server receives it. The other way is almost the same, so I won't write it.
The above is the overall architecture.
The user class is two attributes.
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: User object sent by the client:
Copy code code as follows:
package test;
Import java.io.BuffRedInputStream;
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 input stream
Objectstream ois = new objectinputstream (
New bufferedInputStream (socket.getInputStream ()));
Object obj = ois.readObject ();
if (obj! = null) {
User user = (user) obj; // transform the received object into 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: Send a USER object to the server:
Copy code code as follows:
package client;
Import java.io.ioException;
Import java.io.objectPutstream;
Import java.net.socket;
Import java.net.unknownHostexception;
Import com.qiantu.bean.user;
public class testClient {
Public Static Void Main (String [] args) {{
new test (). Start ();
}
public void start () {
try {
Socket socket = new socket ("127.0.0.1", 7777);
// Create input stream
Objectputstream OOS = New ObjectPutstream (Socket
.getputstream ());
User user = new user ();
user.setName ("Liang Guoqiao");
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 results:
Pay attention to:
The "1" physical class should implement the Serializable class, and add the identification serviceVersionuid.
Flush () after the "2" sends the object;
"3" is more important. I don't know where to make mistakes for a long time.
The physical category on both sides of the server is exactly the same. The class name is the same, and the package name should be the same. I have been doing the name for a long time. Essence Essence Essence