Example: bankruptcy

Sockets Programming in C using TCP/IP

Sockets Programming in C using TCP/IP TA: Awad A Younis Class: CS457 Fall 2014 Computer Networks: Consists of Machines Interconnected by communication channels Machines are Hosts and Routers Hosts run applications Routers forward information among communication channels Communication channels is a means of conveying sequences of bytes from one host to another (Ethernet, dial-up, satellite, etc.) Packets: Sequences of bytes that are constructed and interpreted by programs A packet contains Control information: oUsed by routers to figure out how to forward every packet. packet destination User data Protocol: An agreement about the packets exchanged by communicating programs and what they mean. A protocol tells how packets are structured owhere the distention information is located in the packet ohow big it is Protocols are designed to solve specific problems TCP/IP is such collection of solutions (protocol suite or family): oIP, TCP, UDP, DNS, ARP, HTTP, and many more How can we access the services provided by TCP/IP suite?

• In TCP/IP, it takes two piece of information: Internet Address, used by IP (e.g. Company’s main phone number ) Port Number, interpreted by TCP & UDP (extension number of …

Information

Domain:

Source:

Link to this page:

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

Other abuse

Transcription of Sockets Programming in C using TCP/IP

1 Sockets Programming in C using TCP/IP TA: Awad A Younis Class: CS457 Fall 2014 Computer Networks: Consists of Machines Interconnected by communication channels Machines are Hosts and Routers Hosts run applications Routers forward information among communication channels Communication channels is a means of conveying sequences of bytes from one host to another (Ethernet, dial-up, satellite, etc.) Packets: Sequences of bytes that are constructed and interpreted by programs A packet contains Control information: oUsed by routers to figure out how to forward every packet. packet destination User data Protocol: An agreement about the packets exchanged by communicating programs and what they mean. A protocol tells how packets are structured owhere the distention information is located in the packet ohow big it is Protocols are designed to solve specific problems TCP/IP is such collection of solutions (protocol suite or family): oIP, TCP, UDP, DNS, ARP, HTTP, and many more How can we access the services provided by TCP/IP suite?

2 Sockets API. Addresses: Before one program can communicate with another program, it has to tell the network where to find the other program In TCP/IP , it takes two piece of information: Internet Address, used by IP ( Company s main phone number ) Port Number, interpreted by TCP & UDP (extension number of an individual in the company) Client and server Server: passively waits for and responds to clients Client: initiates the communication must know the address and the port of the server Socket(): endpoint for communication Bind(): assign a unique number Listen(): wait for a caller Connect(): dial a number Accept(): receive a call Send() and Receive(): Talk Close(): Hang up Server a TCP socket using socket() a port number to the socket with bind() the system to allow connections to be made to that port using listen() do the following: Call accept() to get a new socket for each client connection communicate with the client using send() and recv() Close the client connection using close() Client a TCP socket using socket() a connection to server using connect() using send() and recv() connection using close() Why socket Programming ?

3 To build network applications. Firefox, google chrome, etc. Apache Http server What is a socket? It is an abstraction through which an application may send and receive data File is an analogy: read (receive) and write (send) Types of Sockets Stream Sockets (TCP): reliable byte-stream service Datagram Sockets (UDP): best effort datagram service What is a socket API? An interface between application and network Applications access the services provided by TCP and UDP through the Sockets API Specifying Addresses Applications need to be able to specify Internet address and Port number. How? Use Address Structure : generic data type : internet address : another view of Sockaddr struct sockaddr_in{ unsigned short sin_family; /* Internet protocol (AF_INET) */ unsigned short sin_port; /* Address port (16 bits) */ struct in_addr sin_addr; /* Internet address (32 bits) */ char sin_zero[8]; /* Not used */ } Create a socket int socket(int protocolFamily, int type, int protocol) protocolFamily: Always PF_INET for TCP/IP Sockets type: Type of socket (SOCK_STREAM or SOCK_DGRAM) protocol: Socket protocol (IPPROTO_TCP or IPPROTO_UDP) socket () returns the descriptor of the new socket if no error occurs and -1 otherwise.

4 Example: #include < > #include < > int servSock; if ((servSock= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) Bind to a socket int bind(int socket, struct sockaddr *localAddress, unsigned int addressLength) socket: Socket (returned by socket ()) localAddress: Populated sockaddr structure describing local address address Length: Number of bytes in sockaddr structure--usually just size o f ( localAddress ) bind() returns 0 if no error occurs and - 1 otherwise. Example: struct sockaddr_in ServAddr; = AF_INET; /* Internet address family = htonl(INADDR_ANY); /* Any incoming interface = htons(ServPort); /* Local port */ if (bind(servSock, (struct sockaddr *) &ServAddr, sizeof(echoServAddr)) < 0) Listen to incoming connections int listen(int socket, int backlog) socket: Socket (returned by socket ()) backlog: Maximum number of new connections ( Sockets ) waiting listen() returns 0 if no error occurs and - 1 otherwise.

5 Example: #define MAXPENDING 5 if (listen(servSock, MAXPENDING) < 0) Accept new connection int accept(int socket, struct sockaddr * clientAddress, int * addressLength ) socket: Socket (listen() already called) clientAddress: Originating socket IP address and port addressLength: Length of sockaddr buffer (in), returned address (out) accept () returns the newly connected socket descriptor if no error occurs and -1 otherwise. Example: #define MAXPENDING 5 if ((clientSock=accept(servSock,(structsock addr*)&ClntAddr,&clntLen))<0) Constricting a Message data: array vs struct ordering: htonl/htons vs ntohl/ntohs and Padding: int/unsigned short and int/unsigned short Some helpful resources: ~danr/courses/6761/Fall00/hw/pa1 (Beej s Guide to Network Programming using Internet Sockets ) (Book: TCP/IP Sockets in C Practical Guide for Programmers) 3.

6 4. Thank You Reference Pocket Guide to TCP/IP Socket, by Michael J. Donahoo and Kenneth L. Calvert Beej s Guide to Network Programming using Internet Sockets , by Brian "Beej" Hall. ( ~danr/courses/6761/Fall00/hw/pa1 )


Related search queries