CSTP FS97CS490D (cotter)1 Sockets Programming in UNIX References: Internetworking with TCP/IP Vol III - BSD version UNIX Network Programming - W. Richard.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
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.
Introduction to Project 1 Web Client and Server Jan 2006.
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.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
1 Sockets Programming in Linux References: Internetworking with TCP/IP Vol III - Linux version UNIX Network Programming - W. Richard Stevens.
Tutorial on Socket Programming Data types and structures for writing client- server programs.
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.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
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.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
The Sockets Library and Concepts Rudra Dutta CSC Spring 2007, Section 001.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Example Servers Pt 2 Objective: To discuss key aspects of various server implementations.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
C Programming in Linux Sockets. All Internet Applications use Sockets to Communicate Servers use passive sockets to listen Clients use active sockets.
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
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.
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.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
1 Dimension of Server Designs r Iterative vs Concurrent r Connection-oriented vs. connectionless r Stateful and stateless m Constrained by application.
Client-Server Programming and Applications. References Douglas Comer, David Stevens, “Internetworking with TCP/IP: Client-Server Programming and Applications”,
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 and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Internet and Intranet Protocols and Applications
Socket Programming in C
TCP Sockets Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Chapter 04. TCP Server/Client.
Socket Programming Neil Tang 09/08/2008
Presentation transcript:

CSTP FS97CS490D (cotter)1 Sockets Programming in UNIX References: Internetworking with TCP/IP Vol III - BSD version UNIX Network Programming - W. Richard Stevens

CSTP WS01CS423 (cotter)2 Sockets Programming in UNIX UNIX Socket API functions Example TCP / UDP Clients Example TCP / UDP Servers

CSTP WS01CS423 (cotter)3 Basic Socket Functions Server(UNIX) Create a socket of a particular type –retcode = socket (family, type, protocol ) –s = socket (PF_INET, SOCK_STREAM, 0) –returns socket number or -1 on error Bind that socket to a specific port –retcode = bind (socket, localaddr, addrlen ) –n = bind (s, (struct sockaddr *)myaddr, sizeof(myaddr)) –returns 0 on success, -1 on fail

CSTP WS01CS423 (cotter)4 Basic Socket Functions Server(UNIX) Wait for an incoming message –retcode = listen (socket, queuelen) –ans = listen(s, 0) /* queuelen max ~ 5*/ –return value 0 = success, -1 = fail Create a new socket and return new socket ID to client –retcode = accept (socket, addr, addrlen) –ans = accept (s, (struct sockaddr *)cl_addr, sizeof(cl_addr)) –return value socket number = success, -1 = fail

CSTP WS01CS423 (cotter)5 Basic Socket Functions Server (UNIX) Read / send a message –retcode = read [write] (socket, buff, bufflen) –ans = read (s, buf, sizeof(buf)) –ans = write (s, buf, sizeof(buf)) –return value word count = success, -1 = fail Close the socket –retcode = shutdown (socket, direction) direction: 0 means input, 1 means output, 2 means both –retcode = close (socket ) –return value 0 = success, -1 = fail

CSTP WS01CS423 (cotter)6 Basic Socket Functions Client (UNIX) Create a socket of a particular type –Socket ( ) Establish a connection to a remote Port/Socket –retcode = connect (socket, addr, addrlen) –ans = connect (s, (struct sockaddr *)&svr, sizeof(svr)) –return value 0 = success, -1 = fail Send and receive messages to/from remote socket –Read ( ) / Write ( ) Close the socket –Close ( )

CSTP WS01CS423 (cotter)7 Additional Socket Functions (UNIX) Byte ordering functions –servaddr.sin_port = htons (SERV_PORT) –myaddr.sin_addr.s_addr = htonl (INADDR_ANY) Name resolution functions –host / protocol / service, by name / address / port Other Stuff –zero out memory blocks bzero ((char *)&myaddr, sizeof(myaddr)); –copy memory blocks bcopy (hp->h_addr, (caddr_T)&svaddr.sin_addr, hp->h_length)

CSTP WS01CS423 (cotter)8 Example UNIX Client Develop a set of procedures that can be used by other programs to implement client / server. int connectTCP (host, service) int connectsock(host, service, “tcp”) [identify service, host, port] [get a socket] [connect to service / host / port] [return socket number]

CSTP WS01CS423 (cotter)9 Example UNIX Client connectTCP.c intconnectTCP (char *host, char *service) { return connectsock (host, service, “tcp”); }

CSTP WS01CS423 (cotter)10 Example UNIX Client connectsock.c #include,,, int connectsock (char *host, char *service, char *protocol) { struct hostent*phe; struct servent*pse; struct protoent*ppe; struct sockaddr_in sin; int s, type;

CSTP WS01CS423 (cotter)11 Example UNIX Client connectsock.c bzero ((char *)&sin, sizeof (sin)); sin.sin_family = AF_INET; if (pse = getservbyname (service, protocol) ) sin.sin_port = pse ->s_port; else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0) error_exit (“can’t get %s service\n”, service); if (phe = gethostbyname (host) ) bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length); else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE ) error_exit (“can’t get %s host\n”, host);

CSTP WS01CS423 (cotter)12 Example UNIX Client connectsock.c if ( (ppe = getprotobyname (protocol) == 0) error_exit (“can’t get %s host\n”, host); if (strcmp (protocol, “tcp”) == 0) type = SOCK_STREAM; else type = SOCK_DGRAM; if (s = socket (PF_INET, type, ppe->p_proto)) < 0) error_exit (“Can’t create a socket\n”); if (connect (s, (struct sockaddr *)&sin, sizeof(sin)) < 0) error_exit (“can’t connect to remote socket\n”); return s; }

CSTP WS01CS423 (cotter)13 Example UNIX Client TCPecho.c #include #define LINELEN128 int main (argc, argv) { host = argv[1]; service = argv[2]; TCPecho (host, service); exit (0); }

CSTP WS01CS423 (cotter)14 Example UNIX Client TCPecho.c int TCPecho (char *host, char *service) { charbuf[LINELEN+1]; ints, n, outchars, inchars; s = connectTCP (host, service); while (fgets (buf, sizeof(buf), stdin)) { buf[LINELEN] = ‘\0’; outchars = strlen(buf); (void) write (s, buf, outchars);

CSTP WS01CS423 (cotter)15 Example UNIX Client TCPecho.c for (inchars = 0; inchars < outchars; inchars +=n) { n = read (s, &buf[inchars], outchars - inchars); if (n < 0) error_exit(“socket read failed\n”); } fputs (buf, stdout); }

CSTP WS01CS423 (cotter)16 TCP Client Algorithm Client / Server Communications –request / response interaction –write / read (send / recv) Single write may require multiple reads –response may be segmented –continue appending reads until return length = 0

CSTP WS01CS423 (cotter)17 Example UNIX Client UDPecho.c #include #define LINELEN128 int main (argc, argv) { host = argv[1]; service = argv[2]; UDPecho (host, service); exit (0); }

CSTP WS01CS423 (cotter)18 Example UNIX Client UDPecho.c int UDPecho (char *host, char *service) { charbuf[LINELEN+1]; ints, n, outchars, inchars; s = connectUDP (host, service); while (fgets (buf, sizeof(buf), stdin)) { buf[LINELEN] = ‘\0’; outchars = strlen(buf); (void) write (s, buf, outchars);

CSTP WS01CS423 (cotter)19 Example UNIX Client UDPecho.c if (read (s, buf, nchars) < 0) error_exit (“Socket read failed \n”); fputs (buf, stdout); }

CSTP WS01CS423 (cotter)20 Iterative Connectionless Servers TIME Server /* UDPtimed.c - main */ #include extern interrno; intpassiveUDP(const char *service); interrexit(const char *format,...); #define UNIXEPOCH /* UNIX epoch, in UCT secs*/

CSTP WS01CS423 (cotter)21 Iterative Connectionless Servers TIME Server int main(int argc, char *argv[]) { struct sockaddr_in fsin;/* the from address of a client */ char*service = "time";/* service name or port number */ charbuf[1];/* "input" buffer; any size > 0 */ intsock;/* server socket*/ time_tnow;/* current time*/ intalen;/* from-address length*/ switch (argc) { case1:break; case2:service = argv[1]; break; default:errexit("usage: UDPtimed [port]\n"); }

CSTP WS01CS423 (cotter)22 Iterative Connectionless Servers TIME Server sock = passiveUDP(service); while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); (void) time(&now); now = htonl((u_long)(now + UNIXEPOCH)); (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin)); }

CSTP WS01CS423 (cotter)23 Concurrent Connection-Oriented TCPechod.c #include,,,,,,,,,,, #define QLEN 5/* maximum connection queue length */ #defineBUFSIZE4096 extern interrno; voidreaper(int); intTCPechod(int fd); interrexit(const char *format,...); intpassiveTCP(const char *service, int qlen);

CSTP WS01CS423 (cotter)24 Concurrent Connection-Oriented TCPechod.c int main(int argc, char *argv[ ]) { char*service = "echo";/* service name or port number*/ struct sockaddr_in fsin;/* the address of a client*/ intalen;/* length of client's address*/ intmsock;/* master server socket*/ intssock;/* slave server socket*/ switch (argc) { case1:break; case2:service = argv[1]; break; default:errexit("usage: TCPechod [port]\n"); }

CSTP WS01CS423 (cotter)25 Concurrent Connection-Oriented TCPechod.c msock = passiveTCP(service, QLEN); (void) signal(SIGCHLD, reaper); while (1) { alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock < 0) { if (errno == EINTR) continue; errexit("accept: %s\n", strerror(errno)); }

CSTP WS01CS423 (cotter)26 Concurrent Connection-Oriented TCPechod.c switch (fork()) { case 0:/* child */ (void) close(msock); exit(TCPechod(ssock)); default:/* parent */ (void) close(ssock); break; case -1: errexit("fork: %s\n", strerror(errno)); }

CSTP WS01CS423 (cotter)27 Concurrent Connection-Oriented TCPechod.c intTCPechod(int fd) { charbuf[BUFSIZ]; intcc; while (cc = read(fd, buf, sizeof buf)) { if (cc < 0) errexit("echo read: %s\n", strerror(errno)); if (write(fd, buf, cc) < 0) errexit("echo write: %s\n", strerror(errno)); } return 0; }

CSTP WS01CS423 (cotter)28 Concurrent Connection-Oriented TCPechod.c /* * reaper - clean up zombie children * */ /*ARGSUSED*/ void reaper(int sig) { intstatus; while (wait3(&status, WNOHANG, (struct rusage *)0) >= 0) /* empty */; }