C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 2nd, 2008, 06:42 PM
beloxx beloxx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 beloxx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 28 sec
Reputation Power: 0
Udp sockets

Hello everyone!
I just joined this forum and I was wondering if anyone could help me with a udp problem. Im trying to make a pong multiplayer game using c++/sdl lib and winsock udp sockets. My question relates to udp and how can I construct it to handle 2 clients that send messages to the server where the data is resent to both clients. I already managed to make a udp server and a client however Im not sure how to go about making it possible for the server to handle 2+ clients at a time. Do I need threads?

Thank you.

Reply With Quote
  #2  
Old May 2nd, 2008, 06:58 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 57 sec
Reputation Power: 480
Theoretically, a UDP server does not need to multithread.

Let's assume that the server has one UDP socket and both clients send to that same socket:
Server creates socket and waits to read something.
Client 1 sends a datagram.
Server reads it, processes it, and sends response to Client 1.
Client 2 sends a datagram.
Server reads it, processes it, and sends response to Client 2.
Client 1 sends a datagram.
Server reads it, processes it, and sends response to Client 1.
Client 1 sends a datagram.
Server reads it, processes it, and sends response to Client 1.

and so on

Of course, in order to run the game the server must know whether it's just heard from Client 1 or 2. Off the top of my head, I would think you might:

1. Embed the client's ID inside the datagram packet.

or

2. Have each client register with the server to join in a game. Part of that registration process would have the server save the client's sockaddr_in. Then when a datagram comes in the server can compare the datagram's sockaddr_in with the ones for the clients who have registered.

Regardless of which approach you use, the server will use the client sockaddr_in it got from the recvfrom call to know where to sendto the response to.


Now, if you want the server to also perform background tasks while waiting for a datagram, then you could use select or non-blocking sockets. For that matter, even if you were using TCP you could still avoid multithreading by using select or non-blocking sockets.

DWise1's Sockets Programming Page:
Dealing With and Getting Around Blocking Sockets
http://members.aol.com/DSC30574/sockets/blocking.html

Reply With Quote
  #3  
Old May 2nd, 2008, 07:25 PM
beloxx beloxx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 beloxx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 28 sec
Reputation Power: 0
Ok, thanks. I still have one question however.

What if both clients send the data at the same time? Will it be handled correctly by the server? (both clients' messages will be received by the server)

Reply With Quote
  #4  
Old May 2nd, 2008, 07:36 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,422 sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 22 m 27 sec
Reputation Power: 1731
The thing with a UDP (connectionless) socket is to use "sendto" and "recvfrom" instead of "send" and "recv". "recvfrom" will supply you with the address of the sender (presuming the proper protocol); you "sendto" that address with the reply.

Using TCP would require a connection (socket) for each client, but in a pong game (two clients) that would not be onerous. In your application, I would call it a tossup.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread).

Reply With Quote
  #5  
Old May 2nd, 2008, 08:40 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 57 sec
Reputation Power: 480
Quote:
Originally Posted by beloxx
Ok, thanks. I still have one question however.

What if both clients send the data at the same time? Will it be handled correctly by the server? (both clients' messages will be received by the server)


I can't think of why not. The packets should queue up in the receive buffer. I guess that if that buffer were to fill up then you'd start dropping packets, but I don't think that should be a problem during normal operation.

As I'm sure you know, tcp guarantees delivery, so if a tcp packet gets dropped or corrupted then the receiver requests a retransmission. You don't have that in udp, which is essentially sendto-and-forget. Any guarantees of delivery must be implemented by the application. Your application protocol (ie, how the applications communicate with each other) could do this in a number of ways; eg:

1. Require handshaking in which the receiver will ACK or NAK within a certain period of time to let the sender know that the packet had arrived; if the sender times out then it didn't arrive and the sender resends. To avoid duplicate packets being received by accident, they should have a sequence field.

2. Some protocols, like TFTP, rely on sequence fields to ensure that all packets are received and pieced back together in the right order.

3. Or a client might always expect a response from the server. If it times out then it tries again.

Reply With Quote
  #6  
Old May 2nd, 2008, 09:43 PM
beloxx beloxx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 6 beloxx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 28 sec
Reputation Power: 0
Ok thanks, I managed to make the server register clients and resend all data that comes in.

Im testing my game on LAN (two clients, one server). I noticed that when one client (client #1) sends a packet it always reseives it back however the other client(client #2) receives packets (of client #1) about 90% of the time.

Is it normal for a client to be guaranteed to receive its own packets from the server?

Also, I thought LAN network never loses packets. Am I wrong? If not, then how come client#2 does not receive all packets that were sent to the server by client#1?

Thank you :-]

Reply With Quote
  #7  
Old May 3rd, 2008, 01:55 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 40 m 58 sec
Reputation Power: 1001
Nope. TCP/IP has several protocols. Each has its advantages and disadvantages.

TCP is a reliable transport protocol, which means that the OS takes care of ensuring that packets don't get lost and they arrive in the same sequence that they were sent out and so on. Of course, the OS cannot guarantee success (for instance, if the wire gets cut or the other end crashes), but it tries its best to deliver the packets. Say if 3 packets are sent and the 2nd one doesn't make it to the other end, TCP will automatically resend the second packet until the other end acknowledges it has received it. The other end will then take the three packets and put them in a proper sequence and deliver it to the listening application. What this means is that if your application uses TCP, then it doesn't need to worry about checking if packets have been lost or arrived out of sequence, since that job is done by the OS. All that your application has to do is hand off the data to send to the OS and let it handle the complicated stuff. Naturally, using TCP means there is some extra overhead involved on both sides to ensure reliable delivery.

UDP is an unreliable transport protocol. In this case, the OS merely transmits and receives packets, but won't check if a packet got lost somewhere or if they've arrived out of sequence. Instead, it leaves that logic to the application to figure it out. This means that there is less overhead in sending and receiving data, but the OS doesn't guarantee any reliability and lets your application handle it.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month
Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Udp sockets


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT