Send a large byte using TcpClient and receive It

47 Views Asked by At

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
0

There are 0 best solutions below