zoukankan      html  css  js  c++  java
  • java socket client

    用tornado做了个socket server。无奈联调的人员对接不上。

    于是撸出了以下demo

    import java.io.*;
    import java.net.*;
    
    public class SocketTest{
        SocketTest(){}
        void test()
        {
            try{
                Socket 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();
        }
    }
    

    既要:

    1. linux 下编译执行的细节:

        javac SocketTest.java

        java SocketTest  (不要 java SocketTest.class, 不然一大堆误导加麻烦)

    2. 字节操作,在java里面用byte数组,不要担心(128~255)溢出。直接赋值即可。

      byte b = (byte)254;

    3. 从socket得到原始的输入输出流,不要再用其他包裹,否则会出现诡异的事情。

      OutputStream out = requestSocket.getOutputStream();

      InputStream in = requestSocket.getInputStream();

  • 相关阅读:
    win7下iis中配置php.ini文件
    editplus快捷键大全
    树和二叉树的存储结构的实现(C/C++实现)
    CTF---编程入门第一题 循环
    CTF---Web入门第九题 FALSE
    CTF---Web入门第八题 Guess Next Session
    CTF---Web入门第七题 猫抓老鼠
    CTF---Web入门第六题 因缺思汀的绕过
    CTF---Web入门第五题 貌似有点难
    CTF---Web入门第四题 Forms
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/5342213.html
Copyright © 2011-2022 走看看