简体   繁体   中英

golang server sending byte array to Android

I have met some issue with Java socket and golang. I am trying to develop a golang server that sends/receives byte array to an android client. now the android client can send byte array to go server but cannot receive anything from go server. I have attached the code below.

The code got stuck when reach in.read(); I tried in.available() to see how many bytes are in the inputstream. it always shows 0:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button regButton = (Button) findViewById(R.id.regbut);
    msg = (TextView) findViewById(R.id.msg);

    regButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    byte[] array;
                    BigInteger mykey = mykey(publicKey);
                    System.out.println(mykey);
                    BigInteger rawkey = generater.modPow(mykey,publicKey);
                    BigInteger serverRawKey;
                    System.out.println(rawkey);
                    array = rawkey.toByteArray();
                    System.out.println(rawkey.bitCount());
                    try{
                        Socket con = new Socket("149.166.134.55",9999);
                        OutputStream out = con.getOutputStream();
                        InputStream in = con.getInputStream();
                        DataOutputStream dOut = new DataOutputStream(out);
                        //DataInputStream din = new DataInputStream(in);
                        byte[] data = new byte[10];
                        dOut.write(array);
                        TimeUnit.SECONDS.sleep(10);
                        System.out.println(in.available());
                        in.read(data);
                        con.close();
                        }catch (Exception e){
                        System.out.println(e);
                       }
                   }
               }).start();
           }
        });
         }

here is the go code. if i remove in.read(); in java everything works just fine. but it pauses when I add in.read();

var publickey *big.Int

func ClientListen(port string) {
    ln, err := net.Listen("tcp", port)
    if err != nil {
        fmt.Println("error\n")
        fmt.Println(err)
        return
    }
    for {
        nc, err := ln.Accept()
        if err != nil {
            fmt.Println(err)
            continue
        }
        go recivemsg(nc)
    }
}

func recivemsg(nc net.Conn) {
    publickey = big.NewInt(15485863)
    var msg []byte
    var buf bytes.Buffer
    rawkey := big.NewInt(0)
    io.Copy(&buf, nc)
    fmt.Println("total size:", buf.Len())
    msg = buf.Bytes()
    rawkey = rawkey.SetBytes(msg)
    fmt.Println(msg, "    ", rawkey)
    r := rand.New(rand.NewSource(99))
    myraw := big.NewInt(0)
    myraw = myraw.Rand(r, publickey)
    fmt.Println(myraw)
    newmsg := myraw.Bytes()
    nc.Write(newmsg)
    nc.Close()
    fmt.Println(nc.RemoteAddr())

}

func main() {
    ClientListen(":9999")
}

thanks guys for taking your time to read my question

io.Copy(&buf, nc) on Go side will continue to read from the connection's input stream until it's closed. On Java side, you try to read from the connection's input stream, but Go side is still waiting for further input, since Java is still keeping its output steam (Go's input stream) open.

Solution: on Java side, close the output stream using the socket's shutdownOutput() method after you are done writing to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM