ofsarac

Hello

I am developing an application which uses sockets. the program is simply a file transfer program. there is a server which is able to send and receive files. also there are clients.

the client, however, is able to download files sequentially or parallel. this means client arranges a downloading option with checkbox, for example. if it is set to sequential, selected files to be downloaded are going to be performed one by one. but if parallel option is selected, desired files must be downloaded in parallel, simultaneously.

I think there is nothing special about the server. it only accepts users and operates what they request. the problem is on client part. because every client must have multiple threads, client side must be able to handle this. I could not build a structure to accomplish.

the question is, that is the proper way to do this I think, creating a client thread in main thread and for parallel option, again creating a new thread in this thread will be ok. but it seems a little complex.

there are some problems: how to copy sockets do I have to connect for every parallel thread in order to use server operations

hot to manage these threads on client thread will be a problem in this

I know it is a little long description but I hope it is clear Wink

thanks in advance...



Re: Visual C++ General Multithreaded file download with sockets

einaros

Is this a native C++ or C++/CLI application





Re: Visual C++ General Multithreaded file download with sockets

scor7910

Method is different which is your network using a P2P or not.

When you are using P2P, Server has not many work, as you wrote,

On condition that you are using P2P, client should do synchronize each clients communication on thread's procedure function,(with synchronization objects), sending a packet(file), manage other client's socket.

At server part, just manage a client's request.

Otherwise, server has many works as I mentioned.

client just connect to a server, and receive a file.(Client no need an additional thread)






Re: Visual C++ General Multithreaded file download with sockets

ofsarac

scor7910 wrote:

Method is different which is your network using a P2P or not.

When you are using P2P, Server has not many work, as you wrote,

On condition that you are using P2P, client should do synchronize each clients communication on thread's procedure function,(with synchronization objects), sending a packet(file), manage other client's socket.

At server part, just manage a client's request.

Otherwise, server has many works as I mentioned.

client just connect to a server, and receive a file.(Client no need an additional thread)

thank you for your reply...

client must have multithreads in order to download files in parallel. that's why I need threads. I have to start threads in the number of files to be downloaded. so, there will be a synchoronization problem. I see. my problem is, do I have to create a new socket connection to server and receive data from this socket descriptor there will be multiple threads. one socket connection and writing to this socket can not be used, I guess.

and you wrote me to synchonize the other clients threads. how can I manage outher threads from a client how do I manage these threads for every thread, there will be a server function code. this code will handle the operations in a thread. that means for every connection -as I mentioned in client thread connection- there will be a thread and server procedure handle the operations.

am I right

I coul not understand the stucture to build, yet.

my question is again same with these additional informations. it may seen that there is no need to express these again. but as I said, I could not solve the problem yet.

I hope I am clear...

and thanks in advance, again...





Re: Visual C++ General Multithreaded file download with sockets

scor7910

I understand what you mean.

Client can request a connection and a receiving a file only one socket.

Client can request a file as below.

Code Snippet

//declare protocol as below

#define GETFILE 1

....

struct __PACKET

{

int command; // Request Command

char buffer[1024]; // buffer message or file

}FILE_PACKET;

//after connected to a server

//If you want Request a file

void XXX::RequestFile(char * fname)

{

FILE_PACKET packet = {0,};

packet.command = GETFILE;

//If Client can select a file , add an additional data to packet structure.

send(s/*socket that connected to server*/,

&packet, sizeof(FILE_PACKET), MSG_DONTWAIT);

}

Server side, using WSAEventSelect as below

Code Snippet

SOCKET client_socket_array[WSA_MAXIMUM_WAIT_EVENTS];

WSAEVENT wait_event_array[WSA_MAXIMUM_WAIT_EVENTS];

//Create server socket and bind

.................

SOCKET new_client;

WSAEVENT new_event;

new_event = WSACreateEvent();

if(WSAEventSelect(server_socket, new_event, FD_ACCEPT) != SOCKET_ERROR)

{

if(listen(server_socket, 5) != SOCKET_ERROR)

{

client_socket_array[client_sock_cnt] = server_socket;

wait_event_array[client_sock_cnt] = new_event;

client_sock_cnt++; //count connected client's socket

}

}

//thread function

WSANETWORKEVENTS netEvent = {0,};

while(1)

{

index = WSAWaitForMultipleEvents(client_sock_cnt, wait_event_array, FALSE, WSA_INFINITE, FALSE);

index = index - WSA_WAIT_EVENT_0;

for(int i = index; i<client_sock_cnt; i++)

{

index = WSAWaitForMultipleEvents(1, &client_event_array[i], TRUE, 0, FALSE);

if(index == WSA_WAIT_FAILED || index == WSA_WAIT_TIMEOUT))

continue;

else

{

index =i;

EnumNetworkEvents(client_socket_array[index], client_event_array[index], &netEvent);

if(netEvent.iErrorCode[FD_ACCEPT_BIT])

{

//process an accept

}

else if(netEvent.lNetworkEvents & FD_READ) // Received from a client

{

//here wait a synchronization object(mutex, semaphore ...);

recv(client_socket_array[index- WSA_WAIT_EVENT_0], packet, sizeof(FILE_PACKET), 0);

if(packet.command == GETFILE)

{

//process Send a File

}

//releas a synchronization object

}

//process other events

}

}