服务器之家

服务器之家 > 正文

Java socket字节流传输示例解析

时间:2020-06-16 11:00     来源/作者:fomeiherz

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

服务端server端: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();
  }
 
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
返回顶部