Socket Programming 101 Vivek Ramachandran.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Socket Programming Computer Networks, Spring 2008 Your TAs.
1 Sockets Nick Feamster Computer Networking I Spring 2013.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne) Data Communication: Introduction to Berkeley Sockets INF1060: Introduction to Operating.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne)
CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
IPv6 Technologies and Advanced Services page 1 Porting applications to IPv6 OpenH323 and IPv6 support K. Stamos Computer Engineer, University of Patras.
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
CS252: Systems Programming Ninghui Li Based on slides by Prof. Gustavo Rodriguez-Rivera Topic 16: Socket Programming & Project 5.
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.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications 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.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
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
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
Socket Programming.
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Berkeley Sockets. Client-server model Sockets interface: Address structure, byte ordering, port no Socket primitives: socket, bind,listen… Example code.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Network Programming with Sockets
Tutorial on Socket Programming
Transport layer API: Socket Programming
Socket Programming in C
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Presentation transcript:

Socket Programming 101 Vivek Ramachandran

Why Socket Programming ? “To build any Networked Application” WWW ( Internet Explorer , Firefox ) FTP (WS FTP ) P2P (Limewire, Bitcomet)

Pre - Requisites Basics of IP / TCP / UDP C programming Basics Data Structures basics Compilation of code on Linux / Unix using gcc etc Same principles can be used to code on windows as well using the winsock library.

Network Programming huh ? Telephone Analogy A telephone call over a “telephony network” works as follows: Both parties have a telephone installed. A phone number is assigned to each telephone. Turn on ringer to listen for a caller. Caller lifts telephone and dials a number. Telephone rings and the receiver of the call picks it up. Both Parties talk and exchange data. After conversation is over they hang up the phone.

Dissecting the Analogy A network application works as follows: An endpoint (telephone) for communication is created on both ends. An address (phone no) is assigned to both ends to distinguish them from the rest of the network. One of the endpoints (caller) initiate a connection to the other. The other end (receiver) point waits for the communication to start. Once a connection has been made, data is exchanged (talk). Once data has been exchanged the endpoints are closed (hang up).

In the world of sockets…… Socket() – Endpoint for communication Bind() - Assign a unique telephone number. Listen() – Wait for a caller. Connect() - Dial a number. Accept() – Receive a call. Send(), Recv() – Talk. Close() – Hang up.

The Client – Server model Server – An entity which is a provider of information. Client – An entity which is a seeker of information. Example – Apache is a web server providing web pages (information) and Internet Explorer is a web client which requests those pages from the server. In the socket programming world almost all communication is based on the Client-Server model. The Server starts up first and waits for a client to connect to it. After a client successfully connects, it requests some information. The Server serves this information to the client. The client then disconnects and the Server waits for more clients.

A TCP Server – Client Interaction

A UDP Server – Client Interaction

Before we dive deeper … Data Structures Let us now look at the data structures used to hold all the address Information: Struct sockaddr { unsigned short sa_family; char sa_data[14]; } Struct sockaddr_in { short sin_family; unsigned short sin_port; // Port Number struct in_addr sin_addr; // IP Address char sin_zero[8]; } Struct in_addr { unsigned long s_addr; // 4 bytes long }

Before we dive deeper…… Byte Ordering Byte ordering or Endianess is the attribute of a system which indicates whether integers are stored / represented left to right or right to left. Example 1: short int x = 0xAABB (hex) This can be stored in memory as 2 adjacent bytes as either (0xaa , 0xbb) or as (0xbb, 0xaa). Big Endian: Byte Value : [0xAA] [0xBB] Memory : [ 0 ] [ 1 ] Little Endian: Byte Value : [0xBB] [0xAA] Memory : [ 0 ] [ 1 ]

Before we dive deeper …. Byte Ordering Example 2: int x = 0xAABBCCDD This 4 byte long integer can be represented in the same 2 orderings: Big Endian: Byte Value: [0xAA] [0xBB] [0xCC] [0xDD] Memory: [ 0 ] [ 1 ] [ 2 ] [ 3 ] Little Endian: Byte Value: [0xDD] [0xCC] [0xBB] [0xAA] Memory: [ 0 ] [ 1 ] [ 2 ] [ 3 ] All Network data is sent in Big Endian format. In the networking world we call this representation as Network Byte Order and native representation on the host as Host Byte Order. We convert all data into Network Byte Order before transmission.

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

Diving Deeper into the syscalls() We will now describe the following calls in detail : Socket() Bind() Listen() Accept() Connect() Read() / Send() / Sendto() Write() / Recv() / Recvfrom() Close()

Socket() – A Connection Endpoint This creates an endpoint for a network connection. Int Socket(int doman, 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.

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 Example: Listen(sock, 10); This will allow a maximum of 10 connections to be in pending state.

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 when accept returns client_len = pointer to size of the client structure Example : struct sockaddr_in client; int len = sizeof(client); Accept(sock, (struct sockaddr *)&client, &len);

Connect() – connect to a service Connect is called by a client to connect to a server port. Int Connect(int sock, (struct sockaddr *)&server_addr, socklen_t len) sock: a socket returned by socket() server_addr: a sockaddr_in struct pointer filled with all the remote server details and cast as a sockaddr struct pointer len: size of the server_addr struct Example: connect(sock, (struct sockaddr *)server_addr, len);

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);

Close() – Bye ..Bye ! Close signals the end of communication between a server-client pair. This effectively closes the socket. Int close(int sock) sock = the socket to close Example : close(sock);

Done ! ! Phew …. This is all the theory you need to know about socket programming !!! Finer points will be more clear when we being coding . So let the games begin ……… :D