Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()

Slides:



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

Socket Programming Computer Networks, Spring 2008 Your TAs.
Socket Programming 101 Vivek Ramachandran.
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.
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.
Chapter 10 IPv4 and IPv6 Interoperability. contents Introduction IPv4 Client, IPv6 Server IPv6 Client, IPv4 Server IPv6 Address Testing Macros IPV6_ADDRFORM.
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.
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.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
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.
#include DatatypeDescription int8_t uint8_t int16_t uint16_t int32_t uint32_t Signed 8-bit integer Unsigned 8-bit integer Signed 16-bit integer Unsigned.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
Introduction to Project 1 Web Client and Server Jan 2006.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
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.
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.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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.
1 CS716 Advanced Computer Networks By A. Wahid Shaikh.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
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.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Advanced Computer Networks
Network Programming CSC- 341
Chapter4 Elementary TCP Socket
Socket Programming in C
Network Programming with Sockets
Network Programming CSC- 341
Socket Programming in C
Advanced Network Programming spring 2007
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Presentation transcript:

Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket() 2.bind() 3.sendto() 4.recvfrom() 5.close()

Socket Address Structures Most socket functions require a pointer to a socket address structure as an argument. Each supported protocol defines its own socket address structure. The names of these structures begin with sockaddr_ and end with a unique suffix for each protocol suite.

IPv4 Socket Address Structure Internet socket address structure is named sockaddr_in and defined in IP address: struct in_addr { in_addr_t s_addr; /* 32-bit IP address */ }; /* network byte ordered */ TCP or UDP Address: struct sockaddr_in { uint8_tsin_len; /* length of structure*/ shortsin_family; /* e.g., AF_INET */ ushortsin_port; /* TCP/UDP port */ structin_addr; /* IP address */ charsin_zero[8]; /* unused */ }; Note: Having sin_len simplifies handling variable length socket address structures. All but sin_family in network byte order

Generic Socket Address Structures Problem: A socket address is always passed by reference when passed as an argument to socket functions. But any socket call that takes one of these pointers as an argument must deal with socket address structures from ANY of the supported protocol families. A problem arises in how to declare the type of pointer that is passed.

continued.. Solution: is to define a generic socket structure defined in Example: int bind(int, struct sockaddr *, socklen_t); struct sockaddr_in serv; /* IPv4 address */ bind(sockfd, (struct sockaddr *)&serv,sizeof(serv)); If we omit this warning comes! Q: Why not do (void *)? struct sockaddr{ uint8_t sa_len; sa_family_t sa_family; /* AF_INET, AF_INET6.. */ char sa_data[14] /* protocol specific addr */ };

Byte Ordering Big Endian vs. Little Endian ◦ Little Endian (Intel, DEC):  Least significant byte of word is stored in the lowest memory address ◦ Big Endian (Sun, SGI, HP):  Most significant byte of word is stored in the lowest memory address ◦ Network Byte Order = Big Endian  Allows both sides to communicate  Must be used for some data (i.e. IP Addresses)  Good form for all binary data

Byte Ordering Functions 16- and 32-bit conversion functions (for platform independence) Examples: int m, n; short int s,t; m = ntohl (n) net-to-host long (32-bit) translation s = ntohs (t) net-to-host short (16-bit) translation n = htonl (m) host-to-net long (32-bit) translation t = htons (s) host-to-net short (16-bit) translation

Address Access/Conversion Functions in_addr_t inet_addr(const char* strptr); ◦ Translate dotted-decimal notation to IP address; returns -1 on failure, thus cannot handle broadcast value “ ” int inet_aton(const char *strptr, struct in_addr *inaddr); ◦ Translate dotted-decimal notation to IP address; returns 1 on success, 0 on failure. char* inet_ntoa(struct in_addr inaddr); ◦ Translate IP address to ASCII dotted-decimal notation (e.g., “ ”)

UDP Client-Server Interaction Model

socket() Function Create a socket ◦ int socket(int family, int type, int protocol); ◦ Returns file descriptor or -1. Fields: ◦ int family  AF_INET  IPv4 protocols  AF_INET6  IPv6 protocols ◦ int type  SOCK_DGRAM  for UDP datagrams  SOCK_STREAM  for TCP streams ◦ int protocol (for AF_INET & AF_INET6 family sockets)  IPPROTO_UDP  UDP Transport  IPPROTO_TCP  TCP Transport

bind() Function Bind a socket to a local IP address and port number ◦ int bind (int sockfd, struct sockaddr* myaddr, int addrlen); ◦ Returns 0 if OK or -1 on error. ◦ sockfd: socket file descriptor (returned from socket) ◦ myaddr: includes IP address and port number  IP address: set by kernel if value passed is INADDR_ANY, else set by caller  port number: set by kernel if value passed is 0, else set by caller ◦ addrlen: length of address structure  = sizeof (struct sockaddr_in) Process SpecifiesResult IP AddressPort # INADDR_ANY 0Kernel chooses IP addr& port # INADDR_ANY nonzeroKernel chooses IP addr, process specifies port # Local IP Addr0Process specifies IP addr, kernel assigns port # Local IP AddrnonzeroProcess specify both

sendto() & recvfrom() Functions int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen); ◦ Send a datagram to another UDP socket.  Returns number of bytes written or -1. int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen); ◦ Read a datagram from a UDP socket.  Returns number of bytes read or -1.

close() Function int close (int sockfd); Close a socket. ◦ Returns 0 on success, -1 on failure. ◦ sockfd: socket file descriptor (returned from socket) Closes communication on socket in both directions. ◦ All data sent before close are delivered to other side. After close, sockfd is not valid for reading or writing.