2

im trying to use the SendFile method https://msdn.microsoft.com/en-us/library/sx0a40c2(v=vs.110).aspx

 TcpClient client;
    private void Form1_Load(object sender, EventArgs e)
    {
        client = new TcpClient();
        client.Connect("10.0.0.1", 10);

        string fileName = @"C:\Users\itapi\Desktop\da.jpg";
        Console.WriteLine("Sending {0} to the host.", fileName);
        client.Client.SendFile(fileName);
    }

server code:

    TcpListener listener;
    TcpClient cl;
    private void Form1_Load(object sender, EventArgs e)
    {
        listener = new TcpListener(IPAddress.Any, 10);
        listener.Start();
        cl = listener.AcceptTcpClient();




    }

my question is: how i am supposed to get the file in the other side? i dont want to use networkstream only pure socket. any help would be apperciated

3 Answers 3

3

The basic recipe for this is:

  • Open connection
  • Read from connection
  • Write to output stream (memory / file / whatever)

Easiest way to do that is to use a NetworkStream.

I dont want to use networkstream only pure socket

Let's for a second look at the internals of Stream.CopyTo:

private void InternalCopyTo(Stream destination, int bufferSize)
{
    int num;
    byte[] buffer = new byte[bufferSize];
    while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
    {
        destination.Write(buffer, 0, num);
    }
}

and NetworkStream.Write:

public override void Write(byte[] buffer, int offset, int size)
{
    // Insert Lots and Lots of checks and safety's here:

    streamSocket.Send(buffer, offset, size, SocketFlags.None);
}

Also note that NetworkStream takes care of proper connection handling, proper closing of your connection, etc.

In short, there's only one good reason you might not want to use NetworkStream and that's: I want to do this completely a-synchronous, because the CopyTo implementation is synchronous. And even that is heavily debatable if you're using async/await. This is applicable for high performance (c.q. lots of connections) in a server environment.

If that's the case, you might want to start with the example code here: https://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx .

Sign up to request clarification or add additional context in comments.

Comments

0

The file is being sent as bytes. There is no header or anything. Just the file bytes.

Probably, you should open the file you want to save this file as using File.Create and use new NetworkStream(mySocket).CopyTo(myFile).

i dont want to use networkstream

That makes it harder. Write a stream copy loop yourself.

Comments

0

In addition to the following code, you must close the sending socket for the while loop to exit. The blockCtr and totalReadBytes are diagnostic only.

    private void btnReceive_Click(object sender, EventArgs e)
    {
        const int arSize = 100;
        byte[] buffer = new byte[arSize];

        string outPath = @"D:\temp\rxFile.jpg";
        Stream strm = new FileStream(outPath, FileMode.CreateNew);

        try
        {
            listener = new TcpListener(IPAddress.Any, 10);
            listener.Start();
            cl = listener.AcceptTcpClient();
            SocketError errorCode;
            int readBytes = -1;

            int blockCtr = 0;
            int totalReadBytes = 0;
            while (readBytes != 0)
            {
                readBytes = cl.Client.Receive(buffer, 0, arSize, SocketFlags.None, out errorCode);
                blockCtr++;
                totalReadBytes += readBytes;
                strm.Write(buffer, 0, readBytes);
            }
            strm.Close();

            MessageBox.Show("Received: " + totalReadBytes.ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.