Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys
I want to send some text messages from server to many clients. client only show them on a messagebox. since there are many client, I get a udp socket, send data to it, and then closesocket. my problem is: after writing data to socket, If I immediately close the socket, message will be lost. where I should close the socket?
thanks in advance
mr.abzadeh
Edit: the original question was not clear, so I edited it
Edit: my code is added here(simplified)
SOCKET socket1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 ADDRINFOW * pAddr;
 ADDRINFOW hints;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = AF_INET;
 hints.ai_protocol = IPPROTO_UDP;
 hints.ai_socktype = SOCK_DGRAM;
 GetAddrInfoW( wszHostName, lpPortName, &hints, &pAddr);
 sendto( socket1, (const char *)pBuf, uSendLen, 0, pAddr->ai_addr, Addr->ai_addrlen);

 closesocket(socket1);//This kills socket and prevents message delivery
Posted
Updated 23-Feb-14 20:14pm
v3
Comments
Sergey Alexandrovich Kryukov 22-Feb-14 23:59pm    
Why UDP? What is your purpose? Help with what? What's the problem?
—SA
mr.abzadeh 24-Feb-14 2:21am    
I use TCP for communication between server and clients, starting at clients, responced by server. Sometimes conversations are started by server. Since I don't want to use two distinct ports, I decided to use UDP for messaging. the Admin at a client posts a message to server by TCP, which is mirrored to some or all of clients via UDP. it may or may not be tolerable if udp doesn't deliver message. Can you show me another plan to send messages to clients, originated at server? Since my question was very badly stated, I clarified and edited it.
My current question is this: In the following code, closesocket destroys socket very soon and prevents message delivery.

SOCKET socket1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
ADDRINFOW * pAddr;
ADDRINFOW hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_socktype = SOCK_DGRAM;
GetAddrInfoW( wszHostName, lpPortName, &hints, &pAddr);
sendto( socket1, (const char *)pBuf, uSendLen, 0, pAddr->ai_addr, Addr->ai_addrlen);

closesocket(socket1);//This kills socket and prevents message delivery
mr.abzadeh 23-Feb-14 14:07pm    
I use TCP for communication between server and clients, starting at clients, responced by server. Sometimes conversations started by server. Since I don't want to use two distinct ports, I decided to use UDP for messaging. the Admin at a client posts a message to server by tcp, which is mirrored to some or all of clients via udp. it may or may not be tolerable if udp doesn't deliver message. Can you show me another plan to send messages to clients, originated at server? Since my question was very badly stated, I clarified and edited it.
[no name] 23-Feb-14 18:55pm    
UDP is often used for what you suggest. Have you read up on UDP. Do you want some links on how to do this? What is your question?
mr.abzadeh 24-Feb-14 2:18am    
I have read up on TCP and lesser on UDP. any link describing and help implementing UDP is appreciated. My current question is this: In the following code, closesocket destroys secket very soon and prevents message delivery.
SOCKET socket1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
ADDRINFOW * pAddr;
ADDRINFOW hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_socktype = SOCK_DGRAM;
GetAddrInfoW( wszHostName, lpPortName, &hints, &pAddr);
sendto( socket1, (const char *)pBuf, uSendLen, 0, pAddr->ai_addr, Addr->ai_addrlen);

closesocket(socket1);//This kills socket and prevents message delivery

1 solution

Why not just do it the way the documentation suggests? It may be productive to contemplate why sendto() returns a value.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740148(v=vs.85).aspx[^]

This may help you also:

http://gafferongames.com/networking-for-game-programmers/virtual-connection-over-udp/[^]

The examples here are good:
http://computing.unn.ac.uk/staff/cgmb3/teaching/sockets/DonahooWinAdaptsListings.pdf[^]
 
Share this answer
 
Comments
mr.abzadeh 24-Feb-14 19:20pm    
I have fuuly checked the return values in my code. Here I simplified the code for breafness.
I studied the links above, all are the principal materials about TCP and UDP
[no name] 24-Feb-14 19:33pm    
Does the datagram arrive at the client if you don't close the socket? I assumed that was the first thing you tested.
mr.abzadeh 26-Feb-14 0:36am    
Yes, It arrives. and even when I closesocket, 80% of times the message is delived. I don't know the resson of 20% fail is UDP fail or race condition where I close socket.
[no name] 26-Feb-14 0:44am    
It is just an unreliable network. Try to read the spec for UDP - it explains all this. If you cannot live with this you need to implement a more reliable method involving handshaking such as TCP or the virtual UDP I gave you in the links.
mr.abzadeh 26-Feb-14 18:26pm    
Thanks for your help. I assume it is the UDP not deliver message. I close this Post, But I have one more question: If the peer that will receive the message has been shut down without notifying, for example, due to power failure, will sendto block the thread? If so, How to overcome this?

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