Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Learning Network Programming

This thread is locked; no one can reply to it. rss feed Print
Learning Network Programming
Matthew Fay
Member #792
November 2000

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.

gnolam
Member #2,030
March 2002
avatar

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

karistouf
Member #5,126
October 2004
avatar

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)

GullRaDriel
Member #3,861
September 2003
avatar

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.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

karistouf
Member #5,126
October 2004
avatar

This are very basic example of UDP.;D
Basic but it works....
;D;D;D;D

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

Go to: