Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two programs that are client/server pairs and they use UDP to talk to one another. In 'What have you tried' is an example of what the server is doing to receive requests from the client. The "stop" message is sent by clicking a button on the server's window while the recvfrom loop is running in a background thread.

On another project I am using WaitForMultipleObjects and I'm thinking I should change my UDP server to use it. Instead of sending a message "stop" to itself, I could use an event to signal WaitForMultipleObjects and check the return code to see if the event was signaled or data came in.

I haven't been able to find a good example of what I need to change in my Winsock code.

I would appreciate any good suggestions.

What I have tried:

RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)

StructSize = 16
RecvAddr4.sin_family = AF_INET
RecvAddr4.sin_port = htons(aui_port)

iResult = bind(RecvSocket, RecvAddr4, StructSize)

do while true
   bytesrecvd = recvfrom(RecvSocket, recvbuf, &
                bufsize, 0, RecvAddr4, StructSize)
   if recvbuf = "stop" then
      exit
   else
      ' do something with the data
   end if
loop

closesocket(RecvSocket)
Posted
Updated 26-Feb-18 23:01pm
v2
Comments
saide_a 25-Feb-18 3:27am    
I don't know I got your problem properly or no!
but you can always check bytesrecvd value for check existence of data, in these way you can have multiple clients and if one of them stop sending data, you can have other client data but in what you said, after stopping one of the clients, server won't answer others. Then in your server your can have closing flag or bottom for ending program

1 solution

I'm not sure,I get your questions excatly.
this code is server's ,get msg from client, if msg is 'stop' then exit.
"
if recvbuf = "stop" then
" I think should be
if recvbuf == "stop" then
 
Share this answer
 
Comments
Roland M Smith 26-Feb-18 22:06pm    
The code example is not C so the equals sign is correct. The way I have it works, I just want to do it differently.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900