CS321: Computer Networks Socket Programming

Similar documents
CS321: Computer Networks Introduction to Application Layer

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

CS 3516: Computer Networks

UNIX Network Programming. Overview of Socket API Network Programming Basics

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

ECE 435 Network Engineering Lecture 2

ECE 435 Network Engineering Lecture 2

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Oral. Total. Dated Sign (2) (5) (3) (2)

Unix Network Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Elementary TCP Sockets

Socket Programming TCP UDP

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

ICT 6544 Distributed Systems Lecture 5

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Lab 0. Yvan Petillot. Networks - Lab 0 1

CS 640: Computer Networking

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Introduction to Client-Server Model

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSE 124 Discussion Section Sockets Programming 10/10/17

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CLIENT-SIDE PROGRAMMING

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Tutorial on Socket Programming

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Network Programming Week #1. K.C. Kim

Network programming(i) Lenuta Alboaie

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

Ports under 1024 are often considered special, and usually require special OS privileges to use.

CSE 333 SECTION 6. Networking and sockets

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Introduction to Socket Programming

Socket Programming(2/2)

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

A Client-Server Exchange

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Ken French HELP Session 1 CS4514

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Socket Programming for TCP and UDP

Introduction to Socket Programming

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

TCP: Three-way handshake

CSE 333 SECTION 8. Sockets, Network Programming

System Programming. Sockets

TCP Network Programming in C

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 333 SECTION 7. Client-Side Network Programming

Network Communication

The User Datagram Protocol

Piotr Mielecki Ph. D.

Programming with TCP/IP. Ram Dantu

Socket Programming 2007/03/28

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Chapter 6. The Transport Layer. Transport Layer 3-1

Client Server Computing

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

C Structures in Practice

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Network Programming Appendix Network Programming Labs

IPv6 Porting Applications

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Department of Computer Science

Internetworking II: Network programming. April 20, 2000

EECS122 Communications Networks Socket Programming. Jörn Altmann

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

UDP CONNECT TO A SERVER

Network Programming and Protocl Design. Nixu Ltd.

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

Application Programming Interfaces

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

1.2 The first Internet (i.e., one of the first packet switched networks) was referred to as the ARPANET.

CSE 333 SECTION 7. Client-Side Network Programming

Communication Networks ( ) / Fall 2013 The Blavatnik School of Computer Science, Tel-Aviv University

Internet applications

// socket for establishing connections

VMCI Sockets Programming Guide VMware ESX/ESXi 4.x VMware Workstation 7.0 VMware Server 2.0

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

Transcription:

CS321: Computer Networks Socket Programming Dr. Manas Khatua Assistant Professor Dept. of CSE IIT Jodhpur E-mail: manaskhatua@iitj.ac.in

Socket Programming It shows how the network application programs are actually created typical network application consists of a client program and a server program Those programs resides in two different end systems. There are two types of network applications Open i.e. operation rules are known to all Proprietary i.e. operation rules has not been published In Server Site: The server needs a local (server) and a remote (client) socket address for communication. In Client Site: The client also needs a local (client) and a remote (server) socket address for communication. 17-01-2018 Dr. Manas Khatua 2

Iterative Communication Using UDP several client programs can access the same server program at the same time the server program can be designed to respond Iteratively (i.e. one by one) Concurrently In UDP communication, the client and server use only one socket each The socket created at the server site lasts forever; the socket created at the client site is closed (destroyed) when the client process terminates. 17-01-2018 Dr. Manas Khatua 3

Cont Each client is served in each iteration of the loop in the server. Each client sends a single datagram and receives a single datagram. If a client wants to send two datagrams, it is considered as two clients for the server. 17-01-2018 Dr. Manas Khatua 4

Iterative Communication Using TCP The TCP server uses two different sockets one for connection establishment (listen socket) the other for data transfer (socket) 17-01-2018 Dr. Manas Khatua 5

Flow Diagram in UDP 17-01-2018 Dr. Manas Khatua 6

Socket Programming (in C) the role of a socket in communication has no buffer to store data to be sent or received is capable of neither sending nor receiving data acts as a reference or a label buffers and necessary variables are created inside OS The C language defines a socket as a structure. 17-01-2018 Dr. Manas Khatua 7

Cont Family: defines the protocol family (PF). The common values are PF_INET (for current Internet), Type: defines four types of sockets SOCK_STREAM (for TCP), SOCK_DGRAM (for UDP), SOCK_SEQPACKET (for SCTP), SOCK_RAW (for directly use the IP) Protocol: defines the specific protocol suite in the family said above (e.g., 0 => TCP/IP) 17-01-2018 Dr. Manas Khatua 8

Cont Local socket address: defines the local socket address Length: size of the socket address Family: AF_INET for TCP/IP protocol suite; (AF: address family) Port Number: it defines the process IP Address: it defines the host on which the process is running Unused: for future use Remote socket address: defines the remote socket address 17-01-2018 Dr. Manas Khatua 9

Echo application using UDP Echo Application: The client program sends a short string of characters to the server; the server echoes back the same string to the client. headerfiles.h : need to use the definition of the socket and all procedures (functions) defined in the interface, 17-01-2018 Dr. Manas Khatua 10

Socket struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below. It is IP address. char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() }; struct sockaddr_in myaddr; int s; myaddr.sin_family = AF_INET; myaddr.sin_port = htons(3490); // host to network short inet_aton("63.161.169.137", &myaddr.sin_addr.s_addr); //inet_aton:: convert from a struct in_addr to a string in dots-and-numbers s = socket(pf_inet, SOCK_STREAM, 0); bind(s, (struct sockaddr*)myaddr, sizeof(myaddr)); 17-01-2018 Dr. Manas Khatua 11

Echo Server Program II UDP echo server program #include "headerfiles.h // All header files required for socket programming int main (void) { II Declare and define variables int s; II Socket descriptor (reference) int len; II Length of string to be echoed char buffer [256]; II Data buffer struct sockaddr_in servaddr; II Server (local) socket address struct sockaddr_in cintaddr; II Client (remote) socket address int cintaddrlen; II Length of client socket address II Build local (server) socket address memset (&servaddr, 0, sizeof (servaddr)); II Allocate memory servaddr.sin_family = AF_INET; II Default Family field servaddr.sin_port = htons (SERVER_PORT) II Default port number servaddr.sin_addr.s_addr = htonl (INADDR_ANY); II Default IP address 17-01-2018 Dr. Manas Khatua 12

Cont II Create socket if ((s = socket (PF_INET, SOCK_DGRAM, 0) < 0); { perror ("Error: socket failed! "); exit (1); } II Bind socket to local address and port If (bind (s, (struct sockaddr*) &servaddr, sizeof (servaddr)) < 0); { perror ("Error: bind failed!"); exit (1); } for ( ; ; ) II Run forever { II Receive String len = recvfrom (s, buffer, sizeof (buffer), 0, (struct sockaddr*)&cintaddr, &cintaddrlen); II Send String sendto (s, buffer, len, 0, (struct sockaddr*)&cintaddr, sizeof(clntaddr)); } II End of for loop } II End of echo server program 17-01-2018 Dr. Manas Khatua 13

Echo Client Program //UDP echo client program #inciude "headerfiles.h int main (int argc, char* argv[ ]) // Three arguments to be checked later { //Declare and define variables int s; // Socket descriptor int len; // Length of string to be echoed char* servname; // Server name int servport; // Server port char* string; // String to be echoed char buffer [256+ 1]; // Data buffer struct sockaddr_in servaddr; // Server socket address // Check and set program arguments if(argc!=3) { printf ("Error: three arguments are needed!"); exit(1); } servname = argv[1]; servport = atoi (argv[2]); string = argv[3]; 17-01-2018 Dr. Manas Khatua 14

Cont // Build server socket address memset (&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; //call DNS to find the server IP corresponding to server name inet_pton (AF_INET, servname, &servaddr.sin_addr); servaddr.sin_port = htons (servport); // Create socket If ((s = socket (PF_INET, SOCK_DGRAM, 0) < 0); { perror ("Error: Socket failed!"); exit (1); } //Send echo string len = sendto (s, string, strlen (string), 0, (struct sockaddr)&servaddr, sizeof(servaddr)); //Receive echo string recvfrom (s, buffer, len, 0, NULL, NULL); // NULL: as we don t need client socket address and length 17-01-2018 Dr. Manas Khatua 15

Cont //Print and verify echoed string buffer [len] = '\0'; printf ("Echo string received: ); fputs (buffer, stdout); //Close the socket close (s); //Stop the program exit (0); } // End of echo client program 17-01-2018 Dr. Manas Khatua 16

Flow Diagram in TCP 17-01-2018 Dr. Manas Khatua 17

Iterative Programming Using TCP Before sending or receiving data, a connection needs to be established between the client and the server. Flow diagram for the client and server data-transfer boxes 17-01-2018 Dr. Manas Khatua 18

Cont We need to control how many bytes of data we have received and where the next chunk of data is stored. 17-01-2018 Dr. Manas Khatua 19

Echo server program using TCP II Echo server program #include "headerfiles.h int main (void) { II Declare and define int Is; II Listen socket descriptor int s; II Socket descriptor (reference) char buffer [256]; II Data buffer char* ptr = buffer; II Data buffer int len = 0; II Number of bytes to send or receive int maxlen = sizeof (buffer); II Maximum number of bytes int n = 0; II Number of bytes for each recv call int waitsize = 16; II Size of waiting clients struct sockaddr _in serveraddr; II Server address struct sockaddr_in clientaddr; II Client address int clntaddrlen; II Length of client address 17-01-2018 Dr. Manas Khatua 20

Cont // Create local (server) socket address memset (&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(inaddr_any); servaddr.sin_port = htons (SERV_PORT); II Default IP address II Default port II Create listen socket if (Is = socket (PF_INET, SOCK_STREAM, 0) < 0) { perror ("Error: Listen socket failed!"); exit (1); } II Bind listen socket to the local socket address if (bind (Is, &servaddr, sizeof (servaddr)) < 0) { perror ("Error: binding failed!"); exit (1); } // Listen to connection requests if (listen (Is, waitsize) < 0) { perror ("Error: listening failed!"); exit (1); } 17-01-2018 Dr. Manas Khatua 21

Cont // Handle the connection for ( ; ; ) // Run forever { II Accept connections from client if (s = accept (Is, &cintaddr, &clntaddrlen) < 0) { perror ("Error: accepting failed!); exit (1); } // Data transfer section while ((n = recv (s, ptr, maxlen, 0)) > 0) { ptr += n ; II Move pointer along the buffer maxlen -= n; II Adjust maximum number of bytes to receive len += n; II Update number of bytes received } send (s, buffer, len, 0); // Send back (echo) all bytes received close (s); II Close the socket } // End of for loop } II End of echo server program main 17-01-2018 Dr. Manas Khatua 22

Echo client program using TCP II TCP echo client program #include "headerfiles.h int main (int argc, char* argv[ ]) { II Declare and define int s; II Socket descriptor int n; II Number of bytes in each recv call char* servname; II Server name int servport; II Server port number char* string; II String to be echoed int len; II Length of string to be echoed char buffer [256 + 1]; II Buffer char* ptr = buffer; II Pointer to move along the buffer struct sockaddr_in serveraddr; II Server socket address II Check and set arguments if (argc!= 3) { printf ("Error: three arguments are needed!"); exit (1); } 17-01-2018 Dr. Manas Khatua 23

Cont servname = arg [1]; servport = atoi (arg [2]); string = arg [3]; // Create remote (server) socket address memset (&servaddr, 0, sizeof(servaddr); serveaddr.sin_family = AF _INET; inet_pton (AF_INET, servname,&serveaddr.sin_addr); serveaddr.sin_port = htons (servport); // Server IP // Server port //Create socket if ((s = socket (PF_INET, SOCK_STREAM, 0) < 0); { perror ("Error: socket creation failed!"); exit (1); } II Connect to the server if (connect (sd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { perror ("Error: connection failed!"); exit (1); } 17-01-2018 Dr. Manas Khatua 24

Cont II Data transfer section send (s, string, strlen(string), 0); while ((n = recv (s, ptr, maxlen, 0)) > 0) { ptr + = n; II Move pointer along the buffer maxlen - = n; II Adjust the maximum number of len += n; II Update the length of string received } II Print and verify the echoed string buffer [Ien] = '\0'; printf ("Echoed string received: "); fputs (buffer, stdout); II Close socket close (s); // Stop program exit (0); } // End of echo client program main 17-01-2018 Dr. Manas Khatua 25

17-01-2018 Dr. Manas Khatua 26