0

I am new to the windows socket programming using C# and i want to create an application that runs under multiple clients with one server. The server will wait for the incoming connection from client and assign a new port to each connection. Server should accept the file transfer from multiple clients. The transferring file can be about 10-20 MB.

I went through many tutorials and examples but they do transfer in one-to-one pattern. I was able to connect the multiple clients to one server and sending the text through it. The server is accepting the clients connection and their sent text messages but I have no idea transferring the files in same pattern.

I will be a great help if there is any tutorials, examples or guide that help me understand the file transfer from multiple clients to single server.

user456064
  • 177
  • 1
  • 3
  • 14
  • Why invent something new? Implement an FTP server, and have the clients connect to it. There are thousands of examples of this out there. – JamieMeyer Jun 18 '14 at 04:01
  • I dont want two way transfer. Only client can transfer the file and the file will be sorted in different structure by the server with db implementation. Any idea about this? – user456064 Jun 18 '14 at 06:53
  • You have complete control of the the client and server, correct. A file upload via the client should work perfectly via FTP.. And, you can disable downloads in your server. – JamieMeyer Jun 18 '14 at 07:10
  • do you have any good examples or tutorials for FTP server and client based on C#. I can find many such in google but i would like to get your suggestion. – user456064 Jun 19 '14 at 01:15

1 Answers1

-1

it's quite easy actually Save the incoming connections in an array. start a new thread that gets the data from the socket and put it in an output buffer (make sure it's threadsafe) and that's it

edit 18-6-2014 here is a c++ example from my network class it is not perfect but you will get the idea m_clientList is a vector in which I save the connections but I don't know if c# has a vector something like a list will work to

DWORD WINAPI Network::ServerAcceptConnections(LPVOID lpParam)
{
    Network* network = (Network*) lpParam;
    SOCKET new_socket;
    char *message;
    int c = sizeof(struct sockaddr_in);
    
    DWORD dwWaitResult;
    Sleep(100);
    while (true)
    {
        new_socket = accept(pNetwork->m_s , (struct sockaddr *)&pNetwork->m_client, &c);

        if (new_socket != INVALID_SOCKET )
        {
            dwWaitResult = WaitForSingleObject( 
            pNetwork->m_ClientListMutex,    // handle to mutex
            INFINITE);  // no time-out interval
            if (dwWaitResult == WAIT_OBJECT_0)
            {
                __try 
                { 
                    //Reply to the client
                    if (network->m_clientList.size() < network->m_maxConnections)
                    {
                        if(network->m_debugModus) print("DEBUG MODUS: Connection accepted\r\n");
                        network->m_clientList.push_back(new_socket);
                        message = NETWORK_CLASS_CONNECTION_SUCCES;
                        Message out;
                        out.client = new_socket;
                        out.message = message;
                        out.size = strlen(message);
                        pNetwork->SendData(out);
                    }
                    else
                    {
                        if(pNetwork->m_debugModus) printf("DEBUG MODUS: Max connections reached\r\n");
                        message = NETWORK_CLASS_MAX_CONNECTIONS;

                        pNetwork->Send(new_socket, message, strlen(message));
                        closesocket(new_socket);
                    }
                } 

                __finally { 
                    // Release ownership of the mutex object
                    if (! ReleaseMutex(network->m_ClientListMutex)) 
                    { 
                        if(pNetwork->m_debugModus) printf("DEBUG MODUS: AcceptConnections: Cant Relese the Mutex\r\n");
                    } 
                } 
            }   
            else if (dwWaitResult == WAIT_ABANDONED)
            {
                if(network->m_debugModus) print("DEBUG MODUS: SendDataThread: The thread got ownership of an abandoned mutex\r\n");
                return FALSE; 
            }
        }
        else
        {
            if(pNetwork->m_debugModus) printf("DEBUG MODUS: accept failed with error code : %d\r\n" , WSAGetLastError());
        }
    }
    return TRUE;
}
hamidie
  • 67
  • 6
Robert Stevens
  • 488
  • 1
  • 7
  • 21