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

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

Concurrent Programming

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Concurrent Programming

Network Programming November 3, 2008

Introduction to Client-Server Model

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

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

Elementary TCP Sockets

Socket Programming TCP UDP

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

CS 3516: Computer Networks

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Piotr Mielecki Ph. D.

Network Programming / : Introduc2on to Computer Systems 21 st Lecture, April. 4, Instructors: Todd Mowry and Anthony Rowe

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

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

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

Network Programming /18-243: Introduc3on to Computer Systems 22 nd Lecture, 13 April Instructors: Bill Nace and Gregory Kesden

Introduction to Socket Programming

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

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

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

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

Network Socket Programming - 3 BUPT/QMUL

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

CMPSC 311- Introduction to Systems Programming Module: Network Programming

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010

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

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

Application Programming Interfaces

04 Elementary. Client/Server. CEN 463 Network Programming. Dr. Mostafa Hassan Dahshan. King Saud University

Network Socket Programming - 3 BUPT/QMUL

Chapter 2 Applications and

CS 640: Computer Networking

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

ECE 435 Network Engineering Lecture 2

Network Communication

Tutorial on Socket Programming

NETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

ECE 435 Network Engineering Lecture 2

CS307 Operating Systems Processes

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

Department of Computer Science

UNIX Network Programming. Overview of Socket API Network Programming Basics

CS4700/CS5700 Fundamentals of Computer Networking

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Client software design

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

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

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

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

Programming with TCP/IP. Ram Dantu

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

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

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

The Transport Layer. The need for the transport layer

Unix Network Programming

CSE 333 SECTION 8. Sockets, Network Programming

UNIT IV- SOCKETS Part A

Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

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

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

Lab 0. Yvan Petillot. Networks - Lab 0 1

Internetworking II: Network programming. April 20, 2000

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Client Server Computing

CSE 333 SECTION 7. Client-Side Network Programming

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

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

STUDY OF SOCKET PROGRAMMING

ECE 650 Systems Programming & Engineering. Spring 2018

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Client/Server. Networking Approach.

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

TCP: Three-way handshake

CS321: Computer Networks Introduction to Application Layer

CS321: Computer Networks Socket Programming

Network programming(i) Lenuta Alboaie

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

SOCKETS. COMP750 Distributed Systems

Example Questions for Midterm EE122, Fall 2008 EECS Berkeley

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

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

ICT 6544 Distributed Systems Lecture 5

CONCURRENT PROGRAMMING. Lecture 5

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

Transcription:

1 Sockets Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University http://nyx.skku.ac.kr

Echo Client (1) 2 #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, cfd; struct hostent *h; struct sockaddr_in saddr; char buf[maxline]; char *host = argv[1]; int port = atoi(argv[2]); if ((cfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf("socket() failed.\n"); exit(1);

Echo Client (2) 3 if ((h = gethostbyname(host)) == NULL) { printf("invalid hostname %s\n", host); exit(2); bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; bcopy((char *)h->h_addr, (char *)&saddr.sin_addr.s_addr, h->h_length); saddr.sin_port = htons(port); if (connect(cfd,(struct sockaddr *)&saddr,sizeof(saddr)) < 0) { printf("connect() failed.\n"); exit(3); while ((n = read(0, buf, MAXLINE)) > 0) { write(cfd, buf, n); n = read(cfd, buf, MAXLINE); write(1, buf, n); close(cfd);

bind() 4 int bind (int sockfd, struct sockaddr *myaddr, socklen_t addrlen) bind() gives the socket sockfd the local address myaddr. myaddr is addrlen bytes long. Servers bind their well-known port when they start. If a TCP server binds a specific IP address to its socket, this restricts the socket to receive incoming client connections destined only to that IP address. Normally, a TCP client let the kernel choose an ephemeral port and a client IP address.

listen() 5 int listen (int sockfd, int backlog) listen() converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests. When a socket is created, it is assumed to be an active socket, that is, a client socket that will issue a connect(). backlog specifies the maximum number of connections that the kernel should queue for this socket. Historically, a backlog of 5 was used, as that was the maximum value supported by 4.2BSD. Busy HTTP servers must specify a much larger backlog, and newer kernels must support larger values.

accept() (1) 6 int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) accept() blocks waiting for a connection request. accept() returns a connected descriptor with the same properties as the listening descriptor. The kernel creates one connected socket for each client connection that is accepted. Returns when the connection between client and server is created and ready for I/O transfers. All I/O with the client will be done via the connected socket. The cliaddr and addrlen arguments are used to return the address of the connected peer process (the client)

accept() (2) 7 Client clientfd listenfd(3) Server 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd. Client clientfd Connection request listenfd(3) Server 2. Client makes connection request by calling and blocking in connect. Client clientfd listenfd(3) Server connfd(4) 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd.

accept() (3) 8 Listening descriptor End point for client connection requests Created once and exists for lifetime of the server Connected descriptor End point of the connection between client and server A new descriptor is created each time the server accepts a connection request from a client. Exists only as long as it takes to service client. Why the distinction? Allows for concurrent servers that can communicate over many client connections simultaneously.

Echo Server (1) 9 #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <arpa/inet.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, listenfd, connfd, caddrlen; struct hostent *h; struct sockaddr_in saddr, caddr; char buf[maxline]; int port = atoi(argv[1]); if ((listenfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf("socket() failed.\n"); exit(1);

Echo Server (2) 10 bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(inaddr_any); saddr.sin_port = htons(port); if (bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { printf("bind() failed.\n"); exit(2); if (listen(listenfd, 5) < 0) { printf("listen() failed.\n"); exit(3); while (1) { caddrlen = sizeof(caddr); if ((connfd = accept(listenfd, (struct sockaddr *)&caddr, &caddrlen)) < 0) { printf ("accept() failed.\n"); continue;

Echo Server (3) 11 h = gethostbyaddr((const char *)&caddr.sin_addr.s_addr, sizeof(caddr.sin_addr.s_addr), AF_INET); printf("server connected to %s (%s)\n", h->h_name, inet_ntoa(*(struct in_addr *)&caddr.sin_addr)); // echo while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ("got %d bytes from client.\n", n); write(connfd, buf, n); printf("connection terminated.\n"); close(connfd);

Echo Server (4) 12 Client socket Server socket bind connect Connection request listen accept write read close EOF read write read Await connection request from next client close

Exercise Client program Server (127.0.0.1) 로수식전달 Ex. 1 + 231 * 43 Server program Client 에서전달받은수식계산하여 client 로전송