A complete method of reading sockets in bytes

xiaoxiao2021-03-06  14

If there is a situation in Socket, you can't use, the general method reads the byte in the socket.

Socket socket = new Socket (ip, port); socket.setSendBufferSize (1);. Socket.getOutputStream () write ( "nihaoddddddddddddddd" .getBytes ());. Socket.getOutputStream () flush (); socket.getOutputStream () .write ("dajiahao" .Getbytes ()); socket.getOutputStream (). Flush (); socket.getOutputStream (). Write ("222" .getBytes ()); socket.getOutputStream (). Flush ();

Socket.getOutputStream (). Close ();

You can read the following method

public static byte [] readSocket (Socket socket) throws Exception {ArrayList l = new ArrayList (); InputStream out = socket.getInputStream (); int totallen = 0; int count; / ** * If the socket has been closed the other socket, Read the contents of the socket again in Socket back to count as a negative number;

* Read the socket timeout, reversely read bytes * / for (;;) {byte [] TMP = new byte [3000]; try {//system.err.println (out.available ()); count = OUT.READ (TMP); System.err.Println ("Count" count);} catch (throwable e) {E.PrintStackTrace (); system.rr.println ("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd); .println ("count" count); if (count <= 0) {Break;} Totallen = count; Byte [] TMP2 = new byte [count]; system.Arraycopy (TMP, 0, TMP2, 0, Count) } Byte [] bs = new byte [TOTALLEN]; int POS = 0; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-49192.html

New Post(0)