Java Socket Client

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.