English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Este artículo comparte un ejemplo de transmisión de bytes de flujo socket en Java, a modo de referencia, el contenido específico es el siguiente
servidor del lado del servidor:
package com.yuan.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Created by YUAN on 2016-09-17. */ public class TalkServer4Byte { private ServerSocket server; private int puerto = 5020; public TalkServer4Byte() { socket.close(); server = new ServerSocket(puerto); } } } InputStream os = new DataInputStream(System.in); System.out.println("Puerto en monitoreo:"); + puerto); Socket socket = null; while (true) { socket.close(); // espera bloqueante, se crea una nueva instancia de conexión cada vez que se recibe una solicitud socket = server.accept(); System.out.println("Dirección del cliente conectado:"); + socket.getRemoteSocketAddress()); // BufferedReader encapsulado en flujo decorativo (recibe el flujo del cliente) BufferedInputStream bis = new BufferedInputStream( socket.getInputStream()); DataInputStream dis = new DataInputStream(bis); byte[] bytes = new byte[1while ( // lectura de un byte a la vez String ret = ""; while (dis.read(bytes) != -1) { ret += bytesToHexString(bytes) + " "; if (dis.available() == 0) { //una solicitud doSomething(ret); } } } System.out.println(e.getMessage()); try { socket.close(); catch (IOException e) { } System.out.println(e.getMessage()); } } } } public static void doSomething(String ret) { System.out.println(ret); } public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } public static String BytesHexString(byte[] b) { String ret = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } public static void main(String[] args) { TalkServer4Byte server = new TalkServer4Byte(); server.talk(); } }
客户端client代码:
package com.yuan.socket; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; /** * Created by YUAN on 2016-09-17. */ public class TalkClient4Byte { private Socket socket; private SocketAddress address; public TalkClient4Byte() { socket.close(); socket = new Socket(); "1270);1conectarse a 50200); public void talk() { 10usar DataInputStream para envolver el flujo de entrada } } } } InputStream os = new DataInputStream(System.in); socket.close(); //byte [] b = new byte[ ]; DataOutputStream dos = new DataOutputStream(socket.getOutputStream());1while ( != os.read(b)) { dos.write(b);-1 enviar a cliente dos.flush(); // dos.close(); } e.printStackTrace(); finally { } } try { socket.close(); catch (IOException e) { } } } } public static void main(String[] args) { TalkClient4Byte client = new TalkClient4Byte(); client.talk(); } }
Este es el contenido completo del artículo. Esperamos que haya ayudado a su aprendizaje y que todos los amigos nos apoyen en los tutoriales de gritos.
Declaración: El contenido de este artículo se ha obtenido de la red, y los derechos de autor pertenecen a los propietarios originales. El contenido ha sido contribuido y subido por los usuarios de Internet de manera voluntaria. Este sitio no posee los derechos de propiedad, no ha sido editado por humanos y no asume responsabilidad alguna por las leyes de derechos de autor. Si encuentra contenido sospechoso de copyright, le invitamos a enviar un correo electrónico a: notice#oldtoolbag.com (al enviar un correo electrónico, por favor reemplace # con @) para denunciar y proporcionar evidencia relevante. Una vez confirmado, este sitio eliminará inmediatamente el contenido sospechoso de infracción.