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

Similar documents
Unix Network Programming

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

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

Network Communication

TCP: Three-way handshake

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

STUDY OF SOCKET PROGRAMMING

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

Group-A Assignment No. 6

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

Socket Programming TCP UDP

CSE 124 Discussion Section Sockets Programming 10/10/17

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

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

EEC-484/584 Computer Networks

Tutorial on Socket Programming

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

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

Elementary TCP Sockets

Socket Programming for TCP and UDP

CS321: Computer Networks Socket Programming

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

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

UNIX Sockets. COS 461 Precept 1

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

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

Εργαστήριο 9 I/O Multiplexing

Programming with TCP/IP. Ram Dantu

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

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

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

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

Sockets 15H2. Inshik Song

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

ICT 6544 Distributed Systems Lecture 5

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

CLIENT-SIDE PROGRAMMING

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

5. System Calls or Bust

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

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

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

CS321: Computer Networks Introduction to Application Layer

What s an API? Do we need standardization?

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

CSE 333 Section 8 - Client-Side Networking

Communication. Sockets (Haviland Ch. 10)

CS 640: Computer Networking

Socket Programming(2/2)

A. Basic Function Calls for Network Communications

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Network Programming Week #1. K.C. Kim

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

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

Server-side Programming

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

Computer Networks Prof. Ashok K. Agrawala

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

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

Introduction to Socket Programming

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

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

Server-side Programming

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Beej s Guide to Network Programming Using Internet Sockets

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

Network Socket Programming - 3 BUPT/QMUL

Internet applications

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

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

UNIX System Programming Lecture 19: IP Sockets

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

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

Network Socket Programming - 3 BUPT/QMUL

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

Introduction to Socket Programming

Socket Programming 2007/03/28

UNIT IV- SOCKETS Part A

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

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Network programming(i) Lenuta Alboaie

API Reference 12/13/2014

The User Datagram Protocol

Network Software Implementations

CS 3516: Computer Networks

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

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

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

CSE 333 SECTION 8. Sockets, Network Programming

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

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

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

CS 351 Week 15. Course Review

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

Transcription:

A Socket Example & George Mason University

Everything is a file descriptor Most socket system calls operate on file descriptors Server - Quick view socket() bind() listen() accept() send(), recv() close() - setup the file descriptor - setup socket comm parameters - wait for incoming connections - process incoming connections - send and receive data - close/terminate connection

int socket(int domain, int type, int protocol); domain = PF_INET or PF_INET6 type = SOCK_STREAM or SOCK_DGRAM protocol = 0 choose the proper protocol for the given type return = the socket file descriptor

Getting a TCP socket file descriptor socket(pf_inet, SOCK_STREAM, 0); Getting a UDP socket file descriptor socket(pf_inet, SOCK_DGRAM, 0);

(sockfd, struct sockaddr *my_addr, int addrlen); () socket sockfd = socket file descriptor returned by my_addr = pointer to struct of type sockaddr addrlen = the size of the above struct

my_addr.sin_family = AF_INET; my_addr.sin_port = htons(1234); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero)); bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));

int listen(int sockfd, int backlog); () socket Sockfd = socket file descriptor returned by Backlog = the number of connections allowed on the incoming queue Example listen(sockfd, 10);

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); () select sockfd = socket file descriptor returned by addr = pointer to struct of type sockaddr addrlen = pointer to an int equal to the size of the addr struct Return = the file descriptor of this connection

int new_fd, addr_size; struct sockaddr_in their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);

int send(int sockfd, const void *msg, int len, int flags); () select sockfd = socket file descriptor returned by msg = the buffer with the data we want to send len = the size of the msg buffer flags = additional flags return = the number of bytes sent

int recv(int sockfd, void *buf, int len, unsigned int flags); sockfd = () select socket file descriptor returned by buf = the buffer to store the data len = the size of the above buffer flags = additional flags return = the number of bytes received

int close(int sockfd); sockfd = the file descriptor associated with the connection we want to terminate

Some functions block while waiting for an event accept() blocks while waiting for a new connection recv() blocks while waiting for new data to arrive We can not use two or more blocking functions on the same process.

() clone Using fork() or One process is responsible for accepting new connections For every connection we create a new process to send() and recv() data () select Using Monitor all sockets for reading and writing When a socket is ready for reading you can call a blocking function on that socket and it is guaranteed that recv() is not going to block

(;;) for { /* accept blocks until we have a new connection */ client_fd = accept(listen_fd, (struct sockaddr *)&cli_addr, &cli_addrlen); /* we save the file descriptor on global array all_fds[] */ all_fds[client_fd] = client_fd; } /* we create a new process that will handle this connection */ clone(handle_client, (void **) (malloc(0x10000) + 0xFFFC), CLONE_FILES, (void *)client_fd);

() fork (. etc Sets up a default environment (stack, file descriptor table () fork Execution continues from the point where we called () clone We have control over the forking environment Share resources (like file descriptor table) between processes Execution continues from a user specified function

(;;) for { select(fd_setsize, &read_fds, NULL, NULL, NULL); (++ fd for (fd = 0; fd < FD_SETSIZE; { (( read_fds & if (FD_ISSET(fd, { read(fd, data, 512); (++ i for (i = 0; i < FD_SETSIZE; { ( listen_fd if (FD_ISSET(i, &all_fds) && i!= fd && i!= write(i, data, data_size); } } } }

Multi-process Server and Client using clone() and select() http://cs.gmu.edu/~astavrou/courses/cs_571_f09/cprocesstutorial