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

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

Client/Server. Networking Approach.

Presenting the CLIO 10 measurement system. Audiomatica

Cracking WEP Keys Using WEPCrack

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

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

Client software design

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

CS307 Operating Systems Processes

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

Local Area Networks; Ethernet

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Piotr Mielecki Ph. D.

Advanced Programming Techniques. Networks. Christopher Moretti

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CAS: Central Authentication Service

ECE 435 Network Engineering Lecture 2

CS4514 B08 HELP Session 1

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

Quadrocopter Flight Control

CS4700/CS5700 Fundamentals of Computer Networking

Application Programming Interfaces

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

Sockets Sockets Communication domains

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

ECE 435 Network Engineering Lecture 2

System Programming. Sockets: examples

Process Communication COMPUTER NETWORKING Part 2

The User Datagram Protocol

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

Linux Network Programming, Part 1

How to write a Measurement Telnet Server

Department of Computer Science

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

UDP CONNECT TO A SERVER

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

Chapter 2 Applications and

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

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

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

A. Basic Function Calls for Network Communications

CS 3516: Computer Networks

// socket for establishing connections

Tutorial on Socket Programming

Introduction to Berkeley Sockets

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

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

Elementary TCP Sockets

CS321: Computer Networks Introduction to Application Layer

CS321: Computer Networks Socket Programming

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

Introduction to Client-Server Model

Introduction to Berkeley Sockets

Introduction to Berkeley Sockets

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


The Transport Layer. The need for the transport layer

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

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

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

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

Programming with TCP/IP. Ram Dantu

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

System Programming. Sockets

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

Azblink API for Sending XMPP Messages via HTTP POST

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

Unix Network Programming

How to write a Measurement Telnet Server

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

INTEGRATED INFORMATION AND COMMUNICATION LEARNING MODEL FOR RASPBERRY Pi ENVIRONMENT

2007 Microsoft Corporation. All rights reserved.

Programming Requirements. Project & Programming Overview. Local Repository SVN 1/10/2011. Version Control: SVN Makefiles Development Language: C only

Interprocess Communication. Interprocess Communication

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 29, 10 13:41 Page 1/1 Apr 29, 10 11:51 Page 1/1

Lab 0. Yvan Petillot. Networks - Lab 0 1

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

Introduction to Socket Programming

CS240: Programming in C

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

CSCE 463/612 Networks and Distributed Processing Spring 2017

>>> SOLUTIONS <<< Answer the following questions regarding the basics principles and concepts of networks.

CSE 333 Lecture 16 - network programming intro

CS 640: Computer Networking

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

NETWORK AND SYSTEM PROGRAMMING

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

Sockets 15H2. Inshik Song

Week 6 Music Control and Networks

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

#1 socket_server.c socket_client.c

Programming Assignment 1 (30 pts) A Client Dynamically Accessing Servers Using the Net Oracle


CSE 333 Lecture 8 - file and network I/O

CSCI 415 Computer Networks Homework 2 Due 02/13/08

Transcription:

Client-Server Model Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server. servers provide services requested by clients. A client sends a message requesting service to the server, and the server returns its response in a response message. Note: a process can be a client and a server at the same time. Ports Need to locate a process. Use a port (like a mailbox). There are two types of ports: well known ports those that rarely change over time. For instance, servers that provide mail, file transfer, remote login, etc. dynamic ports typically used only for the life of a process. For instance, pipes can be implemented using message passing. CS 3013 1 week6-unixsock.tex

Examples of Servers mail server allows client mail program to connect to mail server on remote machine. login server allows clients to establish login sessions on remote machine. file servers client requests to read or write part of a file. The server might support several operations, including read, write, open, close and seek. web server handles HTTP requests. sort server takes a list of items from the client, sorts them, and returns the sorted list authentication server grants permissions to access files, log into the system, etc. CS 3013 2 week6-unixsock.tex

Sockets How do we provide interprocess communication between processes that do not share a common ancestor? Sockets are a generalization of file input/output that supports interprocess communication. Solution: applications use sockets and bind port names to them. Processes then send messages to the bound (port) name. Finger Server Example 1. Create socket (using socket() (don t look at details) 2. Bind port number to socket. Look in /etc/services using getservbyname(). Well-known port number. 3. Wait for request. On request gather information and send response to requesting port. Finger Client: 1. Create socket and bind arbitrary port number to socket. 2. Get port number and host address of finger port. 3. Send request to finger port (containing client port address). 4. Wait for response, read it, and cleanup. Describe for a server that forks off a process to handle the connection. Use an alternate port for the server. Look at picture! CS 3013 3 week6-unixsock.tex

Code Example Client send user name and receives back a message. < ccc1 1 >make sockclient cc -g sockclient.c -o sockclient < ccc1 2 >make sockserver cc -g sockserver.c -o sockserver < ccc1 3 >./sockserver 4242& [1] 28721 < ccc1 4 >./sockclient ccc1 4242 Hello cew, you are visitor number 1 to this server < ccc1 5 >./sockclient ccc1 4242 Hello cew, you are visitor number 2 to this server < ccc1 6 >kill %1 CS 3013 4 week6-unixsock.tex

Client / adapted from Sample 25_1 from "Computer Networks and Internets, 2nd ed by Comer / / sockclient.c - code for example client program that uses TCP / #ifndef unix #define WIN32 #include <windows.h> #include <winsock.h> #else #define closesocket close #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #endif #include <stdio.h> #include <string.h> #include <pwd.h> #define PROTOPORT 5193 / default protocol port number / extern int errno; char localhost[] = "localhost"; / default host name / /------------------------------------------------------------------------ Program: client Purpose: allocate a socket, connect to a server, and print all output Syntax: client [ host [port] ] host - name of a computer on which server is executing port - protocol port number server is using Note: Both arguments are optional. If no host name is specified, the client uses "localhost"; if no protocol port is specified, the client uses the default given by PROTOPORT. ------------------------------------------------------------------------ / main(argc, argv) int argc; CS 3013 5 week6-unixsock.tex

char argv[]; { struct hostent ptrh; / pointer to a host table entry / struct protoent ptrp; / pointer to a protocol table entry / struct sockaddr_in sad; / structure to hold an IP address / int sd; / socket descriptor / int port; / protocol port number / char host; / pointer to host name / int n; / number of characters read / char buf[1000]; / buffer for data from the server / #ifdef WIN32 WSADATA wsadata; WSAStartup(0x0101, &wsadata); #endif struct passwd ppwd; memset((char )&sad,0,sizeof(sad)); / clear sockaddr structure / sad.sin_family = AF_INET; / set family to Internet / / Check command-line argument for protocol port and extract / / port number if one is specified. Otherwise, use the default / / port value given by constant PROTOPORT / if (argc > 2) { / if protocol port specified / port = atoi(argv[2]); / convert to binary / else { port = PROTOPORT; / use default port number / if (port > 0) / test for legal value / sad.sin_port = htons((u_short)port); else { / print error message and exit / fprintf(stderr,"bad port number %s\n",argv[2]); / Check host argument and assign host name. / if (argc > 1) { host = argv[1]; / if host argument specified / else { host = localhost; / Convert host name to equivalent IP address and copy to sad. / ptrh = gethostbyname(host); CS 3013 6 week6-unixsock.tex

if ( ((char )ptrh) == NULL ) { fprintf(stderr,"invalid host: %s\n", host); memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); / Map TCP transport protocol name to protocol number. / if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); / Create a socket. / sd = socket(pf_inet, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "socket creation failed\n"); / Connect the socket to the specified server. / if (connect(sd, (struct sockaddr )&sad, sizeof(sad)) < 0) { fprintf(stderr,"connect failed\n"); / Repeatedly read data from socket and write to user s screen. / if ((ppwd = getpwuid(getuid())) == NULL) { fprintf(stderr,"could not get user name\n"); / send our user name / send(sd, ppwd->pw_name, strlen(ppwd->pw_name)+1, 0); n = recv(sd, buf, sizeof(buf), 0); while (n > 0) { write(1,buf,n); n = recv(sd, buf, sizeof(buf), 0); / Close the socket. / closesocket(sd); CS 3013 7 week6-unixsock.tex

/ Terminate the client program gracefully. / exit(0); CS 3013 8 week6-unixsock.tex

Server / adapted from Sample 25_2 from "Computer Networks and Internets, 2nd ed by Comer / / sockserver.c - code for example server program that uses TCP / #ifndef unix #define WIN32 #include <windows.h> #include <winsock.h> #else #define closesocket close #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #endif #include <stdio.h> #include <string.h> #define PROTOPORT 5193 / default protocol port number / #define QLEN 6 / size of request queue / int visits = 0; / counts client connections / /------------------------------------------------------------------------ Program: server Purpose: allocate a socket and then repeatedly execute the following: (1) wait for the next connection from a client (2) send a short message to the client (3) close the connection (4) go back to step (1) Syntax: server [ port ] port - protocol port number to use Note: The port argument is optional. If no port is specified, the server uses the default given by PROTOPORT. ------------------------------------------------------------------------ / main(argc, argv) int argc; CS 3013 9 week6-unixsock.tex

{ char argv[]; struct hostent ptrh; / pointer to a host table entry / struct protoent ptrp; / pointer to a protocol table entry / struct sockaddr_in sad; / structure to hold server s address / struct sockaddr_in cad; / structure to hold client s address / int sd, sd2; / socket descriptors / int port; / protocol port number / int alen; / length of address / char buf[1000]; / buffer for string the server sends / char ubuf[100]; / buffer for user string / #ifdef WIN32 WSADATA wsadata; WSAStartup(0x0101, &wsadata); #endif memset((char )&sad,0,sizeof(sad)); / clear sockaddr structure / sad.sin_family = AF_INET; / set family to Internet / sad.sin_addr.s_addr = INADDR_ANY; / set the local IP address / / Check command-line argument for protocol port and extract / / port number if one is specified. Otherwise, use the default / / port value given by constant PROTOPORT / if (argc > 1) { / if argument specified / port = atoi(argv[1]); / convert argument to binary / else { port = PROTOPORT; / use default port number / if (port > 0) / test for illegal value / sad.sin_port = htons((u_short)port); else { / print error message and exit / fprintf(stderr,"bad port number %s\n",argv[1]); / Map TCP transport protocol name to protocol number / if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); / Create a socket / CS 3013 10 week6-unixsock.tex

sd = socket(pf_inet, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "socket creation failed\n"); / Bind a local address to the socket / if (bind(sd, (struct sockaddr )&sad, sizeof(sad)) < 0) { fprintf(stderr,"bind failed\n"); / Specify size of request queue / if (listen(sd, QLEN) < 0) { fprintf(stderr,"listen failed\n"); / Main server loop - accept and handle requests / while (1) { alen = sizeof(cad); if ( (sd2=accept(sd, (struct sockaddr )&cad, &alen)) < 0) { fprintf(stderr, "accept failed\n"); if (recv(sd2, ubuf, sizeof(ubuf), 0) > 0) { visits++; sprintf(buf, "Hello %s, you are visitor number %d to this server\n", ubuf, visits); send(sd2,buf,strlen(buf),0); closesocket(sd2); CS 3013 11 week6-unixsock.tex