Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword or section
Like this
0 of .
Results for:
No results containing your search query
P. 1
Winsock Networking Tutorial (C++)

Winsock Networking Tutorial (C++)

Ratings: (0)|Views: 1,377|Likes:
Published by river_11576
Winsock Network C++ Programming Tutorial
Winsock Network C++ Programming Tutorial

More info:

Categories:Types, School Work
Published by: river_11576 on Jul 23, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Networking introduction
First I will give you an introduction to basic networking principles and terms. Anyone with internetaccess will have some knowledge about networks, servers, clients, but to ensure you know enoughto program with it I've included this chapter. You won't need all the details mentioned here whenprogramming winsock, but it's good to know something about the underlying techniques.
1. Networks and protocols
You probably already know what a network is, it's a collection of computers connected to eachother so they can exchange data. There are several types of networks, such as LANs (Local AreaNetwork), WANs (Wide Area Network) and of course the internet. To ensure that all traffic is goingsmoothly, networks rely on protocols:
A protocol is a set of rules describing the format in which data is transmittedover a network.
As stated in the information box above, a protocol describes how to communicate over a network.It can be compared with a human language: at the lowest level nearly everyone can make andhear sounds (compare: electronic signals) but people won't understand each other unless theyspeak a according to a specific language they both understand (compare: protocol).
2. Ethernet
Networks rely on several
protocol layers
, each one having its own task in the communicationprocess. A very commonly used configuration is the ethernet LAN with TCP/IP. In ethernet LANs,computers can be connected using coaxial, twisted pair (UTP) or optic fiber cables. Nowadays, formost networks, UTP cables are used. WANs and the internet (partly a combination of many WANs)use many of the techniques used in ethernet LANs, so I will discuss ethernet LAN technology first.
The lowest layer of ethernet is the hardware level, called the
Media Access Layer
, or MAC forshort. This layer can be a network card, for example, which contains the serial network interfaceand controller that take care of converting the raw data into electronic signals and sending it tothe right place.Package that are sent over a network of course need to reach their destination. So there has tobe some kind of addressing. Various levels of the ethernet interface have different addressingmethods, as you will see later. At the lowest MAC level, addressing is done with MAC numbers.
MAC number
48-bit identifier that is
into each network interface unit. Theallocation of these numbers is done by the IEEE Registration Authority so eachethernet chip has a world wide unique number (that is, if the manufacturerdidn't mess up :). MAC numbers are often noted as colon-separated hexnumbers: 14:74:A0:17:95:D7.
To send a packet to another network interface, the packet needs to include its MAC number. LANsuse a very simple method to send the packets to the right interface:
. This meansthat your network card just shouts the package to every other interface it can reach. Each
Winsock Networking Tutorial (C++)
© 2010 by Thomas Bleeker (MadWizard)
Winsock C++ programming tutorial
Page 1 / 46Page 1 / 46
receiving interface looks at the destination MAC number of the packet, and only buffers it if matches its own MAC number. While this method is easy to implement and quite effective on LANs,bigger networks (WANs, internet) don't use this method for obvious reasons; you wouldn't wanteveryone on the internet to send packets to everyone else on the internet. WANs use betterrouting mechanisms, which I won't discuss here. Just remember that at the lowest level, addressingis done with MAC numbers. Ethernet packets also include a CRC and error detection.
Just above the hardware level is the
level. IP simply stands for Internet Protocol. Just like theMAC layer, IP too has its own way of addressing:
IP number
The numbers used to address at the IP level of the network interface. IPv4,the version most widely used uses 32-bit values, noted in the well knowndotted format: Unlike MAC numbers, IP numbers are nothardcoded into the hardware, they are assigned to it at software level.
IP numbers shouldn't be something strange to you. The internet uses them to uniquely identify aspecific computer. IP addresses can be assigned to a network interface using software. Doing thisassociates the IP number with the MAC address of the network interface. To address using IPnumbers, the associated MAC number needs to be resolved. This is done with the
(AddressResolution Protocol). Each host maintains a list with pairs of IP and MAC numbers. If an IP is usedwithout a matching MAC number, the host sends out a query packet to the rest of the LAN. If anyof the other computers in the LAN recognize their IP number, it sends back the corresponding MACnumber. If no matching MAC number can be found the packet is sent to the
, a computerthat forwards packages to external networks. The IP to MAC conversion is actually done at thedata link layer (MAC layer)The IP protocol adds the source and destination address (IP numbers) to the packet, as well assome other package properties such as the TTL hops (time to live hops), the protocol versionused, header checksum, sequence count and some more fields. They are not important to us so Iwon't explain them in detail.
The next layer is the TCP layer (or alternatively, the UDP layer). This layer is very close to thenetwork application and deals with many things. As final addition to the addressing, TCP adds aport number to the package:
Port number
While IP numbers are used to address a specific computer or network device,port numbers are used to identify which process running on that device shouldreceive the package. Port numbers are 16-bit, and thus limited to 65536numbers. A process can register to receive packets sent to a specific portnumber ('listening'). A notation often used when addressing a port number ona device is 'IP:portnumber', eg. Both sides of a connectionuse a port number, but not necessarily the same.
Many port numbers are WKP (Well Known Ports), that is they are commonly associated with aspecific service. For example, the WWW uses port 80 by default, FTP uses port 21, e-mail uses 25(SMTP) and 110 (POP). Although these are the ports usually used for those services, nobody
Networking introduction - MadWizard.org
Winsock C++ programming tutorialPage 2 / 46Page 2 / 46
prevents you from using different ports. However, it's a good practice to use port numbers higherthan 1024 for other, custom services.While the IP layer doesn't care about the success of transmissions, TCP does. The TCP layerensures data does arrive, and correctly. It also lets the receiver control the data flow, ie. thereceiver can decide when to receive data. If a package is lost during the way to its destination,TCP resends the package. TCP also reorders the packages if they arrive in an order different fromthe original order. This makes the programmer's life easy as it can safely assume the data that issent is received and in the right order. UDP, an alternative for TCP, does not have these featuresand cannot guarantee the arrival of packages. TCP is connection-oriented, and the best choice forcontinuous data streams. UDP on the other hand is connectionless, and packet oriented. I won'tdeal with UDP in this tutorial.
Finally, above the TCP layer is the network software. In windows, your application does notdirectly access the TCP layer but uses the
API. The software layer provides a veryconvenient way of dealing with networking. Thanks to all the underlying layers, you don't need toworry about packets, packet size, data corruption, resending of lost packets etc.
3. The ethernet interface stack
The image above shows the encapsulation of the each protocol in the ethernet interface stack. Itall starts with the software layer, which has a piece of data that it wants to send over thenetwork. Even this data usually has a format (eg. HTTP, FTP protocols), although not shown in theimage. The user data first gets a TCP header including the source and destination port number.Then the IP header is added, containing the source and destination IP address. Finally the data linklayer adds the ethernet header, which specifies the MAC numbers of the source and destination.This is the data that is actually sent over the wires. As you can see there's a lot of overhead in anTCP/IP package. The overhead can be minimized by choosing a large enough data size for thepackage. Luckily winsock will arrange this for you.
Networking introduction - MadWizard.org
Winsock C++ programming tutorial
Page 3 / 46Page 3 / 46

Activity (7)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
vel.sakthi3152 liked this
Hanky Banky liked this
ldzjy119 liked this
meghakumbare liked this
van_khang liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->