java之socket的OOBInline和UrgentData和发送心跳包研究
生活随笔
收集整理的這篇文章主要介紹了
java之socket的OOBInline和UrgentData和发送心跳包研究
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
UrgentData可以理解為緊急發(fā)送數(shù)據(jù)方式,如果我們客戶端先用write方法寫入數(shù)據(jù),再用UrgentData發(fā)送數(shù)據(jù),再去執(zhí)行flush操作,我們可以得到服務(wù)端先打印UrgentData發(fā)送的數(shù)據(jù),然后再打印write寫入的數(shù)據(jù)。
客戶端代碼實現(xiàn):
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); // 向服務(wù)器發(fā)送字符"C"outWriter.write("hello world\r\n");socket.sendUrgentData(65); // 向服務(wù)器發(fā)送字符"A"socket.sendUrgentData(322); // 向服務(wù)器發(fā)送字符"B"outWriter.flush();socket.sendUrgentData(214); // 向服務(wù)器發(fā)送漢字”中”socket.sendUrgentData(208);socket.sendUrgentData(185); // 向服務(wù)器發(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");}} } ?服務(wù)端代碼實現(xiàn):
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("服務(wù)器已經(jīng)啟動,端口號: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");}}}}} } ?運行結(jié)果(先運行服務(wù)端,后運行客戶端):
服務(wù)器已經(jīng)啟動,端口號:1234 ABChello world 中國 說明使用sendUrgentData方法發(fā)送數(shù)據(jù)后,系統(tǒng)會立即將這些數(shù)據(jù)發(fā)送出去;而使用write發(fā)送數(shù)據(jù),必須要使用flush方法才會真正發(fā)送數(shù)據(jù)。 在使用setOOBInline方法打開SO_OOBINLINE選項時要注意是必須在客戶端和服務(wù)端程序同時使用setOOBInline方法打開這個選項,否則無法命名用sendUrgentData來發(fā)送數(shù)據(jù)。
總結(jié):
我們還可以通過socket.sendUrgentData(0xff);來檢測是否與服務(wù)端連通,和ping IP 效果差不多,其它的socket.isConnected() socket.isOutputShutdown()都是本地檢測,我們上面socket發(fā)送數(shù)據(jù),如果在安卓客戶端,我們可以用這個來發(fā)送心跳包,
類似上面客戶端的代碼,通過后臺下發(fā)的IP和端口配置,開啟線程,out.write(data),通過handler.postDelay(Runable, delayTime)發(fā)送心跳包給服務(wù)端。
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java之socket的OOBInline和UrgentData和发送心跳包研究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串之统计字符串
- 下一篇: 字符串之从规定的字符串中根据下标得到字符