Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.

Similar presentations


Presentation on theme: "TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows."— Presentation transcript:

1 TCP Socket Programming

2 r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows to processes on remotely connected computers to talk to each other What is a socket?

3 r SOCK_STREAM  TCP  connection oriented, bidirectional  reliable, in-order delivery Two types of sockets SOCK_DGRAM  UDP  no connection  unreliable delivery, no guarantee on the order  can send/receive

4 CPSC 441 - Application Layer 4 Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet

5 CPSC 441 - Application Layer 5 Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that welcomes client’s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client  allows server to talk with multiple clients  source port numbers used to distinguish clients TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint

6 r Used to address processes on a host  0-1023 is usually reserved for known service  1024-49151 are registered. They are used for multiple purposes  49152-65535 are private ports Ports Transport Layer FTP Server Web Server 21 80 Network Layer DLL/Physical

7 Socket Programming in C CPSC 441 - Application Layer 7

8 Socket Programming - Flow socket() connect()‏ send() recv() close()... socket() bind() listen() accept() recv() send() close() wait for connection request from next client Client Server

9  int s_listen = socket(family, type, protocol);  family: AF_INET specifies Ipv4  type: SOCK_STREAM, SOCK_DGRAM  protocol: 0 (pseudo, IP ). See /etc/protocols socket() ‏

10 connect() r int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); r Connect to server.  sockfd is socket descriptor from socket() r servaddr is a pointer to a structure with:  port number and IP address  must be specified (unlike bind() ) r addrlen is length of structure  client doesn’t need bind()  OS will pick ephemeral port r returns socket descriptor if ok, -1 on error CPSC 441 - Application Layer 10

11  bind(s_listen, localAdd, addLength)  Server specifies which port and address it will be listening to  s_listen: our listening socket descriptor  localAdd: socket address structure  addLength: length of localAdd bind()

12 struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { u_char sin_len; /* length of address */ u_char sin_family; /* family of address, Ex. AF_INET */ u_short sin_port; /* Protocol (TCP/UDP) Port num */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* set to zero, used for padding */ }; Socket Address Structure

13 r Declare address structure  struct sockaddr_in sockAdd; r Set family  sockAdd.sin_family = AF_INET; r Set IP address (2 ways) //specify address to listen to  inet_pton(AF_INET, “127.0.0.1”, &sockAdd.sin_addr.s_addr)‏ //listen to any local address  sockAdd.sin_addr.s_addr = htonl(INADDR_ANY) r Set port  sockAdd.sin_port = htons(9999); Address Structure

14  int status = listen(s_listen, queuelength);  status: -1 if error, 0 otherwise  s_listen: socket descriptor  queuelength: Number of clients that can “wait” for a connection  listen is non-blocking: returns immediately listen()

15  int s_new = accept(s_listen, &clientAddress, &addLength);  s_new: new socket for communication with client  s_listen: the listening socket  clientAddress: struct sockaddr, address of client  addLength: size of client address structure  accept is blocking: waits for connection before returning accept()

16  int send(int s_new, const void *buf, int len, int flags); s_new – socket descriptor buf – pointer to buffer len – size of buffer flags – can be safely set to 0  int recv(int s_new, void *buf, int len, unsigned int flags); similar to send buf holds the data to be transferred Talking

17 r fork() is a C system call used to spawn child processes  Execution for both child and parent process continues at the next instruction  fork() returns 0 if this is the child process PID (>0) of the child process if this is the parent <0 if fork() fails r Used to keep listening on socket and talking on another socket System calls - fork()

18 Demo A simple client – server example: Echo Server

19 r Socket Programming, Dan Rubinstein, http://www1.cs.columbia.edu/~danr/courses/6761/Fall00/intro/6761-1b- sockets.ppt http://www1.cs.columbia.edu/~danr/courses/6761/Fall00/intro/6761-1b- sockets.ppt r 15-441 Socket Programming, www.cs.cmu.edu/afs/cs/academic/class/15441- f01/www/lectures/lecture03.pptwww.cs.cmu.edu/afs/cs/academic/class/15441- f01/www/lectures/lecture03.ppt r Network Programming, Geoff Kuenning, www.cs.hmc.edu/~geoff/classes/hmc.cs105.200701/slides/class21_net2.ppt www.cs.hmc.edu/~geoff/classes/hmc.cs105.200701/slides/class21_net2.ppt r Socket Programming, Abhinav Jain, www.cs.purdue.edu/homes/jain8/cs422/pso3.ppt www.cs.purdue.edu/homes/jain8/cs422/pso3.ppt References


Download ppt "TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows."

Similar presentations


Ads by Google