Socket Programming (Cont.)

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

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;
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.
#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.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Unix Network Programming Part 2: Elementary Sockets Jani Peusaari.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
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.
Client-Side Network Programming
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
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.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
Socket address structures Byte ordering IP address conversion
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
Socket Programming(2/2) 1. Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Network Programming. 2 Background Important guidelines –Use conductor.tamucc.edu or any LINUX machine to develop your network applications –Do not use.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 17 Acknowledgements: The syllabus and power point presentations are modified versions.
Lecture 15 Socket Programming 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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CSE 333 – SECTION 8 Client-Side Network Programming.
Onward with Chat! Networking CS 3470, Section 1.
Network Programming CSC- 341
Network Programming CSC- 341
Week 13 - Friday CS222.
Network Programming with Sockets
Tutorial on Socket Programming
Sockets.
Transport layer API: Socket Programming
Chapter 3 Sockets Introduction
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Introduction to Socket Programming
Network Programming Chapter 12
Lecture 2 Socket Programming
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
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Socket Programming (Cont.) Networking CS 3470, Section 1 Sarah Diesburg

Control Flow TCP Server socket() bind() Well-known port TCP Client listen() Socket() accept() blocks until connection from client connect() Connection establishment Data(request) send() recv() process request Data(reply) send() recv() close() End-of-file notification read() close()

Byte Ordering High-order byte Low-order byte MSB 16-bit value LSB Increasing memory address Address A+1 Address A Little-endian byte order High-order byte Low-order byte MSB 16-bit value LSB Consider a 16-bit integer that is made up of 2 bytes: there are two ways to store the 2 bytes in memory, with low order byte at the starting address, known as little endian byte order, or with the high order byte at the starting address, known as big endian byte order Big-endian byte order Low-order byte High-order byte Address A+1 Address A Increasing memory address

Implications of Byte Order Unfortunately there is no standard between these two byte orderings and we encounter systems that use both formats We refer to the byte ordering used by a given system as host byte order The sender and the receiver must agree on the order in which the bytes of these multi-byte field transmitted: specify network byte order, which is big-endian byte ordering

Byte Order Functions #include <netinet.h> /* Host to network */ uint16_t htons(uint16_t host16bitvalue) Converts a 16-bit integer from host to network byte order uint32_t htonl(uint32_t host32bitvalue) Converts a 32-bit integer from host to network byte order Both return: value in network byte order /* Network to host */ uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohl(uint32_t net32bitvalue) Both return: value in host byte order

When do we use hton/ntoh functions? Use hton the port number in struct sockaddr_in If we create a custom struct to hold our headers and data Sending our data through send() and recv() functions E.g., if our first struct member is a 2-byte header, and sender/receiver have different memory orderings, number would look very different to each machine

Socket Address Structures #include <netinet/in.h> // Pointers to socket address structures are often cast to pointers // to this type before use in various functions and system calls: struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; // IPv4 AF_INET sockets: struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // pad to fit into sockaddr struct in_addr { uint32_t s_addr; // load with inet_pton()

Address Conversion Functions #include <arpa/inet.h> int inet_pton(int af, const char *src, void *dst); /* Returns 1 on success, < 1 on error */ Converts the character string src into a network address structure, then copies the network address structure to dst. int inet_aton(const char *strptr, struct in_addr *addrptr); /* return 1 if string was valid,0 error */ Convert an IP address in string format (x.x.x.x) to the 32-bit packed binary format used in low-level network functions

Address Conversion Functions #include <arpa/inet.h> in_addr_t inet_addr(const char *strptr); /* return 32-bit binary network byte ordered IPv4 address; INADDR_NONE if error, deprecated and replaced by inet_aton() */ char *inet_ntoa(struct in_addr inaddr); /* returns: pointer to dotted-decimal string */

Example struct sockaddr_in ip4addr; int s; ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons(3490); inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr); s = socket(PF_INET, SOCK_STREAM, 0); bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);

Man Pages Use man pages to look up useful information $> man cat Get information about shell commands $> man bind Get information about C library/system calls Also tells you which header files to include $> man man Get information about man pages

Man Pages Sometimes you need to specify a man section E.g., printf is both a shell command and a C library call Use either $>man 1 printf $>man 3 printf See http://www.cs.uni.edu/~diesburg/courses/cs3470_ fa13/resources/man_page_levels.htm

C Header Files Located at /usr/include Can find out what functions are available for you to use and struct definitions (Hint: check out <string.h> and <strings.h>)

Byte Manipulation Functions #include <strings.h> /* Berkeley-derived functions */ void bzero(void *dest, size_t nbytes) Set the first part of an object to null bytes void bcopy(const void *src, void *dest, size_t nbytes); int bcmp(const void *ptr1, const void *ptr2, size_t nbytes) /* return:0 if equal, nonzero if unequal */ #include <string.h> /* ANSI C defined functions */ void *memset(void *dest,int c,size_t len) Sets the first len bytes in memory dest to the value of c void *memcpy(void *dest,const void *src, size_t nbytes) void memcmp(const void *ptr1, const void *ptr2, size_t nbytes)

Demo of Program