Socket Programming 2007/03/28

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

Socket Programming TCP UDP

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

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

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

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

Sockets 15H2. Inshik Song

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

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Socket Programming for TCP and UDP

Introduction to Socket Programming

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

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

BSD Sockets API. Mesut Ali ERGIN. Yeditepe University Information and Computer Science Department

Unix Network Programming

CS321: Computer Networks Socket Programming

A Client-Server Exchange

CS 640: Computer Networking

Tutorial on Socket Programming

Network Socket Programming - 3 BUPT/QMUL

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

CS321: Computer Networks Introduction to Application Layer

Network Socket Programming - 3 BUPT/QMUL

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Network programming(i) Lenuta Alboaie

System Programming. Sockets

Introduction to Socket Programming

Elementary TCP Sockets

Ken French HELP Session 1 CS4514

Department of Computer Science

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

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

TCP: Three-way handshake

Socket Programming(2/2)

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

CS 3516: Computer Networks

CLIENT-SIDE PROGRAMMING

Network Programming November 3, 2008

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

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

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

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

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS

UNIX Sockets. COS 461 Precept 1

// socket for establishing connections

Teaching operating systems concepts using the cloud

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Networked Applications: Sockets. End System: Computer on the Net

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

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

ICT 6544 Distributed Systems Lecture 5

31 ChamSys Remote Protocol Commands

Interprocess Communication

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Programming with TCP/IP. Ram Dantu

CSCE 463/612 Networks and Distributed Processing Spring 2017

518 Lecture Notes Week 12

UNIX System Programming Lecture 19: IP Sockets

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols

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

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

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

UNIT IV- SOCKETS Part A

EECS122 Communications Networks Socket Programming. Jörn Altmann

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

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

Network Software Implementations

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

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

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

CSE 124 Discussion Section Sockets Programming 10/10/17

Topics for this Week

Internetworking II: Network programming. April 20, 2000

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Computer Networks Prof. Ashok K. Agrawala

Introduction to Client-Server Model

Socket Programming Example

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

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

Porting a Network Cryptographic Service to the RMC2000: A Case Study in Embedded Software Development

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

NETWORK AND SYSTEM PROGRAMMING

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

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

Transcription:

Socket Programming 2007/03/28

Reference W. Richard Stevens, Unix Network Programming 2/e Volume 1,1998 James F. Kurose and Keith W. Ross, "Computer Networks: A Top-Down Approach Featuring the Internet 3/e, 2002

Server & Client

What is Socket? Service access point of TCP/IP protocol stacks Socket is an interface between Application layer and Transport layer

What is Socket? A socket is a file descriptor that lets an application read/write data from/to the network Once configured the application can pass data to the socket for network transmission receive data from the socket (transmitted through the network by some other host)

TCP Client & Server

socket( ) int s = socket(domain, type, protocol); s: socket descriptor domain: integer, communication domain e.g., AF_INET (IPv4 protocol) typically used type: communication type SOCK_STREAM: reliable, 2-way, connection-based service (TCP) SOCK_DGRAM: unreliable, connectionless (UDP) protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0

socket( ) : an example int s; /* socket descriptor */ if((s = socket(af_inet, SOCK_STREAM, 0)) < 0) { perror( socket ); exit(1); }

bind( ) int status = bind(sockid, &addrport, size); status: error status, = -1 if bind failed sockid: integer, socket descriptor addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY chooses a local address) size: the size (in bytes) of the addrport structure

bind( ) : an example int s; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ /* create the socket */ srv.sin_family = AF_INET; /* use the Internet addr family */ srv.sin_port = htons(8888); /* bind socket fd to port 8888*/ /* bind: a client may connect to any of my addresses */ srv.sin_addr.s_addr = htonl(inaddr_any); if(bind(s, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror("bind"); exit(1); }

listen() int status = listen(sock, queuelen); status: 0 if listening, -1 if error sock: integer, socket descriptor queuelen: integer, # of active participants that can wait for a connection listen is non-blocking: returns immediately

listen( ): an example int s; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ /* 1) create the socket */ /* 2) bind the socket to a port */ if(listen(s, 5) < 0) { perror( listen ); exit(1); }

accept() int s = accept(sock, &name, &namelen); s: integer, the new socket (used for data-transfer) sock: integer, the orig. socket (being listened on) name: struct sockaddr, address of the active participant namelen: sizeof(name): value/result parameter must be set appropriately before call adjusted by OS upon return accept is blocking: waits for connection before returning

accept( ): an example int s; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ struct sockaddr_in cli; /* used by accept() */ int newfd; /* returned by accept() */ int cli_len = sizeof(cli); /* used by accept() */ /* 1) create the socket */ /* 2) bind the socket to a port */ /* 3) listen on the socket */ newfd = accept(s, (struct sockaddr*) &cli, &cli_len); if(newfd < 0) { perror("accept"); exit(1); }

connect() int status = connect(sock, &name, namelen); status: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connection name: struct sockaddr: address of passive participant namelen: integer, sizeof(name) connect is blocking

connect( ): an example int s; /* socket descriptor */ struct sockaddr_in srv; /* used by connect() */ /* create the socket */ /* connect: use the Internet address family */ srv.sin_family = AF_INET; /* connect: socket s to port 8888 */ srv.sin_port = htons(8888); /* connect: connect to IP Address 140.112.1.1 */ srv.sin_addr.s_addr = inet_addr( 140.112.1.1 ); if(connect(s, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror( connect"); exit(1); }

read( ) int s; /* socket descriptor */ char buf[512]; /* used by read() */ int nbytes; /* used by read() */ /* 1) create the socket */ /* 2) bind the socket to a port */ /* 3) listen on the socket */ /* 4) accept the incoming connection */ if((nbytes = read(s, buf, sizeof(buf))) < 0) { perror( read ); exit(1); } read blocks waiting for data from the client but does not guarantee that sizeof(buf) is read

write( ) int s; /* socket descriptor */ char buf[512]; /* used by write() */ int nbytes; /* used by write() */ /* 1) create the socket */ /* 2) bind the socket to a port */ /* 3) listen on the socket */ /* 4) accept the incoming connection */ if((nbytes = write(s, buf, sizeof(buf))) < 0) { perror( write ); exit(1); }

Address & Port Byte Ordering Address and port are stored as integers Problem: different machines / OS s use different word orderings little-endian: lower bytes first big-endian: higher bytes first these machines may communicate with one another over the network

Byte Ordering Solution htons(): Host to Network Short htonl(): Host to Network Long ntohs(): Network to Host Short ntohl(): Network to Host Long

Other Useful Function void bzero(void *dest, size_tnbytes); void bcopy(const void *src, void *dest, size_tnbytes); int bcmp(const void *ptr1, const void *ptr2, size_tnbytes); void *memset(void *dest, intc, size_tnbytes); void *memcpy(void *dest, const void *src, size_tnbytes); int memcmp(const void *ptr1, const void *ptr2, size_tnbytes);

Other Useful Function (Cont ) int gethostname(char *name, int len) struct hostent * gethostbyaddr(char *addr, int len, int type): in_addr_t inet_addr(const char *cp) char * inet_ntoa(const struct in_addr in) Make sure to #include the header files!