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

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)发送心跳包给服务端。
 
 
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码莎拉蒂 .

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值