In Java networking programming, examples of client and server communication:
First, the server listens to the code
PackageCom.server;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importcom.jim.Student; Public classQqserver {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("Start Server ..."); Qqserver Server=NewQqserver (); } //constructor Function PublicQqserver () {Socket s; //Create serversocket on port No. 9999 monitoringServerSocket SS =NULL; Try{SS=NewServerSocket (9999); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("The server is listening ..."); Try{System.out.println ("Start waiting for connection ..."); S= Ss.accept ();//blocking, waiting for connectionSYSTEM.OUT.PRINTLN ("Client connection succeeded"); //Receive StreamObjectInputStream ois=NewObjectInputStream (S.getinputstream ()); //Send StreamObjectOutputStream Oos =NewObjectOutputStream (S.getoutputstream ()); //Receive student ObjectsStudent stu =(Student) ois.readobject (); System.out.println (Stu.getname ()); //Send Student ObjectOos.writeobject (NewStudent ("Server")); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }
The client's code:
Packagecom.client;ImportJava.io.*;Importjava.net.*;Importcom.jim.Student; Public classqqclient {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("Start qqclient ..."); Try{qqclient Client=Newqqclient (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //constructor Function PublicQqclient ()throwsIOException {//String address= "127.0.0.1"; //int port = 9999; Try{Socket s=NewSocket ("127.0.0.1", 9999); Student Stu=NewStudent ("Jim"); //send a message to the serverObjectOutputStream oos=NewObjectOutputStream (S.getoutputstream ()); //objectinputstream ois=new ObjectInputStream (S.getinputstream ()); //accept the message from the serverObjectInputStream ois=NewObjectInputStream (S.getinputstream ()); //sending objects to the server//Oos.writeobject (New Student ("OKK"));Oos.writeobject (STU); //objects returned by the receiving serverStudent Re_stu =(Student) ois.readobject (); System.out.println (Re_stu.getname ()); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Object objects that are transferred on the client and the service
PackageCom.jim; Public classStudentImplementsjava.io.serializable{PrivateString name; Private intAge ; PrivateString address; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } /** * @paramName * Construction Method*/ PublicStudent (String name) { This. Name =name; } /** * @param * */ Public voidStudy () {SYSTEM.OUT.PRINTLN ( This. Name + "is studing"); } }
Java Network programming client and server communication