How to use Java Socket to transmit objects.
First, we need a common object class. Because we need to serialize this object for transmission over the network, it is essential to implement the java. Io. serializable interface, as shown below:
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;
}
}
For the server code, objectinputstream and objectoutputstream are used in the code to receive and send inputstream and outputstream in the socket respectively, and then convert them into Java objects, as shown below:
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. 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 ();
}
}
The client is similar to the server. It also uses objectoutputstream and objectinputstream for processing, as shown below:
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. 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 ){}
}
}
}
}
Finally, test the above Code. First, run the server class, and then run the client class. Then, you can view the received user object instances on the server and client console respectively.
Thanks to the wild call: http://blog.163.com/liyinhui20080527@126/blog/static/815232582012319609772/