Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';套接字是否会发送最后一条消息,除非之前有延迟?_Java_Android_Sockets - Fatal编程技术网

Java 为什么';套接字是否会发送最后一条消息,除非之前有延迟?

Java 为什么';套接字是否会发送最后一条消息,除非之前有延迟?,java,android,sockets,Java,Android,Sockets,好的,我正在看这段代码,它应该得到一个表示图像的字节数组,并将其逐段发送到服务器。当图像传输完成时,需要告知服务器,此结束消息为“关闭”。一切正常,但除非我取消对Thread.sleep的注释,否则不会发送结束消息。此外,由于某些原因,延迟需要相当大,例如100毫秒不起作用。如果有人能解释这种行为,我将不胜感激,因为我对java不是很了解 private class NetTask extends AsyncTask<Void, Void, Void> { private S

好的,我正在看这段代码,它应该得到一个表示图像的字节数组,并将其逐段发送到服务器。当图像传输完成时,需要告知服务器,此结束消息为“关闭”。一切正常,但除非我取消对Thread.sleep的注释,否则不会发送结束消息。此外,由于某些原因,延迟需要相当大,例如100毫秒不起作用。如果有人能解释这种行为,我将不胜感激,因为我对java不是很了解

private class NetTask extends AsyncTask<Void, Void, Void>
{
    private String ip;
    private byte[] to_send;

    public NetTask(String ip, byte[] to_send)
    {
        this.ip = ip;
        this.to_send = to_send;
    }

    @Override
    protected Void doInBackground(Void...params)
    {
        try {
            Log.i(dTag, "" + to_send.length);
            Socket sck = new Socket(ip, 1234);
            DataOutputStream dOut = new DataOutputStream(sck.getOutputStream());
            dOut.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(to_send.length).array());
            Log.d(dTag, "" + to_send.length);
            int x = 500;
            int len = to_send.length;
            for (int i = 0; i < len - x + 1; i += x)
                dOut.write(Arrays.copyOfRange(to_send, i, i + x));

            if (len % x != 0)
                dOut.write(Arrays.copyOfRange(to_send, len - len % x, len));
            /*try {
                Thread.sleep(1000);
            }
            catch (Exception ex) {
                Log.d(dTag, "thread sleep error");
            }*/
            dOut.write("CLOSE".getBytes());
            dOut.flush();
            dOut.close();
            sck.close();
        }
        catch (IOException ex) {
            Log.d(dTag, ex.getMessage());
        }
        return null;
    }
}
私有类NetTask扩展异步任务
{
私有字符串ip;
要发送的专用字节[];
公共网络任务(字符串ip,字节[]发送)
{
this.ip=ip;
this.to_send=to_send;
}
@凌驾
受保护的Void doInBackground(Void…参数)
{
试一试{
Log.i(dTag,“+to_send.length);
插座sck=新插座(ip,1234);
DataOutputStream dOut=新的DataOutputStream(sck.getOutputStream());
dOut.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putin(to_send.length.array());
Log.d(dTag,“+to_send.length);
int x=500;
int len=发送长度;
对于(int i=0;i
服务器是c#,下面是代码:

while (ok)
{
    sck.Listen(1000);
    Socket accepted = sck.Accept();
    buffer = new byte[accepted.SendBufferSize];
    int bytesRead = -1;
    bool reading = true;
    int im_size = -1;
    int index = 0;
    byte[] image = null;
    while (reading)
    {
        bytesRead = accepted.Receive(buffer);
        if (bytesRead == 5)
            Console.WriteLine(bytesRead);
        string strData = "YADA";
        byte[] formatted = new byte[bytesRead];
        if (bytesRead == 5)
        {
            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = buffer[i];
            }
            strData = Encoding.ASCII.GetString(formatted);
        }
        if (strData == "CLOSE")
        {
            Console.WriteLine("GOT CLOSE MESSAGE");
            Image im = Image.FromStream(new MemoryStream(image));
            im.Save(@"D:\im1.bmp");
        }
        else
        {
            if (im_size == -1)
            {
                im_size = BitConverter.ToInt32(buffer, 0);
                image = new byte[im_size];
                Console.WriteLine(im_size);
            }
            else
            {
                for (int i = 0; i < bytesRead && index < im_size; i++)
                {
                    image[index++] = buffer[i];
                }
            }
        }
    }
    accepted.Close();
}
while(正常)
{
听(1000);
接受套接字=sck.Accept();
buffer=新字节[accepted.SendBufferSize];
int字节读取=-1;
布尔读数=真;
int im_size=-1;
int指数=0;
字节[]图像=空;
边读
{
bytesRead=已接受。接收(缓冲区);
如果(字节读==5)
控制台写入线(字节读取);
字符串strData=“YADA”;
字节[]格式化=新字节[字节读取];
如果(字节读==5)
{
for(int i=0;i
真奇怪。你也有读取套接字的服务器代码吗?有一个邪恶的魔法选项--
TCP_NODELAY
:)可能与此有关。@Markus服务器代码是c,但我马上就发布了。你应该试试@bayou.io的建议试试TCP_NODELAY,它像个符咒一样工作,我不知道为什么,谢谢!真奇怪。你也有读取套接字的服务器代码吗?有一个邪恶的魔法选项--
TCP_NODELAY
:)可能与此有关。@Markus服务器代码是c,但我马上就发布了。你应该试试@bayou.io的建议试试TCP_NODELAY,它像个符咒一样工作,我不知道为什么,谢谢!