A socket server was made with tornado. But the staff of the joint adjustment is not on the docking.
So the following demo
Import Java.io.*;import java.net.*;p ublic class sockettest{Sockettest () {} void Test () {try{S Ocket requestsocket = new Socket ("xxx.xxx.xxx.xxx", 60006); OutputStream out = Requestsocket.getoutputstream (); InputStream in = Requestsocket.getinputstream (); byte[] bb = new BYTE[16]; for (int i =0;i<13;i++) {bb[i+3]= (byte) ((int) ' a ' +i); } bb[0]= (byte) 0x00; Bb[1]= (byte) 0x0e; Bb[2]= (byte) 0xaa; Bb[15]= (byte) 0xFE; System.out.println ("client>" +new String (BB)); Out.write (BB); Out.flush (); byte[] buffer = new byte[1024]; In.read (buffer); String responsestr = new string (buffer); System.out.println ("server>" +responsestr); Out.close (); In.close (); Requestsocket.close (); } catch (IOException IOException) { Ioexception.printstacktrace (); }//catch (ClassNotFoundException Classnot) {//System.err.println ("Data received in unknown format"); }} public static void Main (String args[]) {sockettest client = new Sockettest (); Client.test (); }}
Both:
1. Details of the compilation execution under Linux:
Javac Sockettest.java
Java sockettest (Do not Java Sockettest.class, or a lot of misleading add trouble)
2. Byte operation, in Java with a byte array, do not worry (128~255) overflow. You can assign values directly.
byte B = (byte) 254;
3. Get the original input and output stream from the socket, and do not use other packages, otherwise there will be strange things.
OutputStream out = Requestsocket.getoutputstream ();
InputStream in = Requestsocket.getinputstream ();
Java Socket Client