Socket Programming
-
Upload
jayaprakash-manoharan -
Category
Documents
-
view
6 -
download
0
description
Transcript of Socket Programming
-
*Socket ProgrammingWhat is a socket?Using socketsTypes (Protocols)Associated functionsStyles
-
*What is a socket?An interface between application and networkThe application creates a socket
Once configured the application canpass data to the socket for network transmissionreceive data from the socket
-
*Two essential types of socketsSOCK_STREAMa.k.a. TCPreliable deliveryin-order guaranteedconnection-orientedBidirectionalBind(), listen(),accept() and connect()SOCK_DGRAMa.k.a. UDPunreliable deliveryno order guaranteesSend or receive
-
Model of SocketStep 1 : Create a socket
Step 2: connect the socket to the address of the server
Step 3: send/receive the data
Step 4: close the socket*
-
Model of SocketStep 1 : Create a socket
Step 2: connect the socket to the address of the server
Step 3: send/receive the data
Step 4: close the socket*
-
*The struct sockaddrThe generic:struct sockaddr {u_short sa_family;char sa_data[14];};
sa_family specifies which address family is being useddetermines how the remaining 14 bytes are used
The Internet-specific:struct sockaddr_in {short sin_family;u_short sin_port;struct in_addr sin_addr;char sin_zero[8];};sin_family = AF_INETsin_port: port # (0-65535)sin_addr: IP-addresssin_zero: unused
-
*Socket Creation in C: socketint s = socket(domain, type, protocol);s: socket descriptor, an integer (like a file-handle)domain: integer, communication domaine.g., PF_INET (IPv4 protocol) typically usedtype: communication typeSOCK_STREAM: reliable, 2-way, connection-based serviceSOCK_DGRAM: unreliable, connectionless,other values: need root permission, rarely used, or obsoleteprotocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0
-
*A Socket-eye view of the InternetEach host machine has an IP addressWhen a packet arrives at a hostmedellin.cs.columbia.edu(128.59.21.14)cluster.cs.columbia.edu(128.59.21.14, 128.59.16.7, 128.59.16.5, 128.59.16.4)newworld.cs.umass.edu(128.119.245.93)
-
TYPES OF ADDRESS STRUCTURESStruct sockaddrIt holds the socket informationStruct in_addrIt holds 32 bit netid/hostid.Struct sockaddr_in/IPv4 Socket AddressIt holds the info about family, port number, internet address and size of the structStruct hostentStruct serventStruct sockaddr_in6/IPv6 Socket AddressStruct sockaddr_storageIts having large enough socket address type
*Relevant info about host and ports
-
*PortsPort 0Port 1Port 65535Each host has 65,536 portsSome ports are reserved for specific apps20,21: FTP23: Telnet80: HTTP
A socket provides an interface to send data to/from the network through a port
-
*Address and port byte-orderingAddress and port are stored as integersu_short sin_port; (16 bit)in_addr sin_addr; (32 bit)
struct in_addr { u_long s_addr;};Problem:different machines / OSs use different word orderingslittle-endian: lower bytes firstbig-endian: higher bytes firstthese machines may communicate with one another over the network
128.119.40.1212.40.119.128Big-EndianmachineLittle-EndianmachineWRONG!!!
-
*UNIXs byte-ordering funcsu_long htonl(u_long x);u_short htons(u_short x);u_long ntohl(u_long x);u_short ntohs(u_short x);
On big-endian machines, these routines do nothingOn little-endian machines, they reverse the byte order
Same code would have worked regardless of endian-ness of the two machines128.119.40.12128.119.40.12Big-EndianmachineLittle-Endianmachine
-
Address conversion FunctionFunction converting IPv4Inet_atonit converts address to numberInet_ntoanetwork address to numberInet_addrinternet host address to numberFunction converting IPv6Inet_ptonIt convert text to binaryInet_ntopIt converts IPv4 and IPv6 from binary to text form*