Example of Java socket byte stream transmission, using ocket

Source: Internet
Author: User

Example of Java socket byte stream transmission, using ocket

Server side:

 

Package com. yuan. socket; import java. io. *; import java.net. serverSocket; import java.net. socket;/*** Created by YUAN on 2016-09-17. */public class TalkServer4Byte {private ServerSocket server; private int port = 5020; public TalkServer4Byte () {try {server = new ServerSocket (port);} catch (IOException e) {}} public void talk () {System. out. println ("Monitoring port:" + port); Socket socket = null; while (tr Ue) {try {// blocking wait. A new connection instance socket = server is created every time a request is received. accept (); System. out. println ("Connect client address:" + socket. getRemoteSocketAddress (); // The decoration stream BufferedReader encapsulates the input stream (receives the stream from the client) BufferedInputStream bis = new BufferedInputStream (socket. getInputStream (); DataInputStream dis = new DataInputStream (bis); byte [] bytes = new byte [1]; // read a byte String ret = ""; while (dis. read (bytes )! =-1) {ret + = bytesToHexString (bytes) + ""; if (dis. available () = 0) {// one request doSomething (ret) ;}} catch (IOException e) {System. out. println (e. getMessage ();} finally {try {socket. close ();} catch (IOException e) {System. out. println (e. getMessage () ;}}} public static void doSomething (String ret) {System. out. println (ret);} public static String bytesToHexString (byte [] src) {StringBuilder stringBuilder = new StringBuilder (""); if (src = null | src. length <= 0) {return null;} for (int I = 0; I <src. length; I ++) {int v = src [I] & 0xFF; String hv = Integer. toHexString (v); if (hv. length () <2) {stringBuilder. append (0);} stringBuilder. append (hv);} return stringBuilder. toString ();} public static String BytesHexString (byte [] B) {String ret = ""; for (int I = 0; I <B. length; I ++) {String hex = Integer. toHexString (B [I] & 0xFF); if (hex. length () = 1) {hex = '0' + hex;} ret + = hex. toUpperCase ();} return ret;} public static void main (String [] args) {TalkServer4Byte server = new TalkServer4Byte (); server. talk ();}}

 

 

 

Client code:

 

Package com. yuan. socket; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java. io. inputStream; import java.net. inetSocketAddress; import java.net. socket; import java.net. socketAddress;/*** Created by YUAN on 2016-09-17. */public class TalkClient4Byte {private Socket socket; private SocketAddress address; public TalkClient4Byte () {try {socket = new Soc Ket (); address = new InetSocketAddress ("127.0.0.1", 5020); socket. connect (address, 1000);} catch (IOException e) {e. printStackTrace () ;}} public void talk () {try {// use DataInputStream to encapsulate the input stream InputStream OS = new DataInputStream (System. in); byte [] B = new byte [1]; DataOutputStream dos = new DataOutputStream (socket. getOutputStream (); while (-1! = OS. read (B) {dos. write (B); // send it to the client} dos. flush (); dos. close ();} catch (IOException e) {e. printStackTrace ();} finally {try {socket. close () ;}catch (IOException e) {}} public static void main (String [] args) {TalkClient4Byte client = new TalkClient4Byte (); client. talk ();}}

 

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.