java之socket的OOBInline和UrgentData和發(fā)送心跳包研究
UrgentData可以理解為緊急發(fā)送數據方式,如果我們客戶端先用write方法寫入數據,再用UrgentData發(fā)送數據,再去執(zhí)行flush操作,我們可以得到服務端先打印UrgentData發(fā)送的數據,然后再打印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); // 向服務器發(fā)送字符"C"
outWriter.write("hello world\r\n");
socket.sendUrgentData(65); // 向服務器發(fā)送字符"A"
socket.sendUrgentData(322); // 向服務器發(fā)送字符"B"
outWriter.flush();
socket.sendUrgentData(214); // 向服務器發(fā)送漢字”中”
socket.sendUrgentData(208);
socket.sendUrgentData(185); // 向服務器發(fā)送漢字”國”
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方法發(fā)送數據后,系統(tǒng)會立即將這些數據發(fā)送出去;而使用write發(fā)送數據,必須要使用flush方法才會真正發(fā)送數據。
在使用setOOBInline方法打開SO_OOBINLINE選項時要注意是必須在客戶端和服務端程序同時使用setOOBInline方法打開這個選項,否則無法命名用sendUrgentData來發(fā)送數據。
總結:
我們還可以通過socket.sendUrgentData(0xff);來檢測是否與服務端連通,和ping IP 效果差不多,其它的socket.isConnected() socket.isOutputShutdown()都是本地檢測,我們上面socket發(fā)送數據,如果在安卓客戶端,我們可以用這個來發(fā)送心跳包,
類似上面客戶端的代碼,通過后臺下發(fā)的IP和端口配置,開啟線程,out.write(data),通過handler.postDelay(Runable, delayTime)發(fā)送心跳包給服務端。
作者:chen.yu
深信服三年半工作經驗,目前就職游戲廠商,希望能和大家交流和學習,
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎入門進階人工智能(鏈接)