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)发送心跳包给服务端。
作者:chen.yu
深信服三年半工作经验,目前就职游戏厂商,希望能和大家交流和学习,
微信公众号:编程入门到秃头 或扫描下面二维码
零基础入门进阶人工智能(链接)