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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀