Java Socket Byte Throttling Transport sample parsing _java

Source: Internet
Author: User
Tags getmessage

This article for everyone to share the Java socket byte Throttle Transmission example, for your reference, the specific contents are as follows

Service-Side server end:

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);
    The catch (IOException e) {}} is public void talk () {SYSTEM.OUT.PRINTLN ("Monitoring port:" + port);
    Socket socket = NULL;
        while (true) {try {//block wait, each request creates a new connection instance socket = Server.accept ();

        SYSTEM.OUT.PRINTLN ("Connect client Address:" + socket.getremotesocketaddress ()); The decorative stream BufferedReader encapsulates the input stream (the stream receiving the client) Bufferedinputstream bis = new Bufferedinputstream (socket.getinputs

        Tream ());
        DataInputStream dis = new DataInputStream (bis); byte[] bytes = new Byte[1];
        Reads one byte String ret = "" at a time;
          while (Dis.read (bytes)!=-1) {ret = bytestohexstring (bytes) + ""; if (Dis.available () = = 0) {//a request dosomething (ret);
      A 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-side 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 socket ();
      Address = new Inetsocketaddress ("127.0.0.1", 5020);
    Socket.connect (address, 1000);
    catch (IOException e) {e.printstacktrace (); } public void Talk () {try {////using DataInputStream encapsulation input stream InputStream OS = new DataInputStream (syste
      
      m.in);
      byte [] b = new byte[1];
      DataOutputStream dos = new DataOutputStream (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 void Main (string[] args) {Talkclient4byte client = new
    Talkclient4byte ();
  Client.talk ();
 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.