Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.

Slides:



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

© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Socket Programming Application Programming Interface.
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.
Jieun Song Port-Binding & Connect-Back Shellcode.
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.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
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.
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)
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed 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.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
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.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
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.
Introduction A Simple Daytime Client A Simple Daytime Server
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,
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.
OS view of networking – Sockets API (an exercise in planning for the future) David E. Culler CS162 – Operating Systems and Systems Programming Lecture.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
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.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Sockets and Beginning Network Programming
Assignment 3 A Client/Server Application: Chatroom
Sockets and Beginning Network Programming
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Socket Programming (Cont.)
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Review: TCP Client-Server Interaction
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
Recitation 11 – 4/29/01 Outline Sockets Interface
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
Advanced Network Programming spring 2007
Network Programming Chapter 12
Chapter 2 Application Layer
Socket Programming(1/2)
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Presentation transcript:

Intro to Socket Programming CS 360

Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is made 4 Usually, server processes are not programmed to exit and are frequently called “daemons”. 4 They “listen” for an incoming connection on a particular PORT number. 4 Ports 0 through 1023 are preassigned and require root privileges to listen to. 4 Ports >1023 are termed ephemeral (mostly not preassigned). 4 Usually, when a connection is made to a server, it forks off a child process which responds to the client while the parent listens for another connection. Clients actively make a connection to a server 4 An Internet address (xxx.xxx.xxx.xxx) and a port number are selected by the client. 4 It then attempts to connect to that host/port number 4 If the connection succeeds, it proceeds to read and write data through its connection. For our purposes, we will only use TCP (no UDP) 4 Reliable, connection-oriented protocol

Page 3 CS 360, WSU Vancouver Server side programming Synopsis:  Make a socket using socket()  Bind the socket to a port number using bind() to give it an “address” on the net  Call listen() to establish our process as a server. –However, listen() does not actually listen  Wait for a completed connection to be established with accept()  When a connection is accepted, use fork() to create a child process 4 The parent closes child’s socket and loops back to accept another connection 4 The child process services the client connection and eventually exits.  At some point the parent should use waitpid() to prevent “zombie” processes

Page 4 CS 360, WSU Vancouver Important header files Generally, you will need to include the following header files:  sys/types.h  sys/socket.h  netinet/in.h  arpa/inet.h  netdb.h An important type that holds the data associated with an Internet address: length AF_INET 16 bit port number 32 bit IP version 4 address Unused struct sockaddr_in { }

Page 5 CS 360, WSU Vancouver Make a socket int listenfd; listenfd = socket(AF_INET, SOCK_STREAM, 0); AF_INET is the domain -> Internet SOCK_STREAM is the protocol family (TCP) If the result is < 0, there is an error, use perror() to print the message

Page 6 CS 360, WSU Vancouver Bind the socket to a port #define MY_PORT_NUMBER struct sockaddr_in servAddr; memset(&servAddr, 0, sizeof(servAdder)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(MY_PORT_NUMBER); servAddr.sin_addr.s_addr = htonl(INADDR_ANY); if ( bind( listenfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { perror(“bind”); exit(1); }

Page 7 CS 360, WSU Vancouver Listen and Accept connections listen( listenfd, 1) Sets up a connection queue one level deep int connectfd; int length = sizeof(struct sockaddr_in); struct sockaddr_in clientAddr; connectfd = accept(listenfd, (struct sockaddr *) &clientAddr, &length); Waits (blocks) until a connection is established by a client When that happens, a new descriptor for the connection is returned If there is an error the result is < 0; The client’s address is written into the sockaddr structure

Page 8 CS 360, WSU Vancouver Getting a text host name Uses DNS to convert a numerical Internet address to a host name Returns NULL if there is an error, use herror() to print the error message, it has the same argument as perror() struct hostent* hostEntry; char* hostName; hostEntry = gethostbyaddr(&(clientAddr.sin_addr), sizeof(struct in_addr), AF_INET); hostName = hostEntry->h_name;

Page 9 CS 360, WSU Vancouver Making a connection from a client Make an Internet socket using TCP protocol Same as server code int socketfd; socketfd = socket( AF_INET, SOCK_STREAM, 0);

Page 10 CS 360, WSU Vancouver Set up the address of the server Plug in the family and port number Get the numeric form of the server address Copy it into the address structure struct sockaddr_in servaddr; struct hostent* hostEntry; struct in_addr **pptr; memset( &servAddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(MY_PORT_NUMBER); hostEntry = gethostbyname(“lx.encs.vancouver.wsu.edu”); /* test for error using herror() */ /* this is magic, unless you want to dig into the man pages */ pptr = (struct in_addr **) hostEntry->h_addr_list; memcpy(&servAddr.sin_addr, *pptr, sizeof(struct in_addr));

Page 11 CS 360, WSU Vancouver Connect to the server A connection is established, after which I/O may be performed on socketfd 4 This is the file I/O equivalent of “open()” Or, there is an error which is indicated by the return being < 0 4 Always check for an error… connect(socketfd, (struct sockaddr *) &servAddr, sizeof(servAddr));

Page 12 CS 360, WSU Vancouver Socket I/O Use Unix read() and write() using the descriptor for the connected socket in the same manner as file I/O. EXCEPT: 4 Read operations on a socket may return less than the requested number of bytes (in the manner of reading from a pipe). 4 This is NOT an indication of the last block of data. 4 Continue reading until the number of bytes read is zero (implying EOF). 4 Just like I/O with pipes except socket descriptors are bi-directional 4 Always test for errors on reads or writes Use use the Unix netstat utility in the shell to observe open ports, established connections, etc.

Page 13 CS 360, WSU Vancouver Lab Assignment Write two programs 4 daytime.c 4 dayserve.c dayserve is a server process that listens on an ephemeral TCP port for one connection. 4 When it receives a connection, it logs the hostname of the client to stdout and writes the current date and time to the client via the socket connection. 4 See library routines time() and ctime() to obtain and format the time of day 4 Suggestion: start by writing some string of your own choosing to the client daytime is a client program that takes one argument on the command line. That argument is the server’s host name or IP address (in dotted decimal). It makes a TCP connection to the indicated host and dayserve’s port number (or indicates the error, if it cannot) and then writes the information received from dayserve to stdout. Use port number (that way you can communicate with each other’s servers) Demonstrate your client and server programs in lab by the end of lab on Friday, April 17 th. Submit the following files by by the end of the day: 4 daytime.c 4 dayserve.c You will need this code and these capabilities for the final project so you would be wise to modularize/encapsulate them for subsequent re-use.