Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Sockets. Socket Berkeley Software Distribution Handle-like data structure for communicating A socket is an endpoint  Send and receive  Attach a protocol.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 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.
Tutorial 8 Socket Programming
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
Introduction to Project 1 Web Client and Server Jan 2006.
Yu-Chi Lai Lecture 3 Network Programming CS 640: Computer Networking.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
UNIX Socket Programming CS 6378
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.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
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.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
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.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
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.
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()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming 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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Client-Server model. Socket programming 
Socket programming Péter Verhás August 2002
Socket Programming in C
Network Programming with Sockets
Tutorial on Socket Programming
Sockets.
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Yu-Chi Lai Lecture 3 Network Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Sockets.
Presentation transcript:

Socket Programming: a Primer Socket to me!

Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network

Feb. 23, 2001EE122, UCB3 So what exactly does a socket do? It is an API between applications and network protocol software Functions it provides: –Define an “end-point” for communication –Initiate and accept a connection –Send and receive data –Terminate a connection gracefully Supports multiple protocol families –Examples: Unix inter-process communication, TCP/IP –Only Internet sockets will be covered in this lecture

Feb. 23, 2001EE122, UCB4 Types of Sockets Two different types of sockets : –stream vs. datagram Stream socket : (a.k.a. connection-oriented socket) –It provides reliable, connected networking service –Error free; no out-of-order packets (uses TCP) –applications: telnet, http, … Datagram socket : (a.k.a. connectionless socket) –It provides unreliable, best-effort networking service –Packets may be lost; may arrive out of order (uses UDP) –applications: streaming audio/video, …

Feb. 23, 2001EE122, UCB5 How should one define a socket? To define an end-point of communication, one needs to specify –the family of protocol it uses (Internet vs. others) –addressing information (IP address + port number) –the type of service it provides (stream vs. datagram) Done in three steps –create a socket –define address and port number –associate address with the socket

Feb. 23, 2001EE122, UCB6 How to create a socket? # include int sock; sock = socket (AF_INET, SOCK_STREAM, 0); /* for stream */ sock = socket (AF_INET, SOCK_DGRAM, 0); /* for datagram */ Notice that the socket descriptor is just a regular int ! So it has the same usage as a file descriptor in Unix…

Feb. 23, 2001EE122, UCB7 How to define address? struct sockaddr { u_short sa_family; char sa_data[14]; } struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; } struct in_addr { u_long s_addr; } WARNING: Don’t forget to convert byte orders! htons, htonl, ntohs, ntohl

Feb. 23, 2001EE122, UCB8 Bind a Socket bind( ): associate a socket descriptor with an address putting everything together int bind (int sockfd, struct sockaddr *addr, int len); int sockfd; struct sockaddr_in addr; sockfd=socket(AF_INET,SOCKE_STREAM, 0); addr.sin_family=AF_INET; addr.sin_port=htons(5000); /* 0: randomly assigned by OS */ addr.sin_addr.s_addr=htonl(INADDR_ANY); /* local address */ bzero(&(addr.sin_zero),8); /* pad zeros */ bind(sockfd,(struct sockaddr *)&addr, sizeof(struct sockaddr));

Feb. 23, 2001EE122, UCB9 How to convert addresses? You also need to define address for the other end If you know its IP address addr.sin_addr.s_addr=inet_addr(“ ”); If you know its name only: –need to perform a DNS lookup struct hostent *gethostbyname(char *name) struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0] } struct hostent *h; struct in_addr *inad; h=gethostbyname(“cory.eecs”); inad=(struct in_addr *) h->h_addr; addr.sin-addr.s_addr=inad;

Feb. 23, 2001EE122, UCB10 Using a Datagram Socket Sending data int sendto(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *dest, int dest_len); Receiving data int recvfrom(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *src, int src_len);

Feb. 23, 2001EE122, UCB11 Using a Stream Socket establishing a connection int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *addr, int addr_len); int accept(int sockfd, void *addr, int *addrlen ); listen () connect( ) accept () client server

Feb. 23, 2001EE122, UCB12 Using a Stream Socket (contd) Sending data int send(int sockfd, void *msg, int msg_len, u_short flags); Receiving data int recv(int sockfd, void *msg, int msg_len, u_short flags); Notice that no address is required!

Feb. 23, 2001EE122, UCB13 A Quick Summary: Datagram socket() to create socket bind() to a receiving port recvfrom() sendto () socket() to create socket bind() to any port recvfrom() sendto () server client

Feb. 23, 2001EE122, UCB14 A Quick Summary: Stream socket() server bind() to a receiving port listen () to socket bind() to any port connect () To server client accept () connection socket() send () recv () send () recv ()

Feb. 23, 2001EE122, UCB15 Sample Codes: Datagram Client #include #define Bfsize 1024 main(int argc, char *argv[]) { int sock; struct sockaddr_in client, server; struct hostent *host, *gethostname(); sock=socket(AF_INET, SOCK_DGRAM, 0); /* open socket */ client.sin_family=AF_INET; client.sin_addr.s_addr=htonl(INADDR_ANY); /* local addr */ client.sin_port=htons(0); /* any port # */ bind(sock,(struct sockaddr *)&client,sizeof(client));

Feb. 23, 2001EE122, UCB16 Sample Code: Datagram Client host=gethostbyname(argv[1]); /* get host name */ memcpy((char *)&server.sin_addr, (char *)host->h_addr, host->h_length); server.sin_family=AF_INET; server.sin_port=htons(atoi(argv[2])); sendto(sock,msg,sizeof(msg),0,(struct sockaddr *)&server, sizeof(server)); close(sock); } Don’t forget error handling when calling these functions in your programs!

Feb. 23, 2001EE122, UCB17 Sample Code: Datagram Server #include #define Bfsize 1024 main(int argc, char *argv[]) { int sock, length, count; struct sockaddr_in server, client; char buffer[Bfsize]; sock=socket(AF_INET, SOCK_DGRAM,0); server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(atoi(argv[1])); /* listening port */ bind(sock,(struct sockaddr *)&server,sizeof(server));

Feb. 23, 2001EE122, UCB18 Sample Code: Datagram Server count=recvfrom(sock, buffer, Bfsize, 0, (struct sockaddr *)&client,&length); printf("---> %s\n", buffer); close(sock); } Note that we don’t have to define client’s address here, because the server can receive from any one on sock. After return from recvfrom, client contains sender’s address information.

Feb. 23, 2001EE122, UCB19 Sample Code: Stream Client #include #define msg “hello ee122“ main(int argc, char *argv[]) { int sock; struct sockaddr_in client, server; struct hostent *host, *gethostname(); host=gethostbyname(argv[1]); memcpy((char *)&server.sin_addr,(char *)host->h_addr,host->h_length); server.sin_family=AF_INET; server.sin_port=htons(atoi(argv[2])); /* no bind is needed! */ sock=socket(AF_INET, SOCK_STREAM,0); connect(sock,(struct sockaddr *)&server, sizeof(server));

Feb. 23, 2001EE122, UCB20 Sample Code: Stream Client send(sock,msg,sizeof(msg),0); close(sock); }

Feb. 23, 2001EE122, UCB21 Sample Code: Stream Server #include #define Bfsize 1024 main(int argc, char *argv[]) { int new_sock, sock, length, count; struct sockaddr_in server, client; struct hostent *host, *gethostbyname(); char buffer[Bfsize]; sock=socket(AF_INET, SOCK_STREAM,0); server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(atoi(argv[1])); bind(sock, (struct sockaddr *)&server, sizeof(server)); listen(sock,1);

Feb. 23, 2001EE122, UCB22 Sample Code: Stream Server new_sock = accept(sock,(struct sockaddr *)&client,&length); count=recv(new_sock,buffer,Bfsize,0); printf("---> %s\n", buffer); close(new_sock); close(sock); }

Feb. 23, 2001EE122, UCB23 Further Reading W. Richard Stevens. Unix Network Programming, Prentice Hall. The Unix Socket FAQ. faq