0

I have been looking around the questions, but those are all for files.

My question is:

How can I Send/Receive a large byte (Approx 1-2MB) in one connection?

Client:

TcpClient tcpclnt = new TcpClient();
var result = tcpclnt.BeginConnect(User.IP, User.Port, null, null);

var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));
if (!success)
{
    return "Failed to connect!";
}

Stream stm = tcpclnt.GetStream();

UTF8Encoding asen = new UTF8Encoding();

msg = Encrypter.EncryptData(msg);

byte[] ba = asen.GetBytes(msg);

stm.Write(ba, 0, ba.Length); // Switch this to?

Server:

System.Net.Sockets.Socket s = myList.AcceptSocket();
byte[] b = new byte[3000000];
int k = s.Receive(b); // Switch this to?
string message = Encoding.UTF8.GetString(b, 0, k);

SOLUTION:

Server code, took me a little while to make it cool:

System.Net.Sockets.Socket s = myList.AcceptSocket(); // Accept the connection

Stream stream = new NetworkStream(s); // Create the stream object
byte[] leng = new byte[4]; // We will put the length of the upcoming message in a 4 length array
int k2 = s.Receive(leng); // Receive the upcoming message length
if (BitConverter.IsLittleEndian)
{
    Array.Reverse(leng);
}
int upcominglength = (BitConverter.ToInt32(leng, 0)); // Convert it to int

byte[] b = ByteReader(upcominglength, stream); // Create the space for the bigger message, read all bytes until the received length!

string message = Encoding.UTF8.GetString(b, 0, b.Length); // Convert it to string!


internal byte[] ByteReader(int length, Stream stream)
{
    byte[] data = new byte[length];
    using (MemoryStream ms = new MemoryStream())
    {
        int numBytesRead;
        int numBytesReadsofar = 0;
        while (true)
        {
            numBytesRead = stream.Read(data, 0, data.Length);
            numBytesReadsofar += numBytesRead;
            ms.Write(data, 0, numBytesRead);
            if (numBytesReadsofar == length)
            {
                break;
            }
        }
        return ms.ToArray();
    }
}

Client code, and it is working nicely!:

var result = tcpclnt.BeginConnect(User.IP, User.Port, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3)); // Connect with timeout

if (!success)
{
    return "Failed to connect!";
}
Stream stm = tcpclnt.GetStream(); // get the stream

UTF8Encoding asen = new UTF8Encoding();

byte[] ba = asen.GetBytes(msg); // get the bytes of the message we are sending
byte[] intBytes = BitConverter.GetBytes(ba.Length); // Get the length of that in bytes
if (BitConverter.IsLittleEndian)
{
    Array.Reverse(intBytes);
}

stm.Write(intBytes, 0, intBytes.Length); // Write the length in the stream!
stm.Flush(); // Clear the buffer!
stm.Write(ba, 0, ba.Length); // Write the message we are sending!

// If we have answers....
byte[] bb = new byte[10000];
int k = stm.Read(bb, 0, 10000);
string mmessage = Encoding.UTF8.GetString(bb, 0, k);
// If we have answers....


tcpclnt.Close(); // Close the socket
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
DreTaX
  • 760
  • 2
  • 9
  • 22
  • 1
    What's the problem with your code? – Botond Botos Dec 22 '16 at 17:06
  • 1
    Most likely the answer to http://stackoverflow.com/questions/10582645/tcpclient-receiving-data-incorrectly?rq=1 is the solution. You can't just receive exactly `n` bytes, because of packet fragmentation. You need to make multiple reads until you've read the expected number of bytes. Also, your sender will have to tell the receiver how many bytes to expect. – Jim Mischel Dec 22 '16 at 17:12
  • So I would do a while and do s.Receive(b); until i read all the bytes? – DreTaX Dec 22 '16 at 17:48

0 Answers0