why android only receive 2896 bytes on c# tcp socket?

Oceansblue

c# Use tcp socket Send message to Android:

string data = "my message....";
byte[] msg = Encoding.UTF8.GetBytes(data);
//for example msg Length is 5210 bytes
client.socket.SendBufferSize = 500000;
socket.Send(msg, msg.Length, SocketFlags.None);

Android receive message from c# server-side:

    socket = new Socket(ServerIP, ServerPort);
    socket.setReceiveBufferSize(500000);
    isReceive = true;
    receiveThread = new ReceiveThread(socket);
    receiveThread.start();

private class ReceiveThread extends Thread{
private InputStream inStream = null;
ReceiveThread(Socket socket){
inStream = socket.getInputStream();
}
@Override
public void run(){
while(isReceive){
byte[] buffer = new byte[99999];
try {
//only receive 2896 bytes?
int size = inStream.read(buffer);
} catch (IOException e) {
unConnSocket();
}
}
}
}

why the size only receive 2896 bytes?

Remy Lebeau

Your Android code has no way of knowing how many bytes the C# code is sending. inStream.read() is reading only the bytes that are currently available on the socket at that moment. You should have the C# code send the string length before sending the string data, so that the Android code knows how many bytes to expect, eg:

string data = "my message....";
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
int dataLen = IPAddress.HostToNetworkOrder(dataBytes.Length);
byte[] dataLenBytes = BitConverter.GetBytes(dataLen);
socket.Send(dataLenBytes);
socket.Send(dataBytes);

private class ReceiveThread extends Thread
{
    private DataInputStream inStream = null;

    ReceiveThread(Socket socket)
    {
        inStream = new DataInputStream(socket.getInputStream());
    }

    @Override
    public void run()
    {
        while (isReceive)
        {
            try
            {
                String s;
                int size = inStream.readInt();
                if (size > 0)
                {
                    byte[] buffer = new byte[size];
                    inStream.readFully(buffer); 
                    s = new String(buffer, "UTF-8");
                }
                else
                    s = "";

                // use s as needed ...
            }
            catch (IOException e)
            {
                unConnSocket();
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android TCP socket doesn't receive data

From Dev

C++ , TCP Socket cannot receive data

From Dev

Receive acknowledgement from TCP Socket

From Dev

Is connect() necessary if I want only to receive data in C/TCP?

From Dev

Android socket receive data

From Dev

C++ | TCP - receive

From Dev

conect android(socket client) with c++(socket server) via TCP

From Dev

TCP Socket send/receive always waiting

From Dev

Java tcp socket does not receive properly

From Dev

How to receive tcp flags in a socket connection

From Dev

C# Socket.receive continuously receives 0 bytes and does not block in the loop

From Dev

C# Socket.receive continuously receives 0 bytes and does not block in the loop

From Dev

Android TCP Socket Error

From Dev

How to dispatch send/receive data using C# socket tcp connection

From Dev

TCP listener only receive the first message

From Dev

NamedPipeServerStream receive MAX=1024 bytes, why?

From Dev

Why the TCP Server does't receive any data from client in Android?

From Dev

What is the best way to receive a capped number of bytes from a socket?

From Dev

boost::asio::async_receive and 0 bytes in socket

From Dev

socket is not connected Android to pc tcp

From Dev

Why I do not correctly receive non-plain-text files in tcp client/server written in C?

From Dev

Can I receive more bytes in one transfer from this c# to android device?

From Dev

Does TCP socket receive data in the same way as it was sent?

From Dev

spring -integration TCP gateway, connect and receive data from Socket

From Dev

Does TCP socket receive data in the same way as it was sent?

From Dev

Cpp socket - TCP transmission (send and then receive with the same port)

From Dev

spring -integration TCP gateway, connect and receive data from Socket

From Dev

TCP blocking socket - possibility of recv small amount of bytes in different packets

From Dev

Is it possible to read the number of bytes buffered for a TCP write socket?

Related Related

  1. 1

    Android TCP socket doesn't receive data

  2. 2

    C++ , TCP Socket cannot receive data

  3. 3

    Receive acknowledgement from TCP Socket

  4. 4

    Is connect() necessary if I want only to receive data in C/TCP?

  5. 5

    Android socket receive data

  6. 6

    C++ | TCP - receive

  7. 7

    conect android(socket client) with c++(socket server) via TCP

  8. 8

    TCP Socket send/receive always waiting

  9. 9

    Java tcp socket does not receive properly

  10. 10

    How to receive tcp flags in a socket connection

  11. 11

    C# Socket.receive continuously receives 0 bytes and does not block in the loop

  12. 12

    C# Socket.receive continuously receives 0 bytes and does not block in the loop

  13. 13

    Android TCP Socket Error

  14. 14

    How to dispatch send/receive data using C# socket tcp connection

  15. 15

    TCP listener only receive the first message

  16. 16

    NamedPipeServerStream receive MAX=1024 bytes, why?

  17. 17

    Why the TCP Server does't receive any data from client in Android?

  18. 18

    What is the best way to receive a capped number of bytes from a socket?

  19. 19

    boost::asio::async_receive and 0 bytes in socket

  20. 20

    socket is not connected Android to pc tcp

  21. 21

    Why I do not correctly receive non-plain-text files in tcp client/server written in C?

  22. 22

    Can I receive more bytes in one transfer from this c# to android device?

  23. 23

    Does TCP socket receive data in the same way as it was sent?

  24. 24

    spring -integration TCP gateway, connect and receive data from Socket

  25. 25

    Does TCP socket receive data in the same way as it was sent?

  26. 26

    Cpp socket - TCP transmission (send and then receive with the same port)

  27. 27

    spring -integration TCP gateway, connect and receive data from Socket

  28. 28

    TCP blocking socket - possibility of recv small amount of bytes in different packets

  29. 29

    Is it possible to read the number of bytes buffered for a TCP write socket?

HotTag

Archive