TCP/IP - Socket Programming



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

Socket Programming. Srinidhi Varadarajan

Tutorial on Socket Programming

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

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

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

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Unix Network Programming

Introduction to Socket programming using C

BSD Sockets Interface Programmer s Guide

Socket Programming in C/C++

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

Application Architecture

Operating Systems Design 16. Networking: Sockets

NS3 Lab 1 TCP/IP Network Programming in C

Network Programming with Sockets. Process Management in UNIX

Implementing Network Software

Lab 4: Socket Programming: netcat part

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

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

UNIX. Sockets. mgr inż. Marcin Borkowski

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

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

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

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

Writing a C-based Client/Server

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

IBM i Version 7.2. Programming Socket programming IBM

Packet Sniffing and Spoofing Lab

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

The POSIX Socket API

Client / Server Programming with TCP/IP Sockets

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

Network Programming with Sockets. Anatomy of an Internet Connection

Programmation Systèmes Cours 9 UNIX Domain Sockets

UNIX Sockets. COS 461 Precept 1

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

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

Elementary Name and Address Conversions

Communication Networks. Introduction & Socket Programming Yuval Rochman

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

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

Windows Socket Programming & IPv6 Translation Middleware

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

An Introductory 4.4BSD Interprocess Communication Tutorial

CSE 333 SECTION 6. Networking and sockets

Network Programming using sockets

Elementary Name and Address. Conversions

Network Programming TDC 561

Concurrent Server Design Alternatives

Programming With Sockets 2

Chapter 11. User Datagram Protocol (UDP)

Generalised Socket Addresses for Unix Squeak

Implementing and testing tftp

Linux Kernel Architecture

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

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

Objectives of Lecture. Network Architecture. Protocols. Contents

An Advanced 4.4BSD Interprocess Communication Tutorial

IPv6 Enabling CIFS/SMB Applications

Software changes for Website and Application IPv6 Readiness

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

Network Programming. Chapter The Client-Server Programming Model

M06 (31-May-2001) Requirements and guidelines for distributed laboratories application migration WP 3

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

Computer Networks Network architecture

Porting and Deploying VoIP to IPv6: Lessons Learned

Computer Networks/DV2 Lab

q Connection establishment (if connection-oriented) q Data transfer q Connection release (if conn-oriented) q Addressing the transport user

Programming OpenSSL. The Server Perspective. by Sean Walton. Copyright 2001 Sean Walton

Lecture 9: Network Security Introduction

15-441: Computer Networks Homework 1

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

TCP/IP Sockets in C. Practical Guide for Programmers

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

A Client-Server Transaction. Systemprogrammering 2006 Föreläsning 8 Network Programming. Hardware Org of a Network Host.

Domain Name System (1)! gethostbyname (2)! gethostbyaddr (2)!

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

University of Amsterdam

TCP/IP Support Enhancements

Name and Address Conversions

Load balancing MySQL with HaProxy. Peter Boros Percona 4/23/13 Santa Clara, CA

sys socketcall: Network systems calls on Linux

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

IPv4/IPv6 Transition for SIP VoIP Applications

Java Network. Slides prepared by : Farzana Rahman

APPLICATION CODE APPLICATION CODE S ERVER PROCESS STUB STUB RPC RUNTIME LIBRARY RPC RUNTIME LIBRARY ENDPOINT MAP ENDPOINT MAP

Computer Networks - Xarxes de Computadors

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

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

Transcription:

TCP/IP - Socket Programming jrb@socket.to.me Jim Binkley 1

sockets - overview sockets simple client - server model look at tcpclient/tcpserver.c look at udpclient/udpserver.c tcp/udp contrasts normal master/slave setup for TCP inetd on UNIX - mother server some details - there are more... Jim Binkley 2

sockets in BSD world since early 80 s, 4.2 BSD client/server model like unix file i/o up to a point, can be redirected to stdin/stdout/stderr (on unix) sockets are dominant tcp/ip application API other API is System V TLI (OSI-based) winsock - windows variations on sockets» sockets in windows event-driven framework Jim Binkley 3

sockets basic definition - endpoint of communication allows connected streams (TCP) or discrete messages (UDP) between processes on same machine, cross network in o.s., really read/write data queues + TCP has connection Queue (server side) talk to socket with handle/sock descriptor Jim Binkley 4

kinds of sockets acc. to address family; i.e. how does addressing work IP address family -> IP addr, tcp/udp port traditional BSD families TCP/IP (AF_INET; i.e., Internet)» TCP/UDP/ raw (talk to IP) UNIX (intra-machine, pipes) XNS, and even APPLETALK, DECNET, IPX... Jim Binkley 5

sockets client read write handle socket layer r/w queues server read write tcp stack Jim Binkley 6

syscalls - TCP client/simple test server int s = socket(2) gethostbyname(3) connect(2) read/write(2) init i/o int s = socket(2) bind(2) listen(2) accept(2) read/write(2) close(2) close(2) client server Jim Binkley 7

socket(2) syscall int s = socket(family, socktype, protocol); family = AF_INET, AF_APPLETALK, etc. socktype = SOCK_STREAM, SOCK_DGRAM, SOCK_RAW protocol = 0, TCP_PROTO, IP_PROTO example - TCP socket: s = socket(af_inet, SOCK_STREAM, 0); used by both client/server Jim Binkley 8

gethostbyname(3) - client struct hostent h* = gethostbyname( sirius.cs.pdx.edu ); kernel(2) calls take ip addresses via struct sockaddr_in pointers, not DNS names maps DNS name to ip address SOMEHOW /etc/hosts DNS servers yellow pages (NIS) SOMEHOW - OS specific Jim Binkley 9

connect(2) - client rc = connect(sd, struct sockaddr *sa, len); client connects to server that can accept normally TCP, but UDP rarely might use client must fill in server port, ip address TCP will attempt to connect to remote machine client side TCP has client TCP port - implicit bind Jim Binkley 10

TCP/UDP ports distinct 64k port spaces client has port, server has port o.s. may typically allocate client port dynamically server SETS port, as well-known number; i.e., client sends packets to that port server port == which service (telnet/ftp/web) Jim Binkley 11

bind(2) - set tcp/udp port (server) int rc = bind(sock, struct sockaddr *sa, len); sock - valid sd sa - struct sockaddr_in (next slide) port value goes in here len - sizeof struct sockaddr_in data storage server sets well-known TCP/UDP port client rarely sets client port with bind if port == 0, kernel chooses for you Jim Binkley 12

sockaddr structure sockaddr is generic structure, struct sockaddr_in is instance of it for INET struct sockaddr_un for UNIX sockets used in bind, connect, accept, sendto, recvfrom calls when ip/port # needs to be passed to/from kernel ip addr/port # are in NETWORK byte order Jim Binkley 13

sockaddr_in - address structure struct sockaddr_in { short sin_family; /* AF_INET *; u_short sin_port; struct in_addr sin_addr; /* ip addr */ char sin_zero[8]; /* pad */ } struct in_addr { u_long s_addr; } Jim Binkley 14

listen(2) - server int rc = listen(sd, 5); TCP server only, NOT UDP has two functions: 1. enables TCP state machine, can now get connection 2. sets TCP socket connection queue to 5 at a time - enables concurrent connections accept(2) takes connection from conn. Q Jim Binkley 15

accept(2) int csd = accept(lsd, struct sockaddr *sa, *len); accepts connection - paired with connect(2) blocks without select(2) call until connection arrives, returns connected sd now in connected state, can make read/write calls, use connected sd (not listen sd) returns client ip/port in sockaddr_in NOTE: len is call by value-result Jim Binkley 16

accept(2) no substitute what is the problem here? int sock; struct sockaddr_in recvsock; int len = sizeof(struct sockaddr_in); int rc = accept(sock, &recvsock, len); Can you say BOOM!!!!!!!... Jim Binkley 17

computer gets programmer s attention Jim Binkley 18

read(2) / write(2) with normal TCP, read may return less data than you expect. Call it in a loop. example: you ask for 1024, you get two 512 byte packets. write will block until your data is written - don t need loop (unless you use non-blocking i/o) note: read(fd, char *buf, int cc) TCP addresses setup at connect time! Jim Binkley 19

syscalls - UDP client/simple test server int s = socket(2) gethostbyname(3) init int s = socket(2) bind(2) for(;;) sendto(2) recvfrom(2) i/o for(;;) recvfrom(2) do something... sendto(2) close(2) close(2) client server Jim Binkley 20

udp - send/recv packets int sd = socket(af_inet, SOCK_DGRAM, 0); bind used to set server port sendto/recvfrom have sockaddr_in parameters that must specify peer ip address/port #. recvfrom will tell you from whom you got the packet (ip/port), you use sendto to send it back one server may get packets from N clients no idea of connection Jim Binkley 21

UDP server socket/bind call loop recvfrom(sd,...&fromaddr...); sendto(sd,...&fromaddr...); one server can serve packets from many clients TCP needs to have one server per client and must use threads/fork a process/task per connection Jim Binkley 22

tcp/udp contrasts tcp is stream tcp is reliable tcp is point to point and connected connect/accept specify addresses at setup time, read/write don t need addresses data is checksummed udp discrete packets udp is unreliable udp can broadcast, 1 to N or server can receive from many clients each read/write specifies address data MAY be csum ed Jim Binkley 23

master/slave tcp server init fork slave on accept cleanup Zombies socket/bind/listen signal(sigchld, reapem); for (;;) nsd = accept(lsd,...) if (fork() == 0) { read/write(nsd,...); close(nsd); } close(nsd); reapem() - signal handler slave does i/o Jim Binkley 24

master/slave - master signal handler init: int reapem(); signal(sigchld, reapem) signal handler: reapem() { for(;;) { rc = waitpid(,wnohang,); if ( rc <= 0) return; } } Jim Binkley 25

inetd - unix mother daemon per well-known port protocol servers ate up too many o.s. resources combined into one TITANIC mother daemon - only one thread at rest listens at tcp/udp ports - spawns stub server to do work see /etc/inetd.conf for setup uses select(2) mechanism Jim Binkley 26

BSD/UNIX select(2) call nohits = select(nofds, readmask, writemask, exceptionmask, timeout); select functions: allows callers to detect i/o to be read on > 1 socket or char device descriptor at a time allows callers to detect TCP connection (so you can call accept) - inetd does this handles TCP out of band data can do timed poll or block if time == 0 Jim Binkley 27

some socket details: inet_addr(3) routines - manipulate ip addrs example: convert string to ip addr struct in_addr * = inet_addr( 1.2.3.4 ); char *inet_ntoa(struct in_addr inaddr); BSD database routines: /etc/hosts - gethostbyname(3), gethostbyaddr(3) /etc/services - getservbyname(3) /etc/protocols - getprotobyname(3) Jim Binkley 28

BSD oft-used TCP/IP files /etc/hosts - host/ip pairs, they don t all fit /etc/services - TCP/UDP well known ports 9 - discard port /etc/resolv.conf - DNS servers /etc/protocols - proto name to number mapping (protocols above IP) /etc/inetd.conf - servers inetd can run Jim Binkley 29

details, the end: byte-order routines: BIG-ENDIAN rules sparc/68k not Intel Architecture long word: htonl(3), ntohl(3) short: htons(3), ntohs(3) bytes - no problem misc. socket ops setsockopt(2), getsockopt(2)» turn on UDP broadcast, multicast» see Stevens for details Jim Binkley 30