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 6905881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:14:17+00:00 2026-05-27T08:14:17+00:00

I’m using this client/server code which i found on the net for communication: client:

  • 0

I’m using this client/server code which i found on the net for communication:

client:

    public void Send(string name, string path)
    {
        try
        {
            IPAddress[] ipAddress = Dns.GetHostAddresses("address");
            IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
            Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

            string fileName = "somefile";
            string filePath = path;
            byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);

            byte[] fileData = File.ReadAllBytes(System.IO.Path.Combine(filePath, fileName)); 
            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            fileData.CopyTo(clientData, 4 + fileNameByte.Length);

            clientSock.Connect(ipEnd);
            clientSock.Send(clientData);
            MessageBox.Show("file has been send: " + fileName);

            clientSock.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("File Sending fail." + ex.Message);
        }
    }

server:

public void Receive()
    {
        try
        {
            lblInfo.Content = "That program can transfer small file. I've test up to 850kb file";
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            Socket clientSock = sock.Accept();
            // 1024 * 25.000 = 25mb max that can be received at once with this program.
            byte[] clientData = new byte[1024 * 25000];
            string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\";

            int receivedBytesLen = clientSock.Receive(clientData);

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            lblInfo.Content = "Client: connected & File started received.";

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            lblInfo.Content = "File: received & saved at path: " + receivedPath;

            bWrite.Close();
            clientSock.Close();
        }
        catch (Exception ex)
        {
            lblInfo.Content = "File Receiving fail." + ex.Message;
        }
    }

this code works fine for 1 client that transferes a file to the server. i was wondering how i can change this code to make it communicate with multiple clients that can send files to the server?

also, the server code ‘hangs’ at Socket clientSock = sock.Accept(); and waits untill it receives something. It would be nice if there is a ‘listener’ that listens for new incomming files and then loop through the code, instead of endless waiting at that line.

Im new to client/server programming and all ears to other suggestions.

  • 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-27T08:14:18+00:00Added an answer on May 27, 2026 at 8:14 am

    Here you have explanations for creating your loops for new connections :

    http://msdn.microsoft.com/en-us/library/dz10xcwh.aspx

    IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
    Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    listener.Bind(ipEnd);
    listener.Listen(100);
    
    Console.WriteLine("Waiting for a connection...");
    Socket handler = listener.Accept();
    
    while (true)
    {
        // 1024 * 25.000 = 25mb max that can be received at once with this program.
        byte[] clientData = new byte[1024 * 25000];
        string receivedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\";
    
        int bytesRec = handler.Receive(clientData);
        int fileNameLen = BitConverter.ToInt32(clientData, 0);
        string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
    
        BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
        bWrite.Write(clientData, 4 + fileNameLen, bytesRec - 4 - fileNameLen);
        bWrite.Close();
    }
    
    //dont forget to close handler at server shutdown
    //handler.Shutdown(SocketShutdown.Both);
    //handler.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.