课程咨询 :186 8716 1620      qq:2066486918

昆明Java培训 > 达内新闻 > Java socket字节流传输的示例
  • Java socket字节流传输的示例

    发布:昆明Java培训      来源:博客园      时间:2016-09-28

  • 昆明Java培训机构的老师今天给大家讲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();

    }

    }

    学Java开发就到昆明达内Java培训班!了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

    推荐文章

上一篇:java基础盲点梳理

下一篇:【昆明java培训】java反射机制详解

最新开班日期  |  更多

Java--零基础全日制班

Java--零基础全日制班

开班日期:11/30

Java--零基础业余班

Java--零基础业余班

开班日期:11/30

Java--周末提升班

Java--周末提升班

开班日期:11/30

Java--零基础周末班

Java--零基础周末班

开班日期:11/30

  • 网址:http://km .java.tedu.cn      地址:昆明市官渡区春城路62号证券大厦附楼6楼
  • 课程培训电话:186 8716 1620      qq:2066486918    全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56