java之socket的OOBInline和UrgentData和发送心跳包研究

举报
chenyu 发表于 2021/07/26 23:14:43 2021/07/26
3.7k+ 0 0
【摘要】 UrgentData可以理解为紧急发送数据方式,如果我们客户端先用write方法写入数据,再用UrgentData发送数据,再去执行flush操作,我们可以得到服务端先打印UrgentData发送的数据,然后再打印write写入的数据。 客户端代码实现: package com.chenyu.string.cn; import java.io.IOException...

UrgentData可以理解为紧急发送数据方式,如果我们客户端先用write方法写入数据,再用UrgentData发送数据,再去执行flush操作,我们可以得到服务端先打印UrgentData发送的数据,然后再打印write写入的数据。

客户端代码实现:


       package com.chenyu.string.cn;
       import java.io.IOException;
       import java.io.OutputStream;
       import java.io.OutputStreamWriter;
       import java.net.Socket;
       public class ClientTest {
       	public static Socket socket;
       	public static final String LocalHOST = "127.0.0.1";
       	public static final int PORT = 1234;
       	public static void main(String[] args) {
       		Client(LocalHOST, PORT);
       	}
       	public static void Client(String address, int port) {
       		try {
       			socket = new Socket(address, port);
       		} catch (Exception e) {
       			System.out.println("connection reset");
       			return;
       		}
       		if (socket != null && socket.isConnected()) {
       			try {
        socket.setOOBInline(true);
        OutputStream out = socket.getOutputStream();
        OutputStreamWriter outWriter = new OutputStreamWriter(out);
        outWriter.write(67); // 向服务器发送字符"C"
        outWriter.write("hello world\r\n");
        socket.sendUrgentData(65); // 向服务器发送字符"A"
        socket.sendUrgentData(322); // 向服务器发送字符"B"
        outWriter.flush();
        socket.sendUrgentData(214); // 向服务器发送汉字”中”
        socket.sendUrgentData(208);
        socket.sendUrgentData(185); // 向服务器发送汉字”国”
        socket.sendUrgentData(250);
        socket.close();
       			} catch (Exception e) {
        System.out.println("has throw exception");
        e.printStackTrace();
       			} finally {
        try {
        if (socket != null) {
        socket.close();
        }
        } catch (IOException e) {
        System.out.println("socket close fail");
        }
       			}
       		} else {
       			System.out.println("socket is null or socket connect fail");
       		}
       	}
       }
   
  
 

服务端代码实现:


       package com.chenyu.string.cn;
       import java.io.BufferedReader;
       import java.io.IOException;
       import java.io.InputStream;
       import java.io.InputStreamReader;
       import java.net.ServerSocket;
       import java.net.Socket;
       public class TestInline {
       	public static ServerSocket serverSocket;
       	public static Socket socket;
       	public static void main(String[] args) {
       		try {
       			serverSocket = new ServerSocket(1234);
       		} catch (IOException e1) {
       			System.out.println("serverSocket is fail");
       			return;
       		}
       		System.out.println("服务器已经启动,端口号:1234");
       		while (true) {
       			try {
        socket = serverSocket.accept();
        socket.setOOBInline(true);
        InputStream in = socket.getInputStream();
        InputStreamReader inReader = new InputStreamReader(in);
        BufferedReader bReader = new BufferedReader(inReader);
        String result;
        while ((result = bReader.readLine()) != null) {
        System.out.println(result);
        }
       // char [] cha = new char[1024];
       // int len = inReader.read(cha);
       // System.out.println(new String(cha,0,len));
        socket.close();
       			} catch (Exception e){
        System.out.println("read data fail");
       			} finally {
        if (socket != null) {
        try {
        socket.close();
        } catch (IOException e) {
        System.out.println("socket close fail");
        }
        }
       			}
       		}
       	}
       }
   	socket = serverSocket.accept(); socket.setOOBInline(true); InputStream in = socket.getInputStream(); InputStreamReader inReader = new InputStreamReader(in); BufferedReader bReader = new BufferedReader(inReader); String result; while ((result = bReader.readLine()) != null) { System.out.println(result); }
// char [] cha = new char[1024];
// int len = inReader.read(cha);
// System.out.println(new String(cha,0,len)); socket.close(); } catch (Exception e){ System.out.println("read data fail"); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { System.out.println("socket close fail"); } } }
		}
	}
}
  
 

运行结果(先运行服务端,后运行客户端):


       服务器已经启动,端口号:1234
       ABChello world
       中国
   
  
 
说明使用sendUrgentData方法发送数据后,系统会立即将这些数据发送出去;而使用write发送数据,必须要使用flush方法才会真正发送数据。
在使用setOOBInline方法打开SO_OOBINLINE选项时要注意是必须在客户端和服务端程序同时使用setOOBInline方法打开这个选项,否则无法命名用sendUrgentData来发送数据。


总结:

我们还可以通过socket.sendUrgentData(0xff);来检测是否与服务端连通,和ping IP 效果差不多,其它的socket.isConnected() socket.isOutputShutdown()都是本地检测,我们上面socket发送数据,如果在安卓客户端,我们可以用这个来发送心跳包,
类似上面客户端的代码,通过后台下发的IP和端口配置,开启线程,out.write(data),通过handler.postDelay(Runable, delayTime)发送心跳包给服务端。
 
 
 
 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/53413325

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。