php装修公司网站源码,徐州建设工程交易网江苏本源,wordpress使用的编程语言,ppt免费模板哪个网站好UrgentData可以理解为紧急发送数据方式#xff0c;如果我们客户端先用write方法写入数据#xff0c;再用UrgentData发送数据#xff0c;再去执行flush操作#xff0c;我们可以得到服务端先打印UrgentData发送的数据#xff0c;然后再打印write写入的数据。
客户端代码实现…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); // 向服务器发送字符CoutWriter.write(hello world\r\n);socket.sendUrgentData(65); // 向服务器发送字符Asocket.sendUrgentData(322); // 向服务器发送字符BoutWriter.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)发送心跳包给服务端。