Unix Network Programming



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

Socket Programming. Srinidhi Varadarajan

Tutorial on Socket Programming

ELEN 602: Computer Communications and Networking. Socket Programming Basics

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

Communication Networks. Introduction & Socket Programming Yuval Rochman

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

Implementing Network Software

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

Lab 4: Socket Programming: netcat part

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

The POSIX Socket API

Introduction to Socket programming using C

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

TCP/IP - Socket Programming

Programmation Systèmes Cours 9 UNIX Domain Sockets

Computer Networks Network architecture

NS3 Lab 1 TCP/IP Network Programming in C

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

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

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

UNIX Sockets. COS 461 Precept 1

Socket Programming in C/C++

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

BSD Sockets Interface Programmer s Guide

Lecture 7: Introduction to Sockets

UNIX. Sockets. mgr inż. Marcin Borkowski

Network Programming with Sockets. Process Management in UNIX

CSE 333 SECTION 6. Networking and sockets

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

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

Application Architecture

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

This tutorial has been designed for everyone interested in learning the data exchange features of Unix Sockets.

Windows Socket Programming & IPv6 Translation Middleware

Network Programming with Sockets. Anatomy of an Internet Connection

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

VMCI Sockets Programming Guide VMware ESXi 5.5 VMware Workstation 9.x

Writing a C-based Client/Server

TCP/IP Sockets in C. Practical Guide for Programmers

Operating Systems Design 16. Networking: Sockets

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

Client / Server Programming with TCP/IP Sockets

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

Concurrent Server Design Alternatives

An Introductory 4.4BSD Interprocess Communication Tutorial

Programming with TCP/IP Best Practices

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

IBM i Version 7.2. Programming Socket programming IBM

Generalised Socket Addresses for Unix Squeak

Packet Sniffing and Spoofing Lab

Beej's Guide to Network Programming

IPv6 Enabling CIFS/SMB Applications

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

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

sys socketcall: Network systems calls on Linux

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

Network Programming. Chapter The Client-Server Programming Model

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

SSC - Communication and Networking Java Socket Programming (II)

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

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

Hardware Prerequisites Atmel SAM W25 Xplained Pro Evaluation Kit Atmel IO1 extension Micro-USB Cable (Micro-A / Micro-B)

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

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

How To Write On A Non-Blocking File On A Thumbtongue (Windows) (Windows 2) (Macintosh) (Amd64) (Apple) (Powerpoint) (Networking) (Unix) (Program) (

Porting and Deploying VoIP to IPv6: Lessons Learned

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

Network Programming using sockets

Name and Address Conversions

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Software changes for Website and Application IPv6 Readiness

Implementing and testing tftp

Programming With Sockets 2

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

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

App Note. VoxStack GSM Gateway API

Elementary Name and Address. Conversions

The exam has 110 possible points, 10 of which are extra credit. There is a Word Bank on Page 8. Pages 7-8 can be removed from the exam.

RPG Does TCP/IP (Socket Progamming in RPG IV)

Best practices in IPv6 enabled networking software development.

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

Network Programming TDC 561

CS162 Operating Systems and Systems Programming Lecture 4. Introduction to I/O (Continued), Sockets, Networking. Recall: Fork and Wait

Elementary Name and Address Conversions

Application Note: AN00121 Using XMOS TCP/IP Library for UDP-based Networking

University of Amsterdam

Firewall Implementation

AT12181: ATWINC1500 Wi-Fi Network Controller - AP Provision Mode. Introduction. Features. Atmel SmartConnect APPLICATION NOTE

Transcription:

Introduction to Computer Networks Polly Huang EE NTU http://cc.ee.ntu.edu.tw/~phuang phuang@cc.ee.ntu.edu.tw 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 _infor 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 Let ina 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 addr s addr = inet_addr( addr("10.12.110.57 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)); ntoa(ina 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 System Calls 10

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 21 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 22 11

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 23 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 24 12

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 dd dd dd = 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 25 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 26 13

accept() Getting a Connection #include <sys/socket.h> int accept(int sockfd, void *addr, int *addrlen); Polly Huang, NTU EE 27 #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(struct sockaddr)); listen(sockfd, BACKLOG); sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); Polly Huang,... NTU EE 28 14

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 29 Example char *msg = Hello World!"; int len, bytes_sent;.. len = strlen(msg); bytes_sent = send(sockfd, msg, len, 0);... Polly Huang, NTU EE 30 15

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 31 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 d (like close()) Polly Huang, NTU EE 32 16

Reference Beej's Guide to Network Programming http://www.ecst.csuchico.edu/~beej/guide/net/ Additional system calls TCP stream client, server example UDP datagram example Polly Huang, NTU EE 33 17