Artikel ini berbagi contoh streaming byte soket java untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Sisi server:
Paket com.yuan.socket; import java.io.*; import java.net.serversocket; import java.net.socket;/*** dibuat oleh yuan pada 2016-09-17. */Public Class TalkServer4Byte {Private Serversocket Server; Private int port = 5020; public talkServer4byte () {coba {server = new serversocket (port); } catch (ioException e) {}} public void talk () {System.out.println ("Monitor Port:" + port); Soket soket = null; while (true) {coba {// memblokir dan menunggu, membuat instance koneksi baru untuk setiap permintaan yang diterima socket = server.accept (); System.out.println ("Hubungkan Alamat Klien:" + Socket.GetRemotesocketAddress ()); // aliran dekoratif BufferedReader merangkum aliran input (terima aliran klien) bufferedInputStream bis = baru bufferedInputStream (socket.getInputStream ()); DataInputStream dis = new DatalputStream (BIS); byte [] bytes = byte baru [1]; // Baca satu byte pada waktu ret = ""; while (dis.read (bytes)! = -1) {ret + = bytestoHexString (bytes) + ""; if (dis.available () == 0) {// permintaan dosomething (ret); }}} catch (ioException e) {System.out.println (e.getMessage ()); } akhirnya {coba {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; } untuk (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 = ""; untuk (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 (); }} Kode Klien Klien:
Paket com.yuan.socket; impor java.io.datainputstream; impor java.io.dataoutputStream; impor java.io.ioException; impor java.io.inputstream; impor java.net.inputsocketaddress; impor java.net.socket; 2016-09-17. */kelas publik TalkClient4Byte {soket soket pribadi; alamat socketaddress pribadi; public talkClient4ByTe () {coba {socket = socket baru (); alamat = inetsocketAddress baru ("127.0.0.1", 5020); socket.connect (alamat, 1000); } catch (ioException e) {e.printstacktrace (); }} public void talk () {coba {// Gunakan DataInputStream untuk merangkum input aliran inputStream os = new datalputStream (System.in); byte [] b = byte baru [1]; DataOutputStream dos = DataOutputStream baru (socket.getoutputStream ()); while (-1! = os.read (b)) {dos.write (b); // kirim ke klien} dos.flush (); dos.close (); } catch (ioException e) {e.printstacktrace (); } akhirnya {coba {socket.close (); } catch (ioException e) {}}} public static void main (string [] args) {talkClient4Byte client = new talkClient4byte (); client.talk (); }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.