Tutorial on Socket Programming



Similar documents
Socket Programming. Srinidhi Varadarajan

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

UNIX Sockets. COS 461 Precept 1

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

Unix Network Programming

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

The POSIX Socket API

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Implementing Network Software

Lab 4: Socket Programming: netcat part

Introduction to Socket programming using C

TCP/IP - Socket Programming

NS3 Lab 1 TCP/IP Network Programming in C

Overview. Socket Programming. Using Ports to Identify Services. UNIX Socket API. Knowing What Port Number To Use. Socket: End Point of Communication

Computer Networks Network architecture

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

Porting applications & DNS issues. socket interface extensions for IPv6. Eva M. Castro. ecastro@dit.upm.es. dit. Porting applications & DNS issues UPM

Socket Programming in C/C++

Programmation Systèmes Cours 9 UNIX Domain Sockets

Communication Networks. Introduction & Socket Programming Yuval Rochman

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

Application Architecture

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

Networks. Inter-process Communication. Pipes. Inter-process Communication

DESIGN AND IMPLEMENT AND ONLINE EXERCISE FOR TEACHING AND DEVELOPMENT OF A SERVER USING SOCKET PROGRAMMING IN C

CSE 333 SECTION 6. Networking and sockets

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Operating Systems Design 16. Networking: Sockets

Generalised Socket Addresses for Unix Squeak

Network Programming with Sockets. Process Management in UNIX

Writing a C-based Client/Server

Networks class CS144 Introduction to Computer Networking Goal: Teach the concepts underlying networks Prerequisites:

UNIX. Sockets. mgr inż. Marcin Borkowski

BSD Sockets Interface Programmer s Guide

Packet Sniffing and Spoofing Lab

Writing Client/Server Programs in C Using Sockets (A Tutorial) Part I. Session Greg Granger grgran@sas. sas.com. SAS/C & C++ Support

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

Client / Server Programming with TCP/IP Sockets

Network Programming with Sockets. Anatomy of an Internet Connection

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Goal: learn how to build client/server application that communicate using sockets. An interface between application and network

IPv6 Enabling CIFS/SMB Applications

TCP/IP Sockets in C. Practical Guide for Programmers

Chapter 3. Internet Applications and Network Programming

What is CSG150 about? Fundamentals of Computer Networking. Course Outline. Lecture 1 Outline. Guevara Noubir noubir@ccs.neu.

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Network Programming TDC 561

Chapter 11. User Datagram Protocol (UDP)

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

System Calls and Standard I/O

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

SSC - Communication and Networking Java Socket Programming (II)

Socket = an interface connection between two (dissimilar) pipes. OS provides this API to connect applications to networks. home.comcast.

IBM i Version 7.2. Programming Socket programming IBM

Concurrent Server Design Alternatives

15-441: Computer Networks Homework 1

KATRAGADDA INNOVATIVE TRUST FOR EDUCATION NETWORK PROGRAMMING. Notes prepared by D. Teja Santosh, Assistant Professor, KPES, Shabad, R.R. District.

Implementing and testing tftp

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

Basic Networking Concepts. 1. Introduction 2. Protocols 3. Protocol Layers 4. Network Interconnection/Internet

A Client Server Transaction. Introduction to Computer Systems /18 243, fall th Lecture, Nov. 3 rd

Linux Kernel Architecture

Network Programming using sockets

Computer Networks - Xarxes de Computadors

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Hostnames. HOSTS.TXT was a bottleneck. Once there was HOSTS.TXT. CSCE515 Computer Network Programming. Hierarchical Organization of DNS

Windows Socket Programming & IPv6 Translation Middleware

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

Application Technique. EtherNet/IP Socket Interface

Computer Networks/DV2 Lab

Dissertation Title: SOCKS5-based Firewall Support For UDP-based Application. Author: Fung, King Pong

Elementary Name and Address. Conversions

Objectives of Lecture. Network Architecture. Protocols. Contents

Transport Layer. Chapter 3.4. Think about

CS 213, Fall 2000 Lab Assignment L5: Logging Web Proxy Assigned: Nov. 28, Due: Mon. Dec. 11, 11:59PM

Unix System Administration

Protocols. Packets. What's in an IP packet

CPS221 Lecture: Layered Network Architecture

How do I get to

Ethernet. Ethernet. Network Devices

µtasker Document FTP Client

Lecture 7: Introduction to Sockets

System calls. Problem: How to access resources other than CPU

IP - The Internet Protocol

Names & Addresses. Names & Addresses. Hop-by-Hop Packet Forwarding. Longest-Prefix-Match Forwarding. Longest-Prefix-Match Forwarding

transmission media and network topologies client/server architecture layers, protocols, and sockets

Linux MDS Firewall Supplement

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Transcription:

Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Seyed Hossein Mortazavi (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian, ) 1

Outline Client- server paradigm Sockets Socket programming in UNIX 2

End System: Computer on the Net Internet Also known as a host 3

Clients and Servers Client program Running on end host Requests service E.g., Web browser Server program Running on end host Provides service E.g., Web server GET /index.html Site under construction 4

Client- Server Communication Client Sometimes on Initiates a request to the server when interested E.g., web browser Needs to know the server s address Server Always on Serve services to many clients E.g.,www.cnn.com Not initiate contact with the clients Needs a fixed address 5

Socket: End Point of Communication Processes send messages to one another Message traverse the underlying network A Process sends and receives through a socket Analogy: the doorway of the house. Socket, as an API, supports the creation of network applications User process socket Operating System User process socket Operating System 6

UNIX Socket API Socket interface A collection of system calls to write a networking program at user- level. Originally provided in Berkeley UNIX Later adopted by all popular operating systems In UNIX, everything is like a file All input is like reading a file All output is like writing a file File is represented by an integer file descriptor Data written into socket on one host can be read out of socket on other host System calls for sockets Client: create, connect, write, read, close Server: create, bind, listen, accept, read, write, close 7

Typical Client Program Prepare to communicate Create a socket Determine server address and port number Why do we need to have port number? 8

Using Ports to Identify Services Client host Client Service request for 128.100.3.40 :80 (i.e., the Web server) Server host 128.100.3.40 OS Web server (port 80) Echo server (port 7) Client Service request for 128.100.3.40 :7 (i.e., the echo server) OS Web server (port 80) Echo server (port 7) 9

Socket Parameters A socket connection has 5 general parameters: The protocol Example: TCP and UDP. The local and remote address Example: 128.100.3.40 The local and remote port number Some ports are reserved (e.g., 80 for HTTP) Root access require to listen on port numbers below 1024 10

Typical Client Program Prepare to communicate Create a socket Determine server address and port number Initiate the connection to the server Exchange data with the server Write data to the socket Read data from the socket Do stuff with the data (e.g., render a Web page) Close the socket 11

Important Functions for Client Program socket() create the socket descriptor connect() connect to the remote server read(),write() communicate with the server close() end communication by closing socket descriptor 12

Creating a Socket int socket(int domain, int type, int protocol) Returns a descriptor (or handle) for the socket Domain: protocol family PF_INET for the Internet Type: semantics of the communication SOCK_STREAM: Connection oriented SOCK_DGRAM: Connectionless Protocol: specific protocol UNSPEC: unspecified (PF_INET and SOCK_STREAM already implies TCP) E.g., TCP: sd = socket(pf_inet, SOCK_STREAM, 0); E.g., UDP: sd = socket(pf_inet, SOCK_DGRAM, 0); 13

Connecting to the Server int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen) Arguments: socket descriptor, server address, and address size Remote address and port are in struct sockaddr Returns 0 on success, and - 1 if an error occurs 14

Sending and Receiving Data Sending data write(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer of data, and length of the buffer Returns the number of characters written, and - 1 on error Receiving data read(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer Returns the number of characters read (where 0 implies end of file ), and - 1 on error Closing the socket int close(int sockfd) 15

Byte Ordering: Little and Big Endian Hosts differ in how they store data E.g., four- byte number (byte3, byte2, byte1, byte0) Little endian ( little end comes first ) ß Intel PCs!!! Low- order byte stored at the lowest memory location byte0, byte1, byte2, byte3 Big endian ( big end comes first ) High- order byte stored at lowest memory location byte3, byte2, byte1, byte 0 IP is big endian (aka network byte order ) Use htons() and htonl() to convert to network byte order Use ntohs() and ntohl() to convert to host order 16

Servers Differ From Clients Passive open Prepare to accept connections but don t actually establish one until hearing from a client Hearing from multiple clients Allow a backlog of waiting clients... in case several try to start a connection at once Create a socket for each client Upon accepting a new client create a new socket for the communication 17

Typical Server Program Prepare to communicate Create a socket Associate local address and port with the socket Wait to hear from a client (passive open) Indicate how many clients- in- waiting to permit Accept an incoming connection from a client Exchange data with the client over new socket Receive data from the socket Send data to the socket Close the socket Repeat with the next connection request 18

Important Functions for Server Program socket() create the socket descriptor bind() associate the local address listen() wait for incoming connections from clients accept() accept incoming connection read(),write() communicate with client close() close the socket descriptor 19

Socket Preparation for Server Program Bind socket to the local address and port int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen) Arguments: socket descriptor, server address, address length Returns 0 on success, and - 1 if an error occurs Define the number of pending connections int listen(int sockfd, int backlog) Arguments: socket descriptor and acceptable backlog Returns 0 on success, and - 1 on error 20

Accepting a New Connection int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen) Arguments: socket descriptor, structure that will provide client address and port, and length of the structure Returns descriptor for a new socket for this connection What happens if no clients are around? The accept() call blocks waiting for a client What happens if too many clients are around? Some connection requests don t get through But, that s okay, because the Internet makes no promises 21

Server Operation accept() returns a new socket descriptor as output New socket should be closed when done with communication Initial socket remains open, can still accept more connections 22

Putting it All Together Server socket() bind() block listen() accept() read() process request write() Client socket() connect() write() read() 23

Supporting Function Calls gethostbyname() get address for given host name (e.g. 128.100.3.40 for name cs.toronto.edu ); getservbyname() get port and protocol for a given service e.g. ftp, http (e.g. http is port 80, TCP) getsockname() get local address and local port of a socket getpeername() get remote address and remote port of a socket 24

Useful Structures struct sockaddr { u_short sa_family; char sa_data[14]; }; struct sockaddr_in { u_short sa_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { u_long s_addr; }; Generic address, connect(), bind(), accept() <sys/socket.h> Client and server addresses TCP/UDP address (includes port #) <netinet/in.h> IP address <netinet/in.h> 25

Other useful stuff Address conversion routines Convert between system s representation of IP addresses and readable strings (e.g. 128.100.3.40 ) unsigned long inet_addr(char* str); char * inet_ntoa(struct in_addr inaddr); Important header files: <sys/types.h>, <sys/socket.h>, <netinet/in.h>, <arpa/inet.h> man pages socket, accept, bind, listen 26

Next tutorial session: Assignment 1 overview Please post questions to the bulletin board Office hours posted on website 27

Socket types Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A, B, C", they will arrive in the same order - "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries. Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol). Raw Sockets: These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol. Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced- packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming 28 packets.