当前位置: 移动技术网 > IT编程>开发语言>Java > Java socket字节流传输示例解析

Java socket字节流传输示例解析

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文为大家分享了java socket字节流传输示例,供大家参考,具体内容如下

服务端server端: 

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("监控端口:" + port);
    socket socket = null;
    while (true) {
      try {
        // 阻塞等待,每接收到一个请求就创建一个新的连接实例
        socket = server.accept();
        system.out.println("连接客户端地址:" + socket.getremotesocketaddress());

        // 装饰流bufferedreader封装输入流(接收客户端的流)
        bufferedinputstream bis = new bufferedinputstream(
            socket.getinputstream());

        datainputstream dis = new datainputstream(bis);
        byte[] bytes = new byte[1]; // 一次读取一个byte
        string ret = "";
        while (dis.read(bytes) != -1) {
          ret += bytestohexstring(bytes) + " ";
          if (dis.available() == 0) { //一个请求
            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代码:

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 {

      //使用datainputstream封装输入流
      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); // 发送给客户端
      }
      
      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();
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网