Este artículo comparte un ejemplo de transmisión de bytes de socket Java para su referencia. El contenido específico es el siguiente
Lado del servidor:
paquete com.yuan.socket; import java.io.*; import java.net.serversocket; import java.net.socket;/*** Creado por Yuan el 2016-09-17. */Public Class TalkServer4Byte {Servidor privado Serversocket; Puerto privado int = 5020; public talkServer4byte () {try {server = new Serversocket (puerto); } catch (ioException e) {}} public void talk () {System.out.println ("Puerto de monitor:" + puerto); Socket de socket = nulo; while (true) {try {// bloqueando y esperando, creando una nueva instancia de conexión para cada solicitud recibida Socket = Server.accept (); System.out.println ("Conecte la dirección del cliente:" + Socket.getRemotesocketAddress ()); // La corriente decorativa BufferedReader encapsula la secuencia de entrada (RECEENTA CLIENTE DEL CLIENTE) BUFFEREDInputStream BIS = new BufferedInputStream (Socket.GetInputStream ()); DataInputStream dis = new DataInputStream (BIS); byte [] bytes = new byte [1]; // Leer un byte a la vez retir = ""; while (dis.read (bytes)! = -1) {ret + = bytestoHexString (bytes) + ""; if (dis.available () == 0) {// a una solicitud Dosomething (ret); }}} Catch (IOException e) {System.out.println (E.GetMessage ()); } finalmente {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; } para (int i = 0; i <src.length; i ++) {int v = src [i] & 0xff; Cadena hv = integer.toHexString (v); if (hv.length () <2) {stringBuilder.append (0); } stringBuilder.append (HV); } return StringBuilder.ToString (); } public static string bytesShexString (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 (); servidor.talk (); }} Código del cliente del cliente:
paquete com.yuan.socket; import java.io.datainputstream; import java.io.dataOutputStream; import java.io.ioexception; import java.io.inputstream; import java.net.inputSocketadDress; import java.net.net.socket; import java.net.socketddress;/* *** creó el 2016-09-17. */public class TalkClient4byte {private Socket Socket; dirección privada de enchufeaddress; public TalkClient4Byte () {try {socket = new Socket (); dirección = nueva inetSocketAddress ("127.0.0.1", 5020); socket.connect (dirección, 1000); } catch (ioException e) {E.PrintStackTrace (); }} public void talk () {try {// use DataInputStream para encapsular el flujo de entrada inputStream OS = new DataInputStream (System.in); byte [] b = nuevo byte [1]; DataOutputStream dos = new DataOutputStream (Socket.getOutputStream ()); while (-1! = Os.read (b)) {dos.write (b); // Enviar al cliente} dos.flush (); dos.close (); } catch (ioException e) {E.PrintStackTrace (); } finalmente {try {Socket.close (); } catch (ioException e) {}}} public static void main (string [] args) {talkClient4byte Client = new TalkClient4Byte (); Client.Talk (); }}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.