Part 3 - Winsock

dingdongboomNetworking and Communications

Oct 27, 2013 (3 years and 9 months ago)

79 views

Outline


Socket programming with Windows OS


C++ UDPSocket class

Socket Programming with
Windows OS


1982
-

Berkeley Software Distributions
introduced sockets as an interface for
communication between local processes


1986
-

Berkeley extended the socket interface
for use over the TCP/IP with UNIX


Today many applications (FTP, Telnet, etc)
depend on these interfaces

Socket Programming with
Windows OS

Socket


a programming interface


a logical, file handle like, construct that an
application uses for communication (everything
in Unix is a file)


not restricted to TCP/IP

Socket Programming with
Windows OS

Communication protocols


connection oriented (Transmission Control
Protocol
-

TCP/IP)


connectionless (User Datagram Protocol
-
UDP
and Inter
-
network Packet Exchange
-

IPX)

Socket Programming with
Windows OS


Microsoft proposed an industry wide socket
standard for Windows OS, called as Windows
Socket Interface or WinSock.


Windows socket based applications use the
WinSock interface to access the default
Windows WinSock implementation,
WinSock.dll

or an alternative implementation such as, the
FTP WinSock.dll

WinSock.dll

FTP WinSock.dll

TCP/IP

IPX

AppleTalk

NetBIOS

Remote Access


Service (RAS)

FTP TCP/IP

Modem

Network Drivers

LAN

Application

Windows Socket, Protocols and Applications

Phone Line

Differences Between

Berkeley and WinSock


Socket is an int data type in Berkeley, but a
SOCKET data type in WinSock


SOCKET_ERROR

is produced by all WinSock
functions, but negative one (
-
1) in Berkeley


Applications must call
WSAStartup()

before
calling any WinSock functions, and should call
the
WSACleanup()

function before terminating

Structures


struct sockaddr { // socket address information


u_short sa_family; // address family


char sa_data[14];


// up to 14 bytes of direct address

};


struct in_addr { // internet address


u_long s_addr; // socket address

};

Structures

struct sockaddr_in { // socket address internet


short sin_family; // 2 bytes


u_short sin_port; // 2 bytes port number


struct in_addr sin_addr; // 4 bytes net ID, host ID


char sin_zero[8]; // 8 bytes unused

};



There are also some functions to convert from and to native types.



The internet address for a socket
-
based application composed of two pieces


-

two
-
byte port number (sin_port) > 1023


-

four
-
byte network address (sin_addr)

Creating a Socket

SOCKET PASCAL FAR
socket

(int af, int type, int protocol)



Actual

Address Family Type Protocol Protocol

AF_INET SOCK_DGRAM IPPROTO_UDP UDP

AF_INET SOCK_STREAM IPPROTO_TCP TCP


A Non
-
blocking Socket

Not to block on a socket call use a non
-
blocking socket.

int PASCAL FAR
ioctlsocket

(SOCKET soc, long command,


u_long FAR * argp)



Command Argp State

FIONBIO non
-
zero non
-
blocking

FIONBIO zero blocking

Binding a Socket


Associating address with a socket


The socket end point becomes a named entity visible in
the name space


int PASCAL FAR
bind

(SOCKET soc, const struct
sockaddr FAR * address, int addrlen)

Sending UDP


Data delivery is not guaranteed


Data grams are directed towards a specific recipient.

int PASCAL FAR
sendto

(SOCKET soc, // my socket



const char FAR * buffer, // sending buffer



int len, // data length



int flags,



// 0



const struct sockaddr FAR * to, // recipient



int tolen) // recipient’s address length

Receiving UDP

int PASCAL FAR
recvfrom

(SOCKET soc, // my socket



char FAR * buffer, // receiving buffer



int len, // data length



int flags,



// 0



struct sockaddr FAR * from, // this is me



int FAR * fromlen) // my address length

Communicating UDP

UDP is like making a radio communication.



You just listen



if somebody calls your call
-
sign, then you answer the
call, and vice versa



The difference is callings are made only once, if you
miss a call, you miss the message



Use non
-
blocking sockets not to block on socket calls

recvfrom/sendto



SERVER



CLIENT



socket() & bind()


socket() & bind()



recvfrom()



sendto()



sendto()



recvfrom()



close()



close()

C++ UDPSocket Class


Constructor


Creates non
-
blocking a socket with a default or passed port
number


Initializes the Winsock.dll


Finds the internet address of the host computer


Binds the socket to this address


Destructor takes care of the closing operations


C++ UDPSocket Class


Sends data to an internet address and port no


Internet address, pointer to send buffer, port no

(if not default) must be passed


void sendData(IP, char *, port=default_port)


C++ UDPSocket Class


Receives data into a buffer through listening
port


Double array is used for my applications


Parses and puts the received data into buffer


bool receiveData (double *, port = default_port)


References



Network Programming in Windows NT, Alok K. Sinha


www.aw.com/cseng (sample code)



www.ecst.csuchico.edu/~beej/guide/net/