Java Network programming client and server communication

Source: Internet
Author: User
Tags stub

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

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.