Could anyone point me in the direction of some easy to understand networking tutorials? My goal is to implement a simple pong clone in C++ that could be played over a LAN.
udp protocol for a real time app ( it means that you dont verify that the packet arrived, and so you have a real time app, with not so much delay)
These day it does now matter if you use UDP or TCP.
Also, there is a TCP setting for disabling packet ack before new sending. It's TCP_NODELAY option who disable the naggle algorythm.
This are very basic example of UDP.;D
Basic but it works....



| 1 | //////////////////CLIENT//////////////////////////////////////////////////////// |
| 2 | int init_client_and_send_packet() |
| 3 | { |
| 4 | WSADATA wsa; |
| 5 | WSAStartup(MAKEWORD(2,0),&wsa); |
| 6 | SOCKADDR_IN sin; |
| 7 | |
| 8 | //define structure |
| 9 | sin.sin_family=AF_INET; |
| 10 | sin.sin_addr.s_addr=inet_addr(ip); |
| 11 | sin.sin_port=htons(port); |
| 12 | //prepare invoice |
| 13 | sock=socket(AF_INET,SOCK_DGRAM,0); //init socket with SOCK_DGRAM (thats say we send in UDP) |
| 14 | bind(sock,(SOCKADDR*)&sin,sizeof(sin)); //structure to socket |
| 15 | sinsize=sizeof(sin); |
| 16 | |
| 17 | //init of our array buffer[] |
| 18 | memset(trace_udp,0,sizeof(trace_udp)); |
| 19 | //we do the invoice |
| 20 | nbrbytessended=sendto(sock,udp_buffer,sizeof(udp_buffer),0,(SOCKADDR*)&sin,sinsize); |
| 21 | return(0); |
| 22 | } |
| 23 | |
| 24 | int close_client() |
| 25 | { |
| 26 | shutdown(sock,2); |
| 27 | closesocket(sock); |
| 28 | return(0); |
| 29 | } |
| 1 | /*************** RECEIVER****************/ |
| 2 | #include<stdio.h> |
| 3 | #include<winsock2.h> |
| 4 | #pragma comment(lib,"ws2_32.lib") |
| 5 | #define NBMAX_CAR 10 |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | WSADATA wsa; |
| 10 | WSAStartup(MAKEWORD(2,0),&wsa); |
| 11 | |
| 12 | SOCKET sock; |
| 13 | SOCKADDR_IN sin; |
| 14 | |
| 15 | |
| 16 | //Variables |
| 17 | int port = 11060; //Ne pas init. si demande au user. |
| 18 | char buffer[NBMAX_CAR];//Pour stocker les packets à envoyer |
| 19 | int bytesreceived=0; |
| 20 | |
| 21 | //building structure |
| 22 | sin.sin_family=AF_INET; |
| 23 | sin.sin_addr.s_addr=INADDR_ANY; |
| 24 | sin.sin_port=htons(port); |
| 25 | |
| 26 | //init socket with SOCK_DGRAM (thats say we send in UDP) |
| 27 | sock=socket(AF_INET,SOCK_DGRAM,0); |
| 28 | |
| 29 | //set active socket to structure |
| 30 | bind(sock,(SOCKADDR*)&sin,sizeof(sin)); |
| 31 | int sinsize=sizeof(sin); |
| 32 | int i=0; |
| 33 | while(1) |
| 34 | { |
| 35 | memset(buffer,0,sizeof(buffer)); |
| 36 | bytesreceived=recvfrom(sock,buffer,sizeof(buffer),0,(SOCKADDR*)&sin,&sinsize); |
| 37 | |
| 38 | i++; |
| 39 | } |
| 40 | } |