ccnet-lec-14-socket-prog
Post on 08-Apr-2018
221 Views
Preview:
TRANSCRIPT
-
8/6/2019 ccnet-lec-14-socket-prog
1/29
Lecture
14Data Communication & Networks
Socket Programming
(A Developers Interface to Network)
Muhammad Yousaf
-
8/6/2019 ccnet-lec-14-socket-prog
2/29
myousaf@ymail.com 2
Developer Vs. Network Designer:
Developer views the network from programming perspective
Concern about Application Programming Interfaces (API),ease of development, flexibility, etc
Network Designer views the network from actualimplementation perspective
Concerns about communication issues, problems, theirsolutions, protocols, etc
-
8/6/2019 ccnet-lec-14-socket-prog
3/29
myousaf@ymail.com 3
Programmers perspective:
Why a programmer should learn about network?Demand for network applications is increasing
In Internet architecture most of the intelligence lies in theend-hosts (programmable machines)
New services on existing network can be launchedmerely by developing new applications
Knowledge of network helps programmer to built networkapplications in a better way
Developer can think about the implementation ofinnovative distributed applications, e.g. remote digitallibrary, remote shopping, entertainment, distributed OS,distributed databases, etc.
-
8/6/2019 ccnet-lec-14-socket-prog
4/29
myousaf@ymail.com 4
Network Designers perspective:
Why a network designer should learn aboutprogramming?
We all demand an actually working network
We dont need a network that only lies in books & papers
Programming helps to actually implement networkingprotocols
IETFs motto: WE BELIEVE IN WORKING CODE
-
8/6/2019 ccnet-lec-14-socket-prog
5/29
myousaf@ymail.com 5
Programming over network:
Socket programming provides most popularinterface to the networking stack
Although other alternatives are also available
e.g. Sun RPC (Remote Procedure Calls)
Socket APIs were introduced in 1981 with BSD-4.1Unix
Implemented as a set of library/system calls
Revolves around client/server architecture
-
8/6/2019 ccnet-lec-14-socket-prog
6/29
myousaf@ymail.com 6
Socket Programming:
Sockets API provide:A transport layer service interface
Similar interfaces to TCP (stream-socket) and UDP(datagram-socket)
Can also serve as interface to IP (raw-socket)Support for both IPv4 & IPv6 is available
Linux also provides interface to MAC layer (data-link-socket)
Some other type of sockets are also available (Unix-domain-socket, routing-socket, etc.)
-
8/6/2019 ccnet-lec-14-socket-prog
7/29
myousaf@ymail.com 7
Socket Programming: cont
Flexibility & Uniformity of socket interface
Socket Interface
TCP UDP
IP
Network Access
-
8/6/2019 ccnet-lec-14-socket-prog
8/29
myousaf@ymail.com 8
Socket Programming: cont
Recall client/server interaction procedure:
Server:Create socket at server side
Binds with well-known Port number & IP address
Listen for the connection requests
Client:Create socket at client side
Sends a connection request
Server:Accept connection request
Client/Server:Send/Receive user Data
Either Client or Server Close the connection
-
8/6/2019 ccnet-lec-14-socket-prog
9/29
myousaf@ymail.com 9
Creating Sockets:
int socket (int family, int type, int protocol);
Create a socket. Returns file descriptor or -1. Also sets errnoon failure.
family: address family / protocol family
AF_INET for IPv4
other possibilities: AF_INET6 (IPv6), AF_UNIX, AF_OSI, AF_LOCAL(Unix socket), AF_ROUTE (routing)
type: type of communication
SOCK_STREAM for TCPSOCK_DGRAM for UDP
protocol: protocol within family
typically 0 (default)
-
8/6/2019 ccnet-lec-14-socket-prog
10/29
myousaf@ymail.com 10
Binding the socket:
int bind (int sockfd, struct sockaddr* serverAddr, int addrlen);Bind a socket to a local IP address and port number. Returns0 on success, -1 and sets errno on failure.
sockfd: socket file descriptor (returned from socket)serverAddr: includes IP address and port number
IP address: set by kernel if value passed is INADDR_ANY, else setby caller
port number: set by kernel if value passed is 0, else set by caller
addrlen: length of address structure
sizeof (struct sockaddr_in)
-
8/6/2019 ccnet-lec-14-socket-prog
11/29
myousaf@ymail.com 11
Waiting for Connections:
int listen (int sockfd, int backlog);Put socket into passive state (wait for connections rather thaninitiate a connection). Returns 0 on success, -1 and setserrno on failure.
sockfd : socket file descriptor
backlog : bound on length of un-accept()ed connection queue(connection backlog)
-
8/6/2019 ccnet-lec-14-socket-prog
12/29
myousaf@ymail.com 12
Sending connection request:
int connect (int sockfd, struct sockaddr* servaddr, int addrlen);Connect to another socket. Returns 0 on success, -1 and setserrno on failure.
sockfd : socket file descriptorservaddr : IP address and port number of server
addrlen : length of address structure
-
8/6/2019 ccnet-lec-14-socket-prog
13/29
myousaf@ymail.com 13
Accepting a connection request:
int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen);Accept a new connection (first one off queue of pendingconnections). Returns file descriptor or -1. Also sets errno.
sockfd : socket file descriptorcliaddr : IP address and port number of client (when returned)
addrlen : length of address structure
addrlen is a value-result argument:
caller passes the size of address structure,
kernel returns the size of clients address (number of
bytes written)
-
8/6/2019 ccnet-lec-14-socket-prog
14/29
myousaf@ymail.com 14
Sending Data:
int write (int sockfd, char* buf, size_t nbytes);Write data to a stream (TCP) or connected datagram
(UDP) socket. Returns number of bytes written or -1.Also sets errno on failure
sockfd : socket file descriptor
buf: data buffer
nbytes : number of bytes to try to write
int send (int sockfd, char* buf, size_t nbytes , int flags);
-
8/6/2019 ccnet-lec-14-socket-prog
15/29
myousaf@ymail.com 15
Sending Data: cont
int sendto (int sockfd, char* buf, size_t nbytes, int flags, structsockaddr* destaddr, int addrlen);
Send a datagram to another UDP socket. Returns number ofbytes written or -1. Also sets errno on failure.
sockfd: socket file descriptor
buf: data buffer
nbytes : number of bytes to try to read
flags : typically use 0
destaddr : IP address and port number of destination socketaddrlen : length of address structure
-
8/6/2019 ccnet-lec-14-socket-prog
16/29
myousaf@ymail.com 16
Receiving Data:
int read (int sockfd, char* buf, size_t nbytes);
Read data from a stream (TCP) or connected datagram
(UDP) socket. Returns number of bytes read or -1. Alsosets errno on failure. Returns 0 if socket closed.
sockfd : socket file descriptor
buf: data buffer
nbytes : number of bytes to try to read
int recv (int sockfd, char* buf, size_t nbytes , int flags);
-
8/6/2019 ccnet-lec-14-socket-prog
17/29
myousaf@ymail.com 17
Receiving Data: cont
int recvfrom (int sockfd, char* buf, size_t nbytes, int flags,struct sockaddr* srcaddr, int* addrlen);
Read a datagram from a UDP socket. Returns number of bytesread (0 is valid) or -1. Also sets errno on failure.
sockfd :socket file descriptor
buf: data buffer
nbytes : number of bytes to try to read
flags : typically use 0
srcaddr : IP address and port number of sending socket(returned from call)
addrlen : length of address structure
-
8/6/2019 ccnet-lec-14-socket-prog
18/29
myousaf@ymail.com 18
Closing a connection:
int close (int sockfd);Closes a socket and deletes descriptor from system
tables. Returns 0 on success, -1 and sets errno onfailure.
sockfd : socket file descriptor
All data sent before close are delivered to other sideAfter close() , sockfd is not valid for reading orwriting.
-
8/6/2019 ccnet-lec-14-socket-prog
19/29
myousaf@ymail.com 19
Example code:
Following a sample code for client and serverCode is written in simple C-Language
System used: Linux
Although server and client are doing very simple
work, but it will give an idea about networkprogramming
Code can be compiled/run over command promptterminal1# gcco server server.c
./serverterminal2# gcco client client.c
./client serverAddress
-
8/6/2019 ccnet-lec-14-socket-prog
20/29
myousaf@ymail.com 20
server.c: (include, define)
#include
#include
#include
#include
#include #include
#include
#include
#define PORT 1233 /* my well-known port */
#define BACKLOG 10/* length of pending connections queue */
-
8/6/2019 ccnet-lec-14-socket-prog
21/29
myousaf@ymail.com 21
server.c: (main, socket)
main()
{
int sockfd, new_fd;/*listen on sockfd, new connection on new_fd */
struct sockaddr_in my_addr; /* servers address */
struct sockaddr_in their_addr; /* client addr */int sin_size; /* address size */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");exit(1);
}
-
8/6/2019 ccnet-lec-14-socket-prog
22/29
myousaf@ymail.com 22
server.c: (bind)
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* convert to short,network byte order */
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);/* automatically fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr)) == -1) {perror("bind");
exit(1);
}
-
8/6/2019 ccnet-lec-14-socket-prog
23/29
myousaf@ymail.com 23
server.c: (listen, accept)
if(listen(sockfd, BACKLOG) == -1) {
perror("listen"); exit(1);
}
while(1) { /* main accept() loop ; always true*/
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size)) == -1) {
perror("accept"); continue;
}
printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
-
8/6/2019 ccnet-lec-14-socket-prog
24/29
myousaf@ymail.com 24
server.c: (send, close)
if(!fork()) { /* this is the child process */
close(sock_fd); /* child doesn't need it */
if(send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd); exit(0);
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0);/* clean up allchild processes */
}/* outer while loop ends here */
} /* main function ends here */
-
8/6/2019 ccnet-lec-14-socket-prog
25/29
myousaf@ymail.com 25
client.c: (include, define)
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 1233 /* servers well-known port */
#define MAXDATASIZE 100/* max number of bytes we canget at once */
-
8/6/2019 ccnet-lec-14-socket-prog
26/29
myousaf@ymail.com 26
client.c: (main)
int main (int argc, char* argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent* he;
struct sockaddr_in their_addr;/* connectors address information */if(argc != 2) {
fprintf (stderr, usage: client hostname\n); exit(1);
}
if((he = gethostbyname (argv[1])) == NULL) {/* get the host info */
perror (gethostbyname); exit (1);
}
-
8/6/2019 ccnet-lec-14-socket-prog
27/29
myousaf@ymail.com 27
client.c: (socket, connect)
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror (socket); exit (1);
}
their_addr.sin_family = AF_INET;/* interpreted by host */
their_addr.sin_port = htons (PORT);
their_addr.sin_addr = *((struct in_addr*)he->h_addr);
bzero (&(their_addr.sin_zero), 8);/* zero the rest of struct */
if(connect (sockfd, (struct sockaddr*)&their_addr, sizeof(struct sockaddr)) == -1) {
perror (connect); exit (1);
}
-
8/6/2019 ccnet-lec-14-socket-prog
28/29
myousaf@ymail.com 28
client.c: (receive, close)
if((numbytes = recv (sockfd, buf, MAXDATASIZE, 0))== -1) {
perror (recv);
exit (1);
}
buf[numbytes] = \0;
printf (Received: %s, buf);
close (sockfd);
return 0;
}/* main function ends */
-
8/6/2019 ccnet-lec-14-socket-prog
29/29
myousaf@ymail com 29
Questions ???
top related