Java Network (socket)

Source: Internet
Author: User

This article combs a basic Java TCP message communication, constructs a simple packet to carry on the transmission, the code is as follows:

    • Packet
 Public class Packet {    private  String attribute;          Public Packet (String attr) {        this. Attribute = attr;    }           Public String getattribute () {        return  attribute;    }      Public void setAttribute (String attribute) {        this. Attribute = attribute;    }} 
    • Server-side code
 Public classApp {Private Static Final intPORT = 4000;  Public Static voidMain (string[] args)throwsIOException {ServerSocket Listen=NewServerSocket (PORT); Socket Client=NULL;  while(true) {Client=listen.accept (); NewThread (Newserverthread (client)). Start (); }    }}
 Public classServerthreadImplementsrunnable{Private Static Final intBUFSIZE =1024; PrivateSocket client =NULL;  PublicServerthread (Socket client) { This. Client =client; } @Override Public voidrun () {Try{InputStream in=Client.getinputstream (); OutputStream out=Client.getoutputstream (); byte[] buffer =New byte[BUFSIZE];  while(In.read (buffer)! =-1) {                //Judging Baotou                if(Buffer[0] = = 0x01 && buffer[1] = = 0x02){                    byte[] tmp =New byte[BUFSIZE]; intindex = 0;  for(inti = 2; i < buffer.length; i++) {Tmp[index++] =Buffer[i]; } Packet Packet=NewPacket (NewString (tmp, "GB2312"));                System.out.println (Packet.getattribute ()); }Else{System.out.println ("Message format is incorrect");            }} out.close ();                    In.close (); } Catch(IOException e) {e.printstacktrace (); }        }}
    • Client code
 Public classApp {Private Static Final intPORT = 4000; Private Static Final intBUFSIZE =1024;  Public Static voidMain (string[] args)throwsIOException {Socket client=NewSocket ("127.0.0.1", PORT); InputStream in=Client.getinputstream (); OutputStream out=Client.getoutputstream (); Packet Packet=NewPacket ("type parameter 2345"); byte[] Output =New byte[BUFSIZE]; output[0] = 0x01; output[1] = 0x02; intindex = 2; //If you add a packet length field, you can implement packet subcontracting, group package        byte[] Tmp1 =Packet.getattribute (). GetBytes ();  for(inti= 0; i < tmp1.length; i++) {Output[index++] =Tmp1[i];                } out.write (output); byte[] buffer =New byte[BUFSIZE];  while(In.read (buffer)! =-1) {System.out.println (NewString (buffer, "GB2312"));        } out.close ();        In.close ();                Client.close (); }}

Java Network (socket)

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.