duanji1899 2016-11-02 18:53
浏览 91
已采纳

golang服务器将字节数组发送到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
")
        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

  • 写回答

1条回答 默认 最新

  • dqmchw0071 2016-11-03 07:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?