Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4109108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:45:04+00:00 2026-05-20T21:45:04+00:00

I have a large application written using .Net remoting for file transfer. This was

  • 0

I have a large application written using .Net remoting for file transfer. This was borking in some circumstances with the sockets being forcibly closed – I wasn’t using sockets directly, but a .Net remoting call with byte arrays (I wasn’t sending the whole file in one transfer, I was splitting it up).

So, I decided to change the actual file transfer part to use sockets.

As a proof of concept, to see if I got the principles right, I have written a simple console client and a server.

I am using ASynch recieves but synchronous writes – I have tried with both being ASync but same reseult, and keeping it synchronous made debugging easier.

What the apps do (code below) is the server sits and waits for files to be transfered, and it stores them in a directory at a given name.

When enter is pressed, the server then reads the files recieved and sends them back to the clients who store them under a different name. I wanted to test file transfer both ways.

Using one instance of the client application, all is well – the server recieves it and then sends it back to the client. All is well. Yes, the client throws an exception when you terminate the server – but that is fine – I know that the socket was forcibly closed…I can deal with tidying upu the code when it is working.

However, when I create 2 instances of the client code (not forgetting to modify the code slightly to read a different file to send, and also to store the received file under a different name) – the server receives both files from the clients, sends the first one back just fine, and then a few segments into the second file it throws with a “non blocking socket operation could not be completed immediatly” – which is odd because nothing is blocking, and the recieves are async – and the sends are actually blocking!

Any suggestions please as to what I am doing wrong – no doubt it is something stupid, but still…

The aim of the final code is to be able to have n clients contact the server and send files to it, and also, at random intervals have the server send 1 or more files back to some/all of the clients.

Cheers folks!

Server code

    using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace SocketServer
{

    class ConnectionInfo
    {
        public Socket Socket;
        public byte[] Buffer;
        public int client;        
    }



    class Program
    {

        static int chunkSize = 16 * 1024;
        static int chucksizeWithoutHeaderData = chunkSize - 8;
        static List<ConnectionInfo> list = new List<ConnectionInfo>();

        static Socket serverSocket;

        static int nClient = 0;


        static void AcceptCallback(IAsyncResult result)
        {


            ConnectionInfo info = new ConnectionInfo();
            info.Socket = serverSocket.EndAccept(result);

            info.Buffer = new byte[chunkSize];


            Console.WriteLine("Client connected");
            nClient++;
            info.client = nClient;

            list.Add(info);

            info.Socket.BeginReceive(info.Buffer,0,info.Buffer.Length,SocketFlags.None, new AsyncCallback(ReceiveCallBack), info);

            serverSocket.BeginAccept(new AsyncCallback(AcceptCallback),null);

        }

        static void ReceiveCallBack(IAsyncResult result)
        {
            ConnectionInfo info = result.AsyncState as ConnectionInfo;
            try
            {

                Int32 nSegmentNumber = BitConverter.ToInt32(info.Buffer,0);
                Int32 nMaxSegment = BitConverter.ToInt32(info.Buffer,4);

                string strFileName = string.Format(@"c:\temp\from-client-{0}.dat",info.client);

                int bySize = info.Socket.EndReceive(result);
                using (FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate))
                {
                    Console.WriteLine("Received segment {0} of {1} from client {2}", nSegmentNumber, nMaxSegment, info.client);
                    fs.Position = fs.Length;
                    fs.Write(info.Buffer, 8, bySize-8);

                    if (nSegmentNumber >= nMaxSegment)
                    {
                        Console.WriteLine("Completed receipt from client {0}", info.client);
                    }

                }

                info.Socket.BeginReceive(info.Buffer, 0, info.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), info);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }



        static void Main(string[] args)
        {
            try
            {

                Console.WriteLine("Server");

                IPAddress address = IPAddress.Parse("127.0.0.1"); //The IP address of the server
                IPEndPoint myEndPoint = new IPEndPoint(address, 6503);
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                serverSocket.Bind(myEndPoint);

                serverSocket.Listen(1000);

                for (int n = 0; n < 10; ++n)
                {
                    serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
                }

                Console.WriteLine("Server now waiting");
                Console.ReadLine();

                foreach (ConnectionInfo info in list)
                {


                    string strFileName = string.Format(@"c:\temp\from-client-{0}.dat", info.client);

                    using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        int nMaxChunk = 0;
                        int nCurrentChunk = 0;

                        nMaxChunk = (int)(fs.Length / chucksizeWithoutHeaderData);
                        if ((nMaxChunk * chucksizeWithoutHeaderData) < fs.Length)
                        {
                            ++nMaxChunk;
                        }


                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            byte[] byBuffer;
                            Int64 nAmount = 0;
                            byte[] byMaxChunk = BitConverter.GetBytes(nMaxChunk);
                            while (fs.Length > nAmount)
                            {
                                ++nCurrentChunk;

                                byte[] byCurrentChunk = BitConverter.GetBytes(nCurrentChunk);

                                byBuffer = br.ReadBytes(chucksizeWithoutHeaderData);


                                Console.WriteLine("Sending {0}bytes, chunk {1} of {2} to client {3}", byBuffer.Length,nCurrentChunk,nMaxChunk, info.client);

                                byte [] byTransmitBuffer = new byte[byBuffer.Length + 8];
                                Array.Copy(byCurrentChunk, byTransmitBuffer, 4);
                                Array.Copy(byMaxChunk, 0,byTransmitBuffer, 4, 4);
                                Array.Copy(byBuffer, 0, byTransmitBuffer, 8, byBuffer.Length);

                                info.Socket.Send(byTransmitBuffer);
                                nAmount += byBuffer.Length;
                            }
                        }
                    }
                }


                Console.WriteLine("Press enter to end server");
                Console.ReadLine();



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        } 

    }
}

Client code

    using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace SocketClient
{
    class Program
    {
        static TcpClient socket = new TcpClient();
        static int chunkSize = 16 * 1024;
        static int chucksizeWithoutHeaderData = chunkSize - 8;
        static byte[] byReceiveBuffer = new byte[chunkSize];
        static void ReceiveCallBack(IAsyncResult result)
        {
            Socket socket = result.AsyncState as Socket;
            try
            {
                int bySize = socket.EndReceive(result);

                Console.WriteLine("Recieved bytes {0}", bySize);

                if (bySize != 0)
                {





                    Int32 nSegmentNumber = BitConverter.ToInt32(byReceiveBuffer, 0);
                    Int32 nMaxSegment = BitConverter.ToInt32(byReceiveBuffer, 4);

                    Console.WriteLine("Received segment {0} of {1}", nSegmentNumber, nMaxSegment);

                    string strFileName = string.Format(@"c:\temp\client-from-server.dat");
                    using (FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate))
                    {
                        fs.Position = fs.Length;
                        fs.Write(byReceiveBuffer, 8, bySize-8);
                    }

                    if (nSegmentNumber >= nMaxSegment)
                    {
                        Console.WriteLine("all done");
                    }
                }


                socket.BeginReceive(byReceiveBuffer, 0, byReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }



        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to go");
            Console.ReadLine();


            socket.Connect("127.0.0.1", 6503);

            Console.WriteLine("Client");
            Console.ReadLine();

            byte[] byBuffer;




            socket.Client.BeginReceive(byReceiveBuffer, 0, byReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket.Client);


            using (FileStream fs = new FileStream(@"c:\temp\filetosend.jpg", FileMode.Open, FileAccess.Read, FileShare.None))
             {



                 using (BinaryReader br = new BinaryReader(fs))
                 {

                     int nMaxChunk = 0;
                     int nCurrentChunk = 0;

                     nMaxChunk = (int)(fs.Length / chucksizeWithoutHeaderData);
                     if ((nMaxChunk * chucksizeWithoutHeaderData) < fs.Length)
                     {
                         ++nMaxChunk;
                     }

                     byte[] byMaxChunk = BitConverter.GetBytes(nMaxChunk);

                        Int64 nAmount = 0;

                        while (fs.Length > nAmount)
                        {
                            ++nCurrentChunk;
                            byte[] byCurrentChunk = BitConverter.GetBytes(nCurrentChunk);

                            byBuffer = br.ReadBytes(chucksizeWithoutHeaderData);
                            Console.WriteLine("Sending {0}bytes, chunk {1} of {2}", byBuffer.Length, nCurrentChunk, nMaxChunk);

                            byte[] byTransmitBuffer = new byte[byBuffer.Length + 8];
                            Array.Copy(byCurrentChunk, byTransmitBuffer, 4);
                            Array.Copy(byMaxChunk, 0, byTransmitBuffer, 4, 4);
                            Array.Copy(byBuffer, 0, byTransmitBuffer, 8, byBuffer.Length);

                            socket.Client.Send(byTransmitBuffer);
                            nAmount += byBuffer.Length;
                        }


                 }
             }



            Console.WriteLine("done");


            Console.ReadLine();



        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:45:05+00:00Added an answer on May 20, 2026 at 9:45 pm

    I think that I may have a solution – although I am not 100% confident…I will keep testing and report back if it fails.

    Anyways – I have set the socket.SendBufferSize and RecieveBufferSize to 4 x the chunk size, and now all seems to be well.

    The thing there (re buffer size) just put the problem off, as I thought it might.

    However, I came across a snippet of code elsewhere, and putting these lines into the code fixed the issue.

      try
                                {
                                    bySent = info.Socket.Send(byTransmitBuffer);
                                }
                                catch (SocketException ex)
                                {
                                    Console.WriteLine("Only sent {0}, remaining = {1}", bySent,fs.Length -nAmount);
                                    if (ex.SocketErrorCode == SocketError.WouldBlock ||
                                      ex.SocketErrorCode == SocketError.IOPending ||
                                      ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                                    {
                                        // socket buffer is probably full, wait and try again
                                        Thread.Sleep(30);
                                    }
                                    else
                                        throw ex;  // any serious error occurr
                                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large application (~50 modules) using a structure similar to the following:
I have a large application that uses EJB 2.x entity beans (BMP). This is
I have a large .NET web application. The system has projects for different intentions
I have a third party .NET Assembly and a large Java application. I need
We have fairly large C++ application which is composed of about 60 projects in
Here at work we have a very large application with multiple sub applications. (500
I have a semi-large web application that we run locally and I need to
We have a large (about 580,000 loc) application which in Delphi 2006 builds (on
I have a large Compact Frameworks V2.0 application that in most cases works very
I have a large tree of Java Objects in my Desktop Application and am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.