C++ socket and thread [modified]
-
Hi, I have a small programm which listen a socket in a new thread. but I want to restart the porgramm after the first message. Start the thread in main.cpp
hThread = CreateThread( NULL, 0, ThreadFunc, &dwThrdParam, 0, &dwThreadId);
The programm :
DWORD WINAPI ThreadFunc( LPVOID lpParam ) { WSAStartup(MAKEWORD(2,2),&initialisation_win32); socket_recevoir=socket(AF_INET,SOCK_DGRAM,0); information_recevoir.sin_family=AF_INET; information_recevoir.sin_addr.s_addr=INADDR_ANY; information_recevoir.sin_port=htons(25); bind(socket_recevoir,(struct sockaddr*)&information_recevoir,sizeof(information_recevoir)); tempo=sizeof(information_recevoir); nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo); buffer[nb_caracters]=0; if (nb_caracters >0) { ExecuteCommand(buffer, NULL, 0); } closesocket(socket_recevoir); return 0; }
In main.cpp, I want to call again this programm. Can you help me please ?
modified on Monday, April 5, 2010 10:42 AM
-
Hi, I have a small programm which listen a socket in a new thread. but I want to restart the porgramm after the first message. Start the thread in main.cpp
hThread = CreateThread( NULL, 0, ThreadFunc, &dwThrdParam, 0, &dwThreadId);
The programm :
DWORD WINAPI ThreadFunc( LPVOID lpParam ) { WSAStartup(MAKEWORD(2,2),&initialisation_win32); socket_recevoir=socket(AF_INET,SOCK_DGRAM,0); information_recevoir.sin_family=AF_INET; information_recevoir.sin_addr.s_addr=INADDR_ANY; information_recevoir.sin_port=htons(25); bind(socket_recevoir,(struct sockaddr*)&information_recevoir,sizeof(information_recevoir)); tempo=sizeof(information_recevoir); nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo); buffer[nb_caracters]=0; if (nb_caracters >0) { ExecuteCommand(buffer, NULL, 0); } closesocket(socket_recevoir); return 0; }
In main.cpp, I want to call again this programm. Can you help me please ?
modified on Monday, April 5, 2010 10:42 AM
Hi, from what I can see the socket is not doing anything or you did not post the full code. The server socket never calls
listen
oraccept
. Maybe you could provide us more information? In a nutshell there a three possible designs for a server: your server handles one client at a time (example), uses asynchronous I/O (multiple connections in a single thread context) or uses multi threading (one connection per thread). Here are some links that might come helpful: - Internet / Network - CodeProject[^] - Beej's guide to networking programming[^] - Winsock Programmer's FAQ[^] Last two have working client/server examples in section 6. Hope it helps :) /MWebchat in Europe :java: Now with 26% more Twitter
modified on Monday, April 5, 2010 5:40 AM
-
Hi, from what I can see the socket is not doing anything or you did not post the full code. The server socket never calls
listen
oraccept
. Maybe you could provide us more information? In a nutshell there a three possible designs for a server: your server handles one client at a time (example), uses asynchronous I/O (multiple connections in a single thread context) or uses multi threading (one connection per thread). Here are some links that might come helpful: - Internet / Network - CodeProject[^] - Beej's guide to networking programming[^] - Winsock Programmer's FAQ[^] Last two have working client/server examples in section 6. Hope it helps :) /MWebchat in Europe :java: Now with 26% more Twitter
modified on Monday, April 5, 2010 5:40 AM
-
this application execute this line : ExecuteCommand(buffer, NULL, 0) but only one time. I will check your links
Could you please format your code with <pre> tag for readability and remove the French comments? It was quite hard to read. Looking again at your code, you have a blocking UDP socket waiting for a connection, which calls
ExecuteCommand()
and exists the thread? Perhaps all you are looking for is a for loop (and a way to programatically exit the worker thread)? :) /MWebchat in Europe :java: Now with 26% more Twitter
modified on Monday, April 5, 2010 9:10 AM
-
Could you please format your code with <pre> tag for readability and remove the French comments? It was quite hard to read. Looking again at your code, you have a blocking UDP socket waiting for a connection, which calls
ExecuteCommand()
and exists the thread? Perhaps all you are looking for is a for loop (and a way to programatically exit the worker thread)? :) /MWebchat in Europe :java: Now with 26% more Twitter
modified on Monday, April 5, 2010 9:10 AM
-
Think you for your help. I removed the comments and used
. I hope it's easier to read.
yes, you're right, I want to add a loop but I don't where in my code.Think you
Okay thanks for the formatting! :) Let my try to help.... put the loop around the blocking part (pseudocode):
socket();
bind();
for(;;)
{
recvfrom();
if(ok) ExecuteCommand();
}
closesocket();Right now the worker thread has no exit condition, you probably want to add one in order to avoid terminating the thread the hard way at application exit. Also have a look at some other issues, e.g. starting from this line
nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo);
should become something like
int nb_caracters = recvfrom(socket_recevoir, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&information_recevoir, &tempo);
if(nb_caracters > 0) //data was received and no socket error (return value -1)?
{
buffer[nb_caracters] = 0; //one byte was reserved for terminating Null
ExecuteCommand(buffer, NULL, 0); //lets hope it was a complete package and also not a duplicate
}Webchat in Europe :java: Now with 26% more Twitter
modified on Monday, April 5, 2010 3:34 PM