Unix Network Programming

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

Beej s Guide to Network Programming Using Internet Sockets

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

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

Beej s Guide to Network Programming

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

STUDY OF SOCKET PROGRAMMING

Socket Programming TCP UDP

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

Socket Programming for TCP and UDP

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

Programming with TCP/IP. Ram Dantu

Tutorial on Socket Programming

ICT 6544 Distributed Systems Lecture 5

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

Network Communication

5. System Calls or Bust

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

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

CS 640: Computer Networking

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Group-A Assignment No. 6

TCP: Three-way handshake

CSE 124 Discussion Section Sockets Programming 10/10/17

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

Why a Computer Network??? Stand alone Computer. For What?

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

Socket Programming(2/2)

Department of Computer Science

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

CLIENT-SIDE PROGRAMMING

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

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

CS321: Computer Networks Socket Programming

Network programming(i) Lenuta Alboaie

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

B TCP/IP programming

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

System Programming. Sockets

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Elementary TCP Sockets

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

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

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

Network Programming and Protocl Design. Nixu Ltd.

EEC-484/584 Computer Networks

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

CS321: Computer Networks Introduction to Application Layer

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

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

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

Introduction to Socket Programming

Network Socket Programming - 3 BUPT/QMUL

CS 3516: Computer Networks

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Exam in Computer Networks

CSE 333 SECTION 8. Sockets, Network Programming

A Client-Server Exchange

Network Socket Programming - 3 BUPT/QMUL

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

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

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

CS4700/CS5700 Fundamentals of Computer Networking

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

UNIX Sockets. COS 461 Precept 1

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

CSE 333 SECTION 7. Client-Side Network Programming

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

Network Programming Week #1. K.C. Kim

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

Socket Programming 2007/03/28

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

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

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

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

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

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

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

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

NETWORK AND SYSTEM PROGRAMMING

Communication. Sockets (Haviland Ch. 10)

C Structures in Practice

A. Basic Function Calls for Network Communications

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

The User Datagram Protocol

TCP Network Programming in C

UDP CONNECT TO A SERVER

#1 socket_server.c socket_client.c

SOCKETS. COMP750 Distributed Systems

Lab 0. Yvan Petillot. Networks - Lab 0 1

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Transcription:

Introduction to Computer Networks Polly Huang EE NTU Unix Network Programming The socket struct and data handling System calls Based on Beej's Guide to Network Programming 1

The Unix Socket A file descriptor really The Unix fact When Unix programs do any sort of I/O, they do it by reading or writing to a file descriptor A file descriptor is simply an integer associated with an open file Polly Huang, NTU EE 3 A File Descriptor A file in Unix can be A network connection A FIFO queue A pipe A terminal A real on-the-disk file Or just about anything else Polly Huang, NTU EE 4 2

Jeez, everything in Unix is a file! Well, we know how to handle files! In theory The read() and write() calls allows to communicate through a socket In practice The send() and recv() offer much greater control over your data transmission Polly Huang, NTU EE 6 3

The structs int For the file descriptor struct sockaddr Space holder for types of addresses struct sockaddr_in Specific for the Internet type _in for Internet struct in_addr 4 byte IP address Polly Huang, NTU EE 7 struct sockaddr struct sockaddr { unsigned short sa_family; // address family, AF_xxx }; char sa_data[14]; // 14 bytes of protocol address Polly Huang, NTU EE 8 4

struct sockaddr_in struct sockaddr_in { short int sin_family; // Address family unsigned, AF_INET short int sin_port; // Port number, in network byte order struct in_addr sin_addr; // Internet address, in network byte order unsigned char sin_zero[8]; // Same size as struct sockaddr }; Polly Huang, NTU EE 9 Struct in_addr struct in_addr { // Internet address (a structure for historical reasons) unsigned long s_addr; // that's a 32-bit long, or 4 bytes }; Polly Huang, NTU EE 10 5

Reference Letina be of type struct sockaddr_in ina.sin_addr.s_addr references the 4-byte IP address in network byte order Polly Huang, NTU EE 11 Types of Byte Ordering Network Byte Order Most significant byte first Need conversion from the app program to the network Host Byte Order Least significant byte first Usually no need in app program But need conversion if data coming from the network Polly Huang, NTU EE 12 6

Functions to Convert htons() Host to Network Short htonl() Host to Network Long ntohs() Network to Host Short ntohl() Network to Host Long Polly Huang, NTU EE 13 Storing the IP address ina.sin_addr.s_addr = inet_addr("10.12.110.57 ); Returns 1 on error For unsigned short it s 255.255.255.255 A broadcast address Polly Huang, NTU EE 14 7

A Cleaner Interface #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton(const char *cp, struct in_addr *inp); Polly Huang, NTU EE 15 An Example struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(myport); // short, network byte order inet_aton("10.12.110.57", &(my_addr.sin_addr)); memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct Polly Huang, NTU EE 16 8

Things to Note inet_addr() and inet_aton() both convert IP addresses into the network byte order Not all platforms implement inet_aton() Polly Huang, NTU EE 17 Get the IP Address Back printf("%s", inet_ntoa(ina.sin_addr)); inet_ntoa() returns a pointer to a char* And Polly Huang, NTU EE 18 9

Use strcpy() char *a1, *a2;.. a1 = inet_ntoa(ina1.sin_addr); // this is 192.168.4.14 a2 = inet_ntoa(ina2.sin_addr); // this is 10.12.110.57 printf("address 1: %s\n",a1); printf("address 2: %s\n",a2); This program will print: address 1: 10.12.110.57 address 2: 10.12.110.57 Polly Huang, NTU EE 19 Quiz Time! 20 10

System Calls socket() Creating the File Descriptor #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); domain: AF_INET type: SOCK_STREAM or SOCK_DGRAM protocol: 0 or getprotobyname() Polly Huang, NTU EE 22 11

bind() Associating Port with the FD #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct sockaddr *my_addr, int addrlen); Polly Huang, NTU EE 23 Example (Typical Server) #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define MYPORT 3490 main() { int sockfd; struct sockaddr_in my_addr; sockfd = socket(af_inet, SOCK_STREAM, 0); // do some error checking! my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(myport); // short, network byte order my_addr.sin_addr.s_addr = inet_addr("10.12.110.57"); memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct // don't forget your error checking for bind(): bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));... Polly Huang, NTU EE 24 12

connect() Making a Connection #include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Polly Huang, NTU EE 25 Example (Typical Client) #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define DEST_IP "10.12.110.57" #define DEST_PORT 23 main() { int sockfd; struct sockaddr_in dest_addr; // will hold the destination addr sockfd = socket(af_inet, SOCK_STREAM, 0); // do some error checking! dest_addr.sin_family = AF_INET; // host byte order dest_addr.sin_port = htons(dest_port); // short, network byte order dest_addr.sin_addr.s_addr = inet_addr(dest_ip); memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct // don't forget to error check the connect()! connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));... Polly Huang, NTU EE 26 13

listen() Waiting for Connection #include <sys/socket.h> int listen(int sockfd, int backlog); On the server side, you see typically this: socket(); bind(); listen(); /* accept() goes here */ Polly Huang, NTU EE 27 accept() Getting a Connection #include <sys/socket.h> int accept(int sockfd, void *addr, int *addrlen); Polly Huang, NTU EE 28 14

#include <string.h> The Server Example #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define MYPORT 3490 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold main() { int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_in my _addr; // my address information struct sockaddr_in their_addr; // connector's address information int sin_size; sockfd = socket(af_inet, SOCK_STREAM, 0); // do some error checking! my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(myport); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct // don't forget your error checking for these calls: bind(sockfd, (struct sockaddr *)&my_addr, sizeof(structsockaddr)); listen(sockfd, BACKLOG); sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); Polly Huang,... NTU EE 29 send() and recv() Data Transmission int send(int sockfd, const void *msg, int len, int flags); int recv(int sockfd, void *buf, int len, unsigned int flags); Polly Huang, NTU EE 30 15

Example char *msg = Hello World!"; int len, bytes_sent;.. len = strlen(msg); bytes_sent = send(sockfd, msg, len, 0);... Polly Huang, NTU EE 31 Quiz Time! 32 16

sendto() and recvfrom() Transmission the Datagram Style int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); Or if transmitting over TCP socket, one can simply use send() and recv(). Polly Huang, NTU EE 33 close() and shutdown() Closing the Communication close(sockfd); int shutdown(int sockfd, int how); 0 -- Further receives are disallowed 1 -- Further sends are disallowed 2 -- Further sends and receives are disallowed (like close()) Polly Huang, NTU EE 34 17

Reference Beej's Guide to Network Programming https://beej.us/guide/bgnet/ Additional system calls TCP stream client, server example UDP datagram example Polly Huang, NTU EE 35 18