Client-Server Model ● Frequently used programming paradigms – client-server model: one process provides services for a set of client process, eg. HTTP.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Socket Programming Application Programming Interface.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 JMH Associates © 2000, All rights reserved Chapter 12 Windows Sockets and Network Programming.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
 Wind River Systems, Inc Chapter - 13 Network Programming.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Chapter 2 Applications and Layered Architectures Sockets.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
Assignment 3 A Client/Server Application: Chatroom
Sockets and Beginning Network Programming
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
Week 13 - Friday CS222.
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Client-Server Model ● Frequently used programming paradigms – client-server model: one process provides services for a set of client process, eg. HTTP server-web browser. X server- X client. – master-slave: one master process assigns tasks to a group of slave processes, eg. Cluster Scheduler-Cluster Node, kernel- user processes.

Using Shell Programs ● popen: NAME popen, pclose - process I/O SYNOPSIS #include FILE *popen(const char *command, const char *type); int pclose(FILE *stream); DESCRIPTION The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string contain- ing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The type argument is a pointer to a null-terminated string which must be either 'r' for reading or 'w' for writing.

Using Shell Programs ● popen: The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the pro- cess that called popen(), unless this is altered by the command itself. Conversely, reading from a ''popened'' stream reads the command's stan- dard output, and the command's standard input is the same as that of the process that called popen. Note that output popen streams are fully buffered by default. The pclose function waits for the associated process to terminate and returns the exit status of the command as returned by wait4.

Using Shell Programs ● popen: ● System fp = popen( "who|sort", "r" ); /* open the command */ while ( fgets( buf, 100, fp ) != NULL )/* read from command */ printf("%3d %s", i++, buf ); /* print data */ pclose( fp ); /* IMPORTANT! */ return 0; system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

Converting a file descriptor to a stream ● Streams: – Buffered for efficiency – Easier data conversion (fprintf ) ● FILE *file: file=fdopen(fd)

POPEN.C pipe(p) fork() | close(p[1]); close(p[1]); fp=fdopen(p[0],”r”); dup2(p[1],1); return fp; execl(“/bin/sh”,”sh”,”-c”, cmd, NULL);

Sockets

● Setting up a server 1. get a socket = phone line 2. bind address to socket = get a phone number 3. listen = allow incoming calls 4. connect,write,close=provide service.

Sockets ● Setting up a server 1. get a socket = phone line 2. bind address to socket = get a phone number 3. listen = allow incoming calls 4. connect,write,close=provide service. #include int socket(int domain, int type, int protocol); domain tells the call where the socket is to be used. For example PF_INET specifies the internet domain for networking. Another domain, is PF_UNIX, which is used if the processes are on the same machine. The type of the socket to be created specifies whether it is to be used in a connection or connectionless mode. SOCK_STREAM specifies a connection oriented link, SOCK_DGRAM a connectionless link. The final parameter, protocol, specifies which protocol should be used by this socket. This will normally be set to 0, in which case, by default, a SOCK_STREAM socket wiIl use TCP and a SOCK_DGRAM socket will use UDP-both standard UNIX protocols. The socket system call normally returns a non-negative integer which is the socket file descriptor, which enables sockets to be treated using the familiar UNIX file model.

Sockets ● Setting up a server 1. get a socket = phone line 2. bind address to socket = get a phone number 3. listen = allow incoming calls 4. accept,write,close=provide service. #include int bind(int sockfd, const struct sockaddr *address,size_t add_len); The first parameter, sockfd, is the socket file descriptor originally returned by the socket system call. The second parameter is a pointer to a generic socket structure. However, because we are sending information across the network in our example, we will actually provide the address of the relevant struct sockaddr_in which contains the addressing information for our server. The final parameter holds the size of the actual socket structure used. If the bind call is successful then it returns 0. On error, the bind call returns -1, which may happen if a socket already exists for the address. The errno will then contain EADDRINUSE.

Sockets ● Setting up a server 1. get a socket = phone line 2. bind address to socket = get a phone number 3. listen = allow incoming calls 4. accept,write,close=provide service. #include int listen(int sockfd, int queue-size)int listen(int sockfd, int queue-size); The sockfd parameter is as above. The server can queue up to queue-size incoming connection requests.

Sockets ● Setting up a server 1. get a socket = phone line 2. bind address to socket = get a phone number 3. listen = allow incoming calls 4. accept,write,close=provide service. include #include int accept(int sockfd, struct sockaddr *address,size-t *add_len); The accept system call is passed the listening socket descriptor returned from the original socket system call. On completion, the return value is the new socket id to be used for the communication. The address parameter is filled out with the information about the client. However, because this is a connection oriented communication the server very rarely needs to know the address of the client, and therefore address can be replaced with NULL. If address is not NULL then the variable pointed to by add_len should initially contain the length of the address structure described by address. On the return of the accept call, *add_len will hold the number of bytes actually copied.

Sockets 4. accept,write,close=provide service. #include read write Also: size_t recv(int sockfd, void *buffer, size_t length,int flags); size_t send(int sockfd, const void *buffer, size_t length,int flags); The recv call specifies the file descriptor to read the data from, the buffer into which the data should be put, and the length of the buffer. As with read, recv returns the amount of data read. The flags parameter affects the way in which the data can be received. The possible values are: MSG_PEEK: The process can look at the data without actually receivingit. MSG_OOB: Normal data is bypassed and the process only receives out of band data, for example, an interrupt signal. MSG_WAITALL: The recv call will only return when the full amount of data is available. send behaves just like write if the flags parameter is set to 0. It sends the message contained in buffer to sockfd, the local socket. The length parameter specifies the length of buffer. As with recv the flags parameter affects the way that messages are sent. The possible values are: MSG-OOB: Send 'out of band' data.

Client ● Setting up a client 1. get a socket = phone line 2. connect 3. write,close=provide service.

Sockets:Debugging, Info ● Using web browser: – enter ip address: sauron.cs.kent.edu:7001 – tcpserver.c listening on 7001 ● Look at man 7 socket ● Look at socklib.c on class page ● Look at rfc1945.txt on class page

TimeServer2 main() { int sock, fd; sock = make_server_socket( PORTNUM ); if ( sock == -1 ) oops( "make_server_socket" ); while( ( fd = accept(sock,NULL,NULL) ) != -1 ) { process_request(fd); close(fd); } process_request(fd) /* * send the date out to the client via fd */ { int pid = fork(); if ( pid == -1 ) return ; /* error getting a new process */ if ( pid != 0 ) return; /* parent does not wati */ /* child code here */ dup2( fd, 1 ); /* moves socket to fd 1 */ close(fd); /* closes socket */ execlp("date","date",NULL); /* exec date */ oops("execlp date"); }

TimeServer2d main() { int sock, fd; sock = make_server_socket( PORTNUM ); if ( sock == -1 ) oops( "make_server_socket" ); while( ( fd = accept(sock,NULL,NULL) ) != -1 ) { process_request(fd); close(fd); } process_request(fd) /* * send the date out to the client via fd */ { int pid = fork(); if ( pid == -1 ) return ; /* error getting a new process */ if ( pid != 0 ){ /* parent */ wait(NULL); /* do we have to wait? */ return; /* what about zombies? */ } /* child code here */ dup2( fd, 1 ); /* moves socket to fd 1 */ close(fd); /* closes socket */ execlp("date","date",NULL); /* exec date */ oops("execlp date");}

Windows Sockets ● Initialize the WinSock DLL ● int WSAStartup( WORD wVersionRequired, LPWSADATA lpWSAData);

Windows Sockets wVersionRequired – Indicates the highest version of the WinSock DLL you need – Returns a non-zero value if the DLL cannot support the version you want – Low byte specifies the major version – High byte specifies the minor version – 0x0101  version 1.1 lpWSAData points to a WSADATA structure that returns information on the configuration of the DLL WSAGetLastError() for error number

Allocate a Socket Sockets are analogous to handles – A communication channel Call socket() to create (or open) a socket – Actually a HANDLE

Allocate a Socket – Server: “Listening socket” for client connection requests typedef unsigned int SOCKET; SOCKET socket(int af, int type, int protocol);

Allocate a Socket – Server: “Listening socket” for client connection requests typedef unsigned int SOCKET; SOCKET socket(int af, int type, int protocol);

Allocate a Socket – af denotes the address family – PF_INET or AF_INET designates the Internet protocol – type specifies connection-oriented ( SOCK_STREAM ) or datagram communications ( SOCK_DGRAM ) – protocol unnecessary for TCP/IP ● Use 0 socket returns INVALID_SOCKET upon failure

BIND Next, bind the socket to its address and service endpoint int bind ( SOCKET s, const struct sockaddr *saddr, int namelen); – s is an “unbound” SOCKET returned by socket() – saddr specifies the address family and protocol-specific information – namelen is sizeof(sockaddr) Returns SOCKET_ERROR in case of error

BIND sa_data is protocol-specific TCP/IP sockaddr_in : struct sockaddr_in { shortsin_family; // AF_INET u_shortsin_port; structin_addr sin_addr; //4-byte IP addr char sinzero [8]; }; typedef struct sockaddr_in SOCKADDR_IN, *PSOCKADDR_IN;

BIND List of hosts (mapped to IP addresses) can be found in %SystemRoot%\system32\drivers\etc\hosts List of Services(mapped to services) can be found in %SystemRoot%\system32\drivers\etc\services If you bind to a specific IP address, you can only receive incoming packets over that IP address If you have more than one IP address, bind to hotnl(INADDR_ANY) “host to network long”

BIND BOOL WINAPI WNetGetHostAddress( LPCSTR lpszHost, LPCSTR lpszService, LPCSTR lpszProto, LPSOCKADDR lpAddr) /* Fill in a SOCKADDR using host, protocol, service */ { LPHOSTENT lpHost; LPSERVENT lpServ; SOCKADDR_IN sin; ZeroMemory(&sin, sizeof(sin));

BIND sin.sin_family = PF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); if(lpszHost != NULL) { lpHost = gethostbyname(lpszHost); if(lpHost != NULL) { CopyMemory(&sin.sin_addr, lpHost->h_addr_list[0], lpHost->h_length); } lpServ = getservbyname(lpszService, lpszProto);

BIND if(lpServ != NULL) { sin.sin_port = lpServ->s_port; ZeroMemory(sin.sin_zero, sizeof(sin.sin_zero)); CopyMemory(lpAddr, &sin, sizeof(SOCKADDR)); return TRUE; /* lpAddr is now ready for bind() */ } return FALSE; } The address returned by WNetGetHostAddress() can be passed directly to the bind() function

LISTEN listen() makes server available for client connection – Socket goes from “bound” to “listening” state int listen(SOCKET s, int nQueueSize); nQueueSize indicates the number of connection requests you are willing to have queued at the socket – Up to SOMAXCON (5 for 1.1, “unlimited” in 2.0)

Accept – The call to listen() places the socket into the listening state – The server application calls accept() ● Returning a “connected socket” – accept() blocks until a client request for a connection arrives – accept() return value gives the server a new socket for exchanging data

Accept SOCKET accept( SOCKET s, /* Listening socket */ LPSOCKADDR lpAddr, /* Find client details here */ LPINT lpnAddrLen /* Length of the returned structure */);

Client int connect( SOCKET s, LPSOCKADDR lpName, int nNameLen); lpName points to a SOCKADDR structure designating the server machine name and port address

Data Exchange – Partner stations exchange data using send() and recv() – send() and recv() have identical arguments: int send ( int recv ( SOCKET s, LPSTR lpBuffer, int nBufferLen,int nBufferLen, int nFlags); int nFlags);

Data Exchange – nFlags == MSG_OOB indicates urgency ● OOB for out-of-band – MSG_PEEK can be used to peek at the data without removing it – These are standard Berkeley Sockets calls ● But read() and write() are more common under UNIX – Not atomic or message oriented ● Loop until full message is received ● Or sent, although incomplete send() is rare