Client Design. Issues Server Identification Setting up a socket on client side TCP –Reading and writing with a socket –Closing a socket UDP –Reading and.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Remote Procedure Call (RPC)
Socket Programming Application Programming Interface.
Data Communications and Networking (Third Edition)
UDP and Multi-thread Socket Programming
Name and Address Conversions© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Socket Programming.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Tutorial 8 Socket Programming
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
Babak Esfandiari (based on slides by Qusay Mahmoud)
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Assignment 3 A Client/Server Application: Chatroom.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
Elementary Name and Address Conversions
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
CSTP FS01CS423 (cotter)1 Protocols 2 References: RFC’s 791, 793, 768, 826.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
Elementary Name and Address Conversions
CSE 6590 Department of Computer Science & Engineering York University 111/9/ :26 AM.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
Socket Programming Lab 1 1CS Computer Networks.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 25 Acknowledgements: The syllabus and power point presentations are modified versions.
Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
UNIX Sockets Outline UNIX sockets CS 640.
The Socket Interface A Networking Application Program Interface.
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
TCP/IP1 Address Resolution Protocol Internet uses IP address to recognize a computer. But IP address needs to be translated to physical address (NIC).
Client-server communication Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
SOCKET PROGRAMMING Presented By : Divya Sharma.
1 Issues in Client/Server Refs: Chapter 27 Case Studies RFCs.
Name and Address Conversions
Sockets and Beginning Network Programming
Sockets and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
UNIX Sockets Outline Homework #1 posted by end of day
Internet and Intranet Protocols and Applications
Issues in Client/Server Programming
TCP/IP Protocol Suite: Review
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Exceptions and networking
Presentation transcript:

Client Design

Issues Server Identification Setting up a socket on client side TCP –Reading and writing with a socket –Closing a socket UDP –Reading and writing with a socket –Closing a socket

Server Identification What is the machine name of the server? What is the IP address of that machine? What is the port number of the service? What is the protocol number (integer) of the protocol you wish to use?

Identifying the machine name Hard code name into the code –name OR ip address Use command-line for user specification Store the data on disk Use a protocol server DNS

Hard coding the name Typical beginner approach char servername[]=“america3.pcs.cnu.edu” char servername[]=“ ” No flexibility for –moving server –using an alternate server while testing upgrades –multiple versions of the server Using a name does allow for some flexibility as we will see later.

Server name from command-line e.g. serverapp america3.pcs.cnu.edu Window OS should provide a means for user to specify arguments. Can you find it in W95/98? int main(int argc, char argv[][]) //other ways to define {… if (argc>1) cout << “first parameter is “ << argv[1] << endl; }

Storing the name on external storage (disk) No need to change application change configuration Typical means of parameterizing an application Others –Windows ini files registry –Unix system variables

Use a protocol server Local server: …e.g. –Implement a local server at a known port which serves all requests for your network (that’s hard coded …..) –Have THE server also respond to requests for address. Use broadcast IP and the port number of the service assumes ONE per network does not scale past the LAN (IP address) well Nonlocal server –broadcast does not work

DNS Use a well known server name –e.g. ftp.pcs.cnu.edu ftp.cnu.edu Let DNS provide the service Extends beyond the realm of the LAN Easy to modify actual server location and update all clients which bother to check Does not do versions uses gethostbyname()

What is the server IP? Assuming the server machine name is known, use DNS DNS client IS your application. –Using gethostbyname() is accessing code linked into your application –machine must be configured to access a dns server, but usually done be administrator A name can map to a number of IP addresses if multiple interfaces (router)

DNS via c/c++ struct hostent{ char FAR* h_name; // official host name char FAR* FAR* h_aliases; // other aliases short h_addrtype;// address type short h_length; // address length char FAR* FAR* h_addr_list// list of addresses }; #define h_addr h_addr_list[0]; struct hostent *hptr; char *hostname=“america3.pcs.cnu.edu”; if (hptr = gethostbyname ( hostname ) ) { cout h_addr << endl; else cout << “IP address not found” << endl;

Identifying the port number Port number selection is tricky Some port numbers reserved by Internet Some port numbers reserved by OS –You can use these is “root” sets it up You choose a port number >2000 You compete with all other apps on the machine No pecking order after OS numbers

Identifying the port number getservbyname Service is not JUST a port number –port and protocol … recall the defn of a socket –many TCP services also on UDP DEFINED ON YOUR MACHINE –in unix.. /etc/services you must keep services definition accurate like storing on a local disk for server name

getservbyname() struct servent { char FAR* s_name; // official service name char FAR* FAR* s_aliases;// other aliases short s_port; // port for this service char FAR* s_proto; // protocol } struct servent *sptr; if (sptr = getservbyname( “yourservice”, “tcp”) ) { cout s_port <<endl; else cout <<“Service information not available” << endl;

What is the protocol number? Not frequently used, more often hardcoded. Used subsequently in socket() call. Like service, locally defined –in unix in /etc/protocols use getprotobyname()

getprotobyname() struct protoent { char FAR* p_name; // official name char FAR* FAR* p_aliases; // alias list short p_proto; // official number } struct protoent *pptr*; char pname[]=“tcp”; if (pptr = getprotobyname (pname) ) { cout << “Protocol:”<<pname<<“ Number:” p_proto << end; else cout << “No definition of protocol” << endl;

TCP vs UDP issues Writing client side applications with these two protocols are different NOT a simple code change to go from one form to another TCP - connection oriented expects both ends to be defined but “dials” first UDP - connectionless requires two ends too but does not “dial” before sending

TCP client Allocate socket for this protocol Get local IP/port Resolve server IP/port Connecting Reading and writing Closing

Allocating a TCP socket Mostly hardcoded Must define –protocol family –service –protocol (not required if only one protocol in the family provides that service) s = socket(PF_INET, SOCK_STREAM, 0); Protocol Family ServiceOnly one protocol offers this service in TCP/IP

Getting a local IP/port Client side port numbers are not fixed TCP randomly assigns you an available port number. –Good because it avoids conflicts and server does not need it ahead of time due to direction of the connection –returned port is in the updated socket parameter IP address for host is a no-brainer because the machine only has ONE and TCP looks that up for you too when connect ing!

Getting a local IP/port (ctd) For a machine with multiple IPs tricky but not a death sentence if you do it wrong. Issue: try to have data from client-server and server->client travelling same direction –Not completely critical Route 1 Route 2

Writing into a TCP socket ClientServerOS may buffer! write(s,”Hi”..); write(s,”To”..); write(s,”You”..); os buffer HiToYou Reader does not see “write” blocks, only a stream.

Reading from a TCP socket 1. Read and write fixed sized blocks 2. Look for terminating symbols 3. Send SIZE fields at the head of each block Frame formatting strategies Always do reading in loops until the end of the logical frame appears according to the formatting strategy above

Reading TCP sockets in a loop n = recv(socket, bufptr, buflen, 0) while (n!= SOCKET_ERROR && n!=0) { bufptr +=n; buflen -= n; n = recv(s, bufptr, buflen, 0); } Read and write fixed sized blocks Terminating framing techniques are tricky. Fixed size simply require reading ONE byte then using the above strategy.

Closing a socket Need to use partial close. Communication is 2-way. Just because you close in one direction does not imply closing in the reverse direction Use shutdown() to indicate closing in a particular direction. Shutdown(socket, direction); 0 - no more input allowed 1 - no more output allowed 2 - closed in both directions

UDP client Allocate socket for this protocol SOCK_DGRAM for type UDP for protocol Get local IP/port Resolve server IP/port (Specify the server) * Reading and writing * Closing

Connecting with the server There really is NO connecting If you use the connect() call –lets the client socket software know endpoint –NO connection is done, server may not even exist! If connect() used, follow with send() and recv() Else sendto() and recvfrom() –recvfrom() does not tell who from, it learns who from

Connecting with the server (ctd) UDP (continued) recall datagrams are sent and received as blocks –make it as a whole or none –reading is simple but not support for correction recv() also truncates if buffer is not long enough closesocket() to end but not sent to server. shutdown() works too, but nothing sent to server.