Introduction to Socket Programming April 2010. What is a socket? An interface between application and network –The application creates a socket –The socket.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
CS x760 Computer Networks1 Socket Programming. CS 6760 Computer Networks2 Socket Programming  What is a socket?  Using sockets  Types (Protocols) ‏
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Berkeley Sockets. Client-server model Sockets interface: Address structure, byte ordering, port no Socket primitives: socket, bind,listen… Example code.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Chapter4 Elementary TCP Socket
Socket Programming in C
Transport layer API: Socket Programming
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming in C
TCP Sockets Programming
Socket Programming.
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Introduction to Socket Programming April 2010

What is a socket? An interface between application and network –The application creates a socket –The socket type dictates the style of communication connection-oriented vs. connectionless Once configured, the application can –pass data to the socket for network transmission –receive data from the socket (transmitted through the network by some other host)

Two essential types of sockets SOCK_STREAM –TCP sockets –reliable delivery –in-order guaranteed –connection-oriented SOCK_DGRAM –UDP sockets –unreliable delivery –no order guarantees –no notion of “ connection ” App socket Dest. App socket D1 D3 D2 We’ll look at this one

Client – high level view Create a socket Setup the server address Connect to the server Read/write data Shutdown connection

Some utility functions Byte Ordering: Host Byte Order to Network Byte Order: htons(), htonl() Network Byte Order to Host Byte Order: ntohs(), ntohl() IP Address format: Ascii dotted to Binary: inet_aton() Binary to Ascii dotted: inet_ntoa() Many others exist …… explore the man pages :D

Socket() – A Connection Endpoint This creates an endpoint for a network connection. int socket(int domain, int type, int protocol) domain = PF_INET (IPv4 communication) type = SOCK_STREAM (TCP), SOCK_DGRAM (UDP) protocol = 0 (for our discussion) Example : socket(PF_INET, SOCK_STREAM, 0); This will create a TCP socket. The call returns a socket descriptor on success and - 1 on an error.

int connect_ socket(char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket(AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname(hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons(port); sin. sin_ addr.s_ addr = *(unsigned long *) host -> h_ addr_ list[0]; if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0) { close (sock); return -1; } return sock; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Hostent structure struct hostent{ char * h_name/*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length/* length of address */ char ** h_addr_list/*prt to array of ptrs with \ IPv4 or IPv6 address*/ } Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr/* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Make the socket Socket(int family, int type, int protocol); return nonnegative value for OK, -1 for error

Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection

Bind() – Attaching to an IP and Port A server process calls bind to attach itself to a specific port and IP address. int Bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) sockfd = socket descriptor returned by socket() my_addr = pointer to a valid sockaddr_in structure cast as a sockaddr * pointer addrlen = length of the sockaddr_in structure Example : struct sockaddr_in my; my.sin_family = PF_INET; my.sin_port = htons(80); my.sin_addr.s_addr = INADDR_ANY; bzero(&my, 8) bind(sock, (struct sockaddr *)&my, sizeof(my));

Listen() – Wait for a connection The server process calls listen to tell the kernel to initialize a wait queue of connections for this socket. Int Listen(int sock, int backlog) sock = socket returned by socket() backlog = Maximum length of the pending connections queue Listen() returns -1 on error (otherwise 0). Example: Listen(sock, 10); This will allow a maximum of 10 connections to be in pending state.

Listening on a port (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return -1; if(listen(sock,10) < 0) return -1; return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/

Accept() – A new connection ! Accept is called by a Server process to accept new connections from new clients trying to connect to the server. Int Accept(int socket, (struct sockaddr *)&client, socklen_t *client_len) socket = the socket in listen state client = will hold the new client’s information client_len = pointer to size of the client structure Example : struct sockaddr_in client; int len = sizeof(client); Accept(sock, (struct sockaddr *)&client, &len);

Accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error. After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls. read() and write() operate a little differently on sockets (vs. file operation)!

Send / Recv – Finally Data !! Send(), Recv(), Read(), Write() etc calls are used to send and receive data. Int send(int sock, void *mesg, size_t len, int flags) Int recv(int sock, void *mesg, size_t len, int flags) sock = A connected socket mesg = Pointer to a buffer to send/receive data from/in. len = Size of the message buffer flags = 0 (for our purpose) The return value is the number of bytes actually sent/received. Example: char send_buffer[1024]; char recv_buffer[1024]; int sent_bytes; int recvd_bytes; sent_bytes = send(sock, send_buffer, 1024, 0); recvd_bytes = recv(sock, recv_buffer, 1024, 0);

socket() bind() listen() accept() read() write() read() close() Socket() connect() write() read() close() TCP Client TCP Server Well-known port blocks until connection from client process request Connection establishment Data(request) Data(reply) End-of-file notification

Dealing with blocking calls Many functions block –accept(), connect(), recv() For simple programs this is fine What about complex connection routines –Multiple connections –Simultaneous sends and receives –Simultaneously doing non-networking processing

Dealing with blocking (cont..) Options –Create multi-process or multi-threaded code –Turn off blocking feature (fcntl() system call) –Use the select() function What does select() do? –Input: a set of file descriptors –Output: info on the file-descriptors’ status –Therefore, can identify sockets that are “ready for use”: calls involving that socket will return immediately

select function call int status = select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *Timeout); –Status: # of ready objects, -1 if error –nfds: 1 +largest file descriptor to check –readfds: list of descriptors to check if read-ready –writefds: list of descriptors to check if write-ready –exceptfds: list of descriptors to check if an exception is registered –Timeout: time after which select returns

Four parts of first project 0: Get build, configure and run the minet stack 1: HTTP Client 2: Connection-at-a-time HTTP Server 3: Simple select-based Multiple- connection-at-a-time server 4: Complex …. ( Extra Credit )