EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.

Slides:



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

Socket Programming CS3320 Fall 2010.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Principles of app layer protocols –clients and servers –app requirements.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Principles of app layer protocols –clients and servers –app requirements.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
CS Computer Networks I Georgia Tech CS Computer Networks 1: Sockets Programming Adapted from slides by Professor Patrick Traynor.
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.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
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.
ECE 4110 – Internetwork Programming Client-Server Model.
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.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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 XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
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.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
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.
CS 1652 Jack Lange University of Pittsburgh
Chapter4 Elementary TCP Socket
Socket Programming in C
Review: TCP Client-Server Interaction
Transport layer API: Socket Programming
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming.
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Today’s topic: Basic TCP API
Presentation transcript:

EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1

2 If you’re not… Burke Fetscher, Jedidiah McClurg, Beibei Lin, Xitao Wen, Gopi Vajravelu, Taiyo Sogawa, Galiya Ibrgimova, Andrew Lee, Cary Lee, Brad Weinberger, Jonathan Chan, Daniel Lieberman, Xin Zhao, or Weixian Wen r to:

3 If you have not… Joined the newsgroup, r Use your favorite client (thunderbird, outlook, etc. ) r Add a newsgroup account r Server name: news.cs.northwestern.edu r Select newsgroup: cs.340

4 If you have not… Turned in homework1, ……

5 Roadmap r How to do socket programming? r How to use hints from project 1?

6 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets

7 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets

8 source application transport network link physical HtHt HnHn M segment HtHt datagram destination application transport network link physical HtHt HnHn HlHl M HtHt HnHn M HtHt M M network link physical link physical HtHt HnHn HlHl M HtHt HnHn M HtHt HnHn M HtHt HnHn HlHl M router switch message M HtHt M HnHn frame

9 source application destination application sockets Something (that I con’t care) in between

10 Socket programming Client side: r Create client-local TCP socket r specify IP address, port number of server process r Establish connection to server Server side (When contacted by client): r Accept the connection (automatically create a new socket) r Communicate with client through the new socket (different than the socket that it’s listening on) Server always listens (waits)  To some socket (port) that welcomes client’s contact  Usually 80, 8000, 8080 for HTTP server It’s always a client that initiates contact  Contact the socket (port) that the server is listening on

Server – high level view Create a socket Bind the socket with port Listen for connections Accept new client connections Read/write to client connections Shutdown connection Corresponding functions (parameters omitted): socket(SOCK_STREAM) bind() listen() //block accept() recv(), send() close()

Client – high level view Create a socket Connect to server Send HTTP request Receive HTTP response Shutdown connection Corresponding functions (parameters omitted): socket(SOCK_STREAM) connect() send() recv() close()

int connect_ socket( char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname( hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons( port); sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); return -1; } return sock; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Hostent structure struct hostent{ char * h_name/*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length/* length of address */ char ** h_addr_list/*prt to array of ptrs with \ IPv4 or IPv6 address*/ } Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr/* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Make the socket Socket(int family, int type, int protocol); return nonnegative value for OK, -1 for error A piece of real code (client or server?)

int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return -1; return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/ Another piece of real code

socket() bind() listen() accept() read() write() read() close() Socket() connect() write() read() close() TCP Client TCP Server Well-known port blocks until connection from client process request Connection establishment Data(request) Data(reply) End-of-file notification Client-server interaction

Standard socket vs Minet Standard function callMinet function call socket()minet_socket() bind()minet_bind() listen()minet_listen() accept()minet_accept() recv()minet_read() send()minet_write() close()minet_close() connect()minet_connect() select()minet_select() “man” every function to get help on how to use it exactly!

Dealing with blocking calls r Many functions block  accept(), connect(), recvfrom() r Consider the following case:  The server accepts a client, and blocks on receiving the HTTP request  Another client tries to initiate a connection

How to handle multiple connections r Create multi-process or multi-threaded code  Not covered r I/O multiplexing using polling  Not covered r I/O multiplexing using select ()

I/O Multiplexing: Select (1) r select()  Wait on multiple file descriptors/sockets and timeout  Return when any file descriptor is ready to be read or written, or Indicate an error, or timeout exceeded r How to solve the blocking problem?  select() among the listening socket and all the opened connection

I/O Multiplexing: Select (2) r int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); r Use FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO() to manipulate the file descriptor lists r “man select” and get all the help that is needed

21 Roadmap r How to do socket programming? r How to use hints from project 1?

Read comments in the file r In client.cc, from line 53 /* create socket */ // Do DNS lookup /* Hint: use gethostbyname() */ /* set address */ /* connect socket */ /* send request */ 22

See the behavior of correct implementations r /home/hga202/EECS340/netclass- execs/http_client r /home/hga202/EECS340/netclass- execs/http_server1 r /home/hga202/EECS340/netclass- execs/http_server2 r /home/hga202/EECS340/netclass- execs/http_server3 23