Example of Java socket byte stream transmission

Source: Internet
Author: User
Tags getmessage

Server-side servers:

 PackageCom.yuan.socket;ImportJava.io.*;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** Created by YUAN on 2016-09-17.*/ Public classTalkserver4byte {PrivateServerSocket server; Private intPort = 5020;  PublicTalkserver4byte () {Try{Server=NewServerSocket (port); } Catch(IOException e) {}} Public voidTalk () {System.out.println ("Monitoring port:" +port); Socket Socket=NULL;  while(true) {            Try {                //blocking waits, creating a new connection instance each request is receivedSocket =server.accept (); System.out.println ("Connect client Address:" +socket.getremotesocketaddress ()); //Decorative Flow BufferedReader encapsulated input stream (receiving client's stream)Bufferedinputstream bis =NewBufferedinputstream (Socket.getinputstream ()); DataInputStream Dis=NewDataInputStream (bis); byte[] bytes =New byte[1];//read one byte at a timeString ret = "";  while(Dis.read (bytes)! =-1) {ret+ = bytestohexstring (bytes) + ""; if(dis.available () = = 0) {//a requestdosomething (ret); }                }            } Catch(IOException e) {System.out.println (E.getmessage ()); } finally {                Try{socket.close (); } Catch(IOException e) {System.out.println (E.getmessage ()); }            }        }    }         Public Static voiddosomething (String ret) {System.out.println (ret); }     Public StaticString bytestohexstring (byte[] src) {StringBuilder StringBuilder=NewStringBuilder (""); if(src = =NULL|| Src.length <= 0) {            return NULL; }         for(inti = 0; i < src.length; i++) {            intv = src[i] & 0xFF; String HV=integer.tohexstring (v); if(Hv.length () < 2) {stringbuilder.append (0);        } stringbuilder.append (HV); }        returnstringbuilder.tostring (); }     Public StaticString byteshexstring (byte[] b) {String ret= "";  for(inti = 0; i < b.length; i++) {String hex= Integer.tohexstring (B[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex; } ret+=hex.touppercase (); }        returnret; }     Public Static voidMain (string[] args) {talkserver4byte server=NewTalkserver4byte ();    Server.talk (); }}

Client-side code:

 PackageCom.yuan.socket;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.InetSocketAddress;ImportJava.net.Socket;Importjava.net.SocketAddress;/*** Created by YUAN on 2016-09-17.*/ Public classTalkclient4byte {Privatesocket socket; Privatesocketaddress address;  PublicTalkclient4byte () {Try{Socket=NewSocket (); Address=NewInetsocketaddress ("127.0.0.1", 5020); Socket.connect (Address,1000); } Catch(IOException e) {e.printstacktrace (); }    }     Public voidTalk () {Try {            //using the DataInputStream package input streamInputStream OS =NewDataInputStream (system.in); byte[] B =New byte[1]; DataOutputStream dos=NewDataOutputStream (Socket.getoutputstream ());  while(-1! =Os.read (b)) {Dos.write (b);//Send to client} dos.flush ();        Dos.close (); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{socket.close (); } Catch(IOException e) {}}}  Public Static voidMain (string[] args) {talkclient4byte client=NewTalkclient4byte ();    Client.talk (); }}

Example of Java socket byte stream transmission

Related Article

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.