Docsity

Network Programming: Sockets and WinSock API in Windows, Slides of Windows Programming

Toggle sidebar
bg1
L ecture #
L ecture #
27
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Network Programming: Sockets and WinSock API in Windows and more Slides Windows Programming in PDF only on Docsity!

LL e c te c t u ru r ee

Review of Last Lecture

  • Thread synchronisation
  • Kernel Objects: Synchronisation Objects

Well-known port numbers

  • 80 http
  • 25 SMTP
  • 110 POP
  • 43 WHOIS
  • 53 DNS
  • 21 FTP

Introduction to Network Programming

  • DNS: Domain Name System example http://www.vu.edu.pk/ could be http://203.215.177.33/
  • Hostnames of machines
  • Address resolution: ARP and RARP

Windows Sockets

  • An object that represents an endpoint for communication between processes across a network transport. Sockets have a datagram or stream type and can be bound to a specific network address. Windows Sockets provides an application programming interface (API) for handling all types of socket connections in Windows.
  • The BSD sockets on UNIX
  • Concept of client and server

Windows Sockets: HTTP

  • Web browser as an HTTP client application
  • What is HTTP?
  • Structure of URL: protocol, hostname, document name
  • http://www.vu.edu.pk/default.htm

Windows Sockets Library

  • File Purpose
  • ws2_32.dll Main WinSock 2 DLL
  • wsock32.dll For WinSock 1.1 support, 32-bit applications
  • mswsock.dll MS extensions to WinSock
  • winsock.dll For WinSock 1.1 support, 16-bit applications
  • ws2help.dll WinSock2 helper
  • ws2tcpip.dll WinSock 2 helper for TCP/IP stacks

Initialise WinSock

int WSAStartup(

WORD wVersionRequested, LPWSADATA lpWSAData

);

  • WinSock version: high-order byte specifies the minor version (revision) number; the low- order byte specifies the major version number.

WinSock Server Socket functions

int bind(

SOCKET s, const struct sockaddr FAR *name, int namelen

);

struct sockaddr {

u_short sa_family; char sa_data[14];

};

WinSock Server Socket functions

A special version of struct sockaddr is used for internet address familty AF_INET, and that defined as struct sockaddr_in

struct sockaddr_in {

short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8];

};

Resolving Host Names

struct hostent FAR *gethostbyname(

const char FAR *name

);

struct hostent {

char FAR * h_name; char FAR * FAR * h_aliases; short h_addrtype; short h_length; char FAR * FAR * h_addr_list;

};

WinSock Client Socket functions

int connect(

SOCKET s, const struct sockaddr FAR *name, SERVER’s int namelen

);

  • If socket s , is unbound, unique values are assigned to the local association by the system, and the socket is marked as bound.

Difference between Server and Client socket calls

  • Client: socket() > connect() > send/recv() > closesocket
  • Server: socket() > bind() > listen() > accept() > recv/send () > closesocket

Listen

int listen( SOCKET s, int backlog );

Places a socket in a state where it is listening to an incoming connection and can accept it.

backlog specifies maximum number of pending connections; It can not be greater than SO_MAXCON

Returns 0 if all is well, otherwise SOCKET_ERROR

TIP: SOCKET_ERROR is in fact equal to -