Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.

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.
Socket Programming Application Programming Interface.
CS3516 (B10) HELP Session 2 Presented by Lei Cao.
Advanced Sockets Amit Mondal TA, Intro to Networking Jan 22, 2009 Recital 3 Introduction to Networking Instructor: Prof. Aleksandar Kuzmanovic.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
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.
Introduction to Project 1 Web Client and Server Jan 2006.
IP Multiplexing Ying Zhang EECS 489 W07.
Communication. References r On-line tutorials m Beej’s Guide to Network programming m The Java.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
1 Sockets Programming in Linux References: Internetworking with TCP/IP Vol III - Linux version UNIX Network Programming - W. Richard Stevens.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
1 Programming with TCP/IP by Armin R. Mikler. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software.
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.
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.
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.
Communication. References Chapter 3, Tanenbaum and Van Steen Beej’s Network Programming Guide.
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.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Networking Concepts, Socket Programming Spring 2012.
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Communication. References r The 402 web site has many links to socket tutorials. r Chapter 2.1 of the Tanenbaum, van Steen textbook r Chapter 15, of the.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
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 ,
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.
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,
UNIX Internet Socket API
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
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.
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 Abstraction and Interprocess Communication
Sockets and Beginning Network Programming
Socket programming Péter Verhás August 2002
CS 1652 Jack Lange University of Pittsburgh
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Transport layer API: Socket Programming
Introduction to Network Programming Speaker: Jae Chung
Advanced Sockets Introduction to Networking
Socket Abstraction and Interprocess Communication
TCP Sockets Programming
EE 122: Sockets Kevin Lai September 11, 2002.
Socket Programming.
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Programming Neil Tang 09/08/2008
Presentation transcript:

Socket Programming Lec 2 Rishi Kant

Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using socket() function For datagram - Define the IP and port you wish to connect from, and connect to in 2 separate sockaddr_in structures For stream – Define the IP and port you wish to listen on (server) or connect to (client) in a sockaddr_in structure For datagrams and stream servers, bind the sockaddr_in structure to the socket allocated using the bind() function

Review of Socket programming For servers, use the listen() function to signal the OS to monitor incoming connections and use the accept() function to wait for a client For clients, use the connect() function to initiate the 3-way handshake and set up the stream connection to the server For streams, use the send() and recv() functions to transmit and receive bytes For datagram, use the sendto() and recvfrom() functions to transmit and receive packets

Example from book (pg 33) #include #define PORT 5432 #define MAX_LINE 256 int main (int argc, char **argv) { FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host; char buf[MAX_LINE]; int s; int len; if (argc == 2) { host = argv[1]; } else { fprintf (stderr, "usage: simplex-talk host\n"); exit(1); } hp = gethostbyname (host); if (!hp) { fprintf (stderr, "simplex-talk: unknown host: %s\n", host); exit (1); }

Example from book (pg 33) bzero ( (char *) &sin, sizeof (sin)); sin.sin_family = AF_INET; bcopy (hp->h_addr, (char *) &sin.sin_addr, hp->h_length); sin.sin_port = htons (PORT); if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror ("simple-talk: socket"); exit (1); } if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror ("simplex-talk: connect"); close (s); exit (1); } while (fgets (buf, sizeof(buf), stdin)) { buf[MAX_LINE-1] = '\0'; len = strlen (buf) + 1; send (s, buf, len, 0); }

Example from book (pg 33) #include #define PORT 5432 #define MAX_PENDING 5 #define MAX_LINE 256 int main () { FILE *fp; struct sockaddr_in sin, client; char buf[MAX_LINE]; int s, new_s; int len; bzero ( (char *) &sin, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons (PORT); if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror ("simple-talk: socket"); exit (1); } if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) < 0) { }

Example from book (pg 33) perror ("simplex-talk: bind"); close (s); exit (1); } listen (s, MAX_PENDING); while (1) { if ((new_s = accept (s, (struct sockaddr *) &client, &len)) < 0) { perror ("simplex-talk: accept"); exit (1); } while (len = recv (new_s, buf, sizeof (buf), 0)) { fputs (buf, stdout); } close (new_s); }

How to compile your program gcc –o - lsocket –lnsl e.g. gcc –o project proj.c –lsocket -lnsl

Pitfalls Forgetting to convert from host to network byte order and back [htons, htonl etc] Forgetting to check if a function generated an error by checking return value Forgetting to check the number of bytes transmitted/received by send()/recv() Forgetting to use the addressof (&) operator Forgetting to include the proper header files Forgetting to flush output streams [fflush()] Forgetting to set the initial value of length before passing it to accept() or recvfrom() – problem I faced, but not shown in any of the examples

Polling streams Read operations are blocking calls, so we need a way to check when a stream is ready to be read from Accomplished by using the select() function Very good tutorial: net/html/

Polling streams Include files: #include FD_ZERO(fd_set *set) -- clears a file descriptor set FD_SET(int fd, fd_set *set) -- adds fd to the set FD_CLR(int fd, fd_set *set) -- removes fd from the set FD_ISSET(int fd, fd_set *set) -- tests to see if fd is in the set int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)

Java Sockets Socket and ServerSocket classes for TCP DatagramSocket for UDP Clients use the Socket and servers use ServerSocket Simple example: // Server lines ServerSocket svr = new ServerSocket (2000); Socket s = Svr.accept(); // Client lines Socket s = new Socket (“localhost”, 2000); // Streams s.getInputStream(); s.getOutputStream();

Project (Usage) Usage: There will be exactly 1 program, named chitchat, that can work in either client or server mode. The command line usage will be: chitchat [ | ] If no arguments are specified, a usage message should be displayed. If 1 argument is specified, the program should start up in server mode. If 2 arguments are specified, the program should start in client mode.