CS3516 (B10) HELP Session 2 Presented by Lei Cao.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming Application Programming Interface.
CS3516 B10 Help Session 1 CS3516 — TCP/IP Socket Programming Presented by Can (John) Tatar.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
CS4514 B05 HELP Session 1 CS4514 – TCP/IP Socket Programming Presented by Feng Li
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.
Ken French HELP Session 1 CS4514. CS4514 We expect that you have had a programming course similar to 2005 before coming into this class. Programs will.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
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.
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.
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.
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.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
Remote Shell CS230 Project #4 Assigned : Due date :
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
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.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
Introduction to Socket
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.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 2 Applications and Layered Architectures Sockets.
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,
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
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.
UNIX Sockets Outline UNIX sockets CS 640.
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.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Assignment 3 A Client/Server Application: Chatroom
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Interacting With Protocol Software
Transport layer API: Socket Programming
Introduction to Network Programming Speaker: Jae Chung
UNIX Sockets Outline Homework #1 posted by end of day
Berkeley API Socket Programming
Berkeley API Socket Programming
Introduction to Network Programming (v1.2) Speaker: Mingzhe Li
CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.4)
Introduction to Network Programming (v1.3) Speaker: Frank Posluszny
CS3516 — TCP/IP Socket Programming
Socket Programming(1/2)
Socket Programming Neil Tang 09/08/2008
Berkeley API Socket Programming
Sockets.
Presentation transcript:

CS3516 (B10) HELP Session 2 Presented by Lei Cao

12/6/2010 WPI Outline Project 4 Overview Unix Network Programming –UDP/TCP Client –Server Communication with the net oracle Additional suggestions / tips Help Session 2-2

12/6/2010 WPI Help Session 2-3 CS3516 Project4 Your programs should compile and work on ccc.wpi.edu computers, which are running Linux. Net oracle(named oracle) is running on cccWORK4.wpi.edu only. Programs should be written in C or C++ If your program is developed on another platform or machine, you should test the software on ccc before turning in the assignment. Make sure you have the correct #include files in your program.

12/6/2010 WPI What is the Net Oracle? Net Oracle is a name server that maps service names into their transport level address. Net Oracle allows you to register the service you have developed. You register services by sending a message to the oracle containing the server name together with the transport address Given the service name, the client can request the service address or remove the service you have registered. Help Session 2-4

12/6/2010 WPI Project 4 Steps Client: 1.Wait on user’s commands. 2.List all services registered on net oracle(using list command). 1.Connect to service using the transport address returned from net oracle( by connect service [uid] command). 2.Terminate connection with the server when it receives an end-of-file. 3.Terminate the client program(by quit command) Server: 1.Register services with the net oracle. 2.Wait for connections and provide name service to the clients. Help Session 2-5

12/6/2010 WPI Project 4 Communication Model Oracle Name Server (1) register (2) list (3) Connect One Service TCP UDP ClientServer Help Session 2-6

12/6/2010 WPI UDP Transmission (Client) –Connectionless Specify transport address every time you send/recv data –Unreliable Protocol Data lost, bit errors socket() sendto() recvfrom() close() Help Session 2-7

12/6/2010 WPI Example: UDP Client struct hostent *hp; /* /usr/include/netdb.h */ struct sockaddr_in server; /* /usr/include/netinet/in.h */ int sd, lserver = sizeof( server ); /* prepare a socket */ if ( (sd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) { perror( strerror(errno) ); exit(-1); } Help Session 2-8

12/6/2010 WPI /* prepare server address */ bzero( (char*)&server, sizeof(server) ); server.sin_family = AF_INET; server.sin_port = htons( SERVER_PORT ); //endian convert if ( (hp = gethostbyname(SERVER_NAME)) == NULL) { perror( strerror(errno) ); exit(-1); } bcopy( hp->h_addr, (char*)&server.sin_addr, hp->h_length); Example: UDP Client (Continued) Help Session 2-9

12/6/2010 WPI Example: UDP Client (Continued) /* prepare your message */ … /* send/receive data */ sendto( sd, sBuf, data_size, 0, (struct sockaddr*)&server, lserver ); recvfrom( sd, rBuf, MAXLEN, 0, (struct sockaddr*)&server, &lserver ); /* close socket */ close( sd ); Help Session 2-10

12/6/2010 WPI connect() read() / write() send() / recv() close() socket() TCP Connection (Client) –Connection Oriented Specify transport address once at connection –Use File Operations read() / write() –Reliable Protocol Help Session 2-11

12/6/2010 WPI Example: TCP Client int sd; struct hostent *hp; /* /usr/include/netdb.h */ struct sockaddr_in server; /* /usr/include/netinet/in.h */ /* prepare a socket */ if ( (sd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) { perror( strerror(errno) ); exit(-1); } Help Session 2-12

12/6/2010 WPI /* prepare server address */ bzero( (char*)&server, sizeof(server) ); server.sin_family = AF_INET; server.sin_port = htons( SERVER_PORT ); if ( (hp = gethostbyname(SERVER_NAME)) == NULL) { perror( strerror(errno) ); exit(-1); } bcopy( hp->h_addr, (char*)&server.sin_addr, hp->h_length); Example: TCP Client (Continued) Help Session 2-13

12/6/2010 WPI Example: TCP Client (Continued) /* connect to the server */ if (connect( sd, (struct sockaddr*) &server, sizeof(server) ) < 0 ) { perror( strerror(errno) ); exit(-1); } /* send/receive data */ while (1) { read/write(); } /* close socket */ close( sd ); Help Session 2-14

12/6/2010 WPI bind() close() socket() TCP Connection (Server) –Bind transport address to socket –Listen to the socket –Accept connection on a new socket listen() accept() read()/write() Help Session 2-15

12/6/2010 WPI Example: TCP Server int sd, nsd; struct sockaddr_in server; /* /usr/include/netinet/in.h */ sd = socket( AF_INET, SOCK_STREAM, 0 ); bzero( (char*)&server, sizeof(server) ); server.sin_family = AF_INET; server.sin_port = htons( YOUR_SERVER_PORT ); server.sin_addr.s_addr = htonl( INADDR_ANY ); Help Session 2-16

12/6/2010 WPI Example: TCP Server (Continued) bind( sd, (struct sockaddr*) &server, sizeof(server) ); listen( sd, backlog ); while (1) { nsd = accept( sd, (struct sockaddr *) &client, sizeof(client) ); read()/write(); close( nsd ); } close( sd ); Help Session 2-17

Your Server is also a Client Server has to register service with the net oracle via UDP. 12/6/2010 WPI Help Session 2-18

12/6/2010 WPI Oracle Commands enum cmd { cmdErr, /* An error occurred. See sbDesc for details */ cmdGet, /* Get the address of a service */ cmdAckGet, /* ACK for cmdGet message */ cmdEnd, /* Last response to a cmdGet message */ cmdPut, /* Register a new service */ cmdAckPut, /* ACK for cmdPut message */ cmdClr, /* Unregister a service */ cmdAckClr /* ACK for cmdClr message */ }; Help Session 2-19

12/6/2010 WPI Oracle Commands (Request) Find a service: serv.ver = verCur; serv.cmd = cmdGet; serv.uid = ?; serv.sbServ = ?; Register a service: serv.ver = verCur; serv.cmd = cmdPut; serv.uid = ?; serv.sbServ = ?; serv.sbDesc = ?; serv.sa(sockaddr_in) = ? Serv.ti = ? Clear a service: serv.ver = verCur; serv.cmd = cmdClr; serv.uid = ?; serv.sbServ = ?; Help Session 1-20

12/6/2010 WPI Oracle Command (Response) The same structure as with Request Clear: serv.cmd = cmdAckClr; Register: serv.cmd = cmdAckPut; Get : serv.cmd = cmdAckGet; Return two or more messages. The last one: serv.cmd = cmdEnd; Help Session 2-21

12/6/2010 WPI Oracle Communication Example int sd; struct sockaddr_in sa; // you can use gethostbyname() and // getservbyname() to get sa in your project. struct om sendMsg, recvMsg; size_t lom = sizeof(struct om); sendMsg.ver = verCur; sendMsg.cmd = cmdGet; sendto( sd, (void *)&sendMsg, lom, 0, (struct sockaddr *) &sa, lsa ); recvfrom( sd, (void *)&recvMsg, lom, 0, (struct sockaddr *) &sa, &lsa ); // you can also use connect()/send()/recv() for UDP connection, for more // information -- use “man connect”, “man send” and “man recv” Help Session 2-22

12/6/2010 WPI Some Useful System Calls Gethostbyname: map hostname to IP addr struct hostent *gethostbyname( char *name ) Getservbyname: look up service name given struct servent *getservbyname( const char *servname, const char *protocol ) Gethostname: get own hostname int gethostname( char *name, size_t len ) Getsockname: map sd to socket addr int getsockname( int sd, struct sockaddr *sa, size_t *lsa ) Help Session 2-23

12/6/2010 WPI Getservbyname() Get the address of the service The services should be registered in./etc/services The registered name of net oracle is “oracle” struct servent *getservbyname( const char *servname, const char *protocol ) UDP Help Session 2-24

12/6/2010 WPI Select() system call Your client needs to take input from both the network and stdin to close TCP connection or to support your interactive service. How to tell when to do which one? Use the select() function! Notes: #include man select Help Session 2-25