Este artigo compartilha um exemplo de streaming de bytes de soquete Java para sua referência. O conteúdo específico é o seguinte
Lado do servidor:
Pacote com.yuan.socket; importar java.io. */public class TalkServer4Byte {private ServerSocket Server; private int porta = 5020; public TalkServer4Byte () {try {server = new ServerSocket (porta); } catch (ioexception e) {}} public void Talk () {System.out.println ("Porta do monitor:" + porta); Soquete soquete = nulo; while (true) {tente {// bloqueando e aguardando, criando uma nova instância de conexão para cada solicitação recebida soquete = server.accept (); System.out.println ("Connect o endereço do cliente:" + soket.getRemoteSocketAddress ()); // Decorative Stream BufferReader encapsula o fluxo de entrada (Receba Stream Cliente) BufferInputStream bis = new BufferInputStream (Socket.getInputStream ()); DatainputStream DIS = new DatainputStream (BIS); byte [] bytes = novo byte [1]; // leia um byte de cada vez ret = ""; while (Dis.read (bytes)! = -1) {ret + = bytestoHexString (bytes) + ""; if (Dis.Available () == 0) {// A solicitação doSomething (ret); }}} catch (ioexception e) {System.out.println (e.getMessage ()); } finalmente {tente {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; 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] e 0xff); if (hex.Length () == 1) {hex = '0' + hex; } ret += hex.touppercase (); } retornar ret; } public static void main (string [] args) {talkServer4byte server = new TalkServer4Byte (); server.Talk (); }} Código do cliente do cliente:
pacote com.yuan.socket; importar java.io.datainputStream; importar java.io.dataoutputStream; importar java.io.ioException; importar java.io.inputStream; importe java.net.inputsocketaddress; importelefress; 2016-09-17. */public class talkclient4byte {soquete privado; endereço de soquete privado; public TalkClient4Byte () {Try {Socket = new Socket (); endereço = novo inetSocketAddress ("127.0.0.1", 5020); Socket.Connect (endereço, 1000); } catch (ioexception e) {e.printStackTrace (); }} public void Talk () {tente {// Use DatainputStream para encapsular o fluxo de entrada inputStream OS = new DatainputStream (System.in); byte [] b = novo byte [1]; DataOutputStream DOS = new DataOutputStream (Socket.getOutputStream ()); while (-1! = os.read (b)) {dos.Write (b); // envie para o cliente} dos.flush (); dos.close (); } catch (ioexception e) {e.printStackTrace (); } finalmente {tente {socket.close (); } catch (ioexception e) {}}} public static void main (string [] args) {talkclient4byte client = new TalkClient4Byte (); client.Talk (); }}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.