Example: quiz answers

Lesson 1 - Socket Programming An Introduction to Sockets

Lesson 1 - Socket ProgrammingAn Introduction to SocketsSummaryWe are going to introduce some of the functions and data structures you will come across whenprogramming with ConceptsSockets, Stream Sockets , Datagram SocketsBrief Overview of NetworkingA Socket is a mechanism for allowing communication between processes, be it programs running onthe same machine or different computers connected on a network. More specifically, Internet socketsprovide a Programming interface to the network protocol stack that is managed by the operatingsystem. Using this API, a programmer can quickly initialise a Socket and send messages withouthaving to worry about issues such as packet framing or transmission are a number of different types of Sockets available, but we are only really interested in twospecific Internet Sockets .

Lesson 1 - Socket Programming An Introduction to Sockets Summary We are going to introduce some of the functions and data structures you will come across when

Tags:

  Introduction, Programming, Sockets, Socket programming an introduction to sockets

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Lesson 1 - Socket Programming An Introduction to Sockets

1 Lesson 1 - Socket ProgrammingAn Introduction to SocketsSummaryWe are going to introduce some of the functions and data structures you will come across whenprogramming with ConceptsSockets, Stream Sockets , Datagram SocketsBrief Overview of NetworkingA Socket is a mechanism for allowing communication between processes, be it programs running onthe same machine or different computers connected on a network. More specifically, Internet socketsprovide a Programming interface to the network protocol stack that is managed by the operatingsystem. Using this API, a programmer can quickly initialise a Socket and send messages withouthaving to worry about issues such as packet framing or transmission are a number of different types of Sockets available, but we are only really interested in twospecific Internet Sockets .

2 These are: Stream Sockets Datagram socketsWhat differentiates these two Socket types is the transport protocol used for data stream Socket uses the Transmission Control Protocol (TCP) for sending messages. TCP providesan ordered and reliable connection between two hosts. This means that for every message sent, TCPguarantees that the message will arrive at the host in the correct order. This is achieved at thetransport layer so the programmer does not have to worry about this, it is all done for datagram Socket uses the User Datagram Protocol (UDP) for sending messages. UDP is a muchsimpler protocol as it does not provide any of the delivery guarantees that TCP does.

3 Messages, calleddatagrams, can be sent to another host without requiring any prior communication or a connectionhaving been established. As such, using UDP can lead to lost messages or messages being receivedout of order. It is assumed that the application can tolerate an occasional lost message or that theapplication will handle the issue of are advantages and disadvantages to using either protocol and it will be highly dependant ofthe application context. For example, when transferring a file you want to ensure that, upon receipt,the file has not become corrupted. TCP will handle all the error checking and guarantee that it willarrive as you sent it.

4 On the other hand, imagine you are sending 1000 messages detailing playerposition data every second in a computer game. The application will be able to tolerate missingmessages here so UDP would be more Internet Sockets , we can identify a host using an IP address and a port number. The IP addressuniquely identifies a machine while the port number identifies the application we want to contact atthat machine. There are a range of well known port numbers, such as port 80 for HTTP, in the range0 - 1023. When choosing port numbers, anything above the well know port number range should befine, but you cannot guarantee that another application will not be already using with SocketsNow we are going to introduce the Windows Sockets on Error HandlingWithin this section we will not be discussing error handling in too much depth.

5 All functions usedhave a wide range of different error messages associated with them. Discussing each error for eachfunction is beyond the scope of these tutorials. Error values for each function can easily be found inthe Specific FunctionsAll the methods for declaring and using Sockets are available in two header files:1 #include < >2 #include < >Windows Sockets header fileYou will also need to link in your project first thing we have to do before being able to declare or use a Socket is make a call to theWSAS tartup() int WSAS tartup(2 WORD version , // highest version of winsock3 WSADATA *data // pointer to WSADATA struct4 );WSAS tartup() function declarationThis method must be called first before any other calls involving the Sockets library.

6 This methodsimply allows the programmer to define the version of the Windows Sockets specification to be usedand stores the result in toWSAS tartup(), we also haveWSAC leanup()which should be called at the endof your program when you no longer require the use of the Socket int WSAC leanup(void);WSAC leanup() function declarationYou will also findWSAGetLastError()useful when you need to perform some error checking ordebug your code:1 int WSAGetLastError(void);WSAGetLastError() function declarationThis method takes no parameters and returns the error status for the last Socket operation thatfailed. The range of error codes that can be returned is too extensive to discuss here.

7 You find adetailed list of all return codes and values in the Sockets InformationHaving initialised the API we now need to set up the address we will be listening on (if we are theserver) or the one we will be sending to (if we are the client). The functiongetaddrinfo()is goingto help us out int getaddrinfo(2 const char *name , // host name or IP3 const char *service , // service name or port4 const struct addrinfo *hints , // Socket info5 struct addrinfo *result // result struct6 );getaddrinfo() function declaration2We either provideNULLto the name parameter if we are creating our server and in the case of theclient we want to provide a host name string or IP address.

8 The service parameter takes either a stringservice name ( http translates to port 80) or the port number. We then have twoaddrinfostructs. We need to populate thehintsstruct with some some option settings before calling thefunction. Theresultstruct stores the results after the function has been typedef struct addrinfo {2 int ai_flags; // Socket options3 int ai_family; // address family4 int ai_socktype; // Socket type5 int ai_protocol; // protocol type6 size_t ai_addrlen; // ai_addr length7 char *ai_canonname; // canonical name8 struct sockaddr *ai_addr; // pointer to sockaddr struct9 struct addrinfo *ai_next; // next addrinfo struct10 }.

9 Addrinfo struct typedefThere are quite a few fields here so we ll look a little closer at the important ones: aiflags- Here we can set some flags to change the Socket options. There are a numberof options available that you can find in the API. The only one we will need for now is theAIPASSIVE flag. This indicates that the Socket we will be creating will be used in a call to thefunctionbind(). We must bind to a Socket if we plan on listening for messages, we arecreating a server program. aifamily- This is the address family we will be using. For IPv4 we would use the flagAFINETand for IPv6 the flag isAIINET6.

10 We do not have to specify the address family if we want tobe able to use both versions. The flag for this isAFUNSPEC. aisocktype- Here s where we can specify the Socket type, stream (SOCKSTREAM) or datagram(SOCKDGRAM).These are the fields that you will want to populate for thehintsstruct. Theresultsstruct is alinked list as the call to the host may return multiple results. Often only one struct will be returnedbut you cannot guarantee that the one you want will be the first one if more than one has been foundso it is recommended that you traverse the list and check for the one you require, making sure it hasbeen initialised a SocketNow for thesocket() Socket Socket (2 int af, // address family3 int type , // Socket type4 int protocol // protocol type5 ); Socket () functionAs you can see from the parameter names, thesocket()function takes a lot of the things thatwe have already specified when populating ourhintsstruct for thegetaddrinfo()function.


Related search queries