Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.

Slides:



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

Socket Programming in C Slides Adapted on Jörn Altmann‘s Slides.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Katz, Stoica F04 EECS 122: Introduction to Computer Networks Sockets Programming Computer Science Division Department of Electrical Engineering and Computer.
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.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
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,
Socket Programming (C/Java)
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.
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.
Assignment 3 A Client/Server Application: Chatroom.
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.
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.
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)
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.
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.
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 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.
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,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
CS 447 Networks and Data Communication Server-Process Organization IP address and SockAddr_In Data Structure Department of Computer Science Southern Illinois.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 K. Salah Application Layer Module K. Salah Network layer duties.
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.
Sockets and Beginning Network Programming
Sockets and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Implementation of UDP and TCP
Transport layer API: Socket Programming
Network Programming CSC- 341
Internet and Intranet Protocols and Applications
EE 122: Sockets Kevin Lai September 11, 2002.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Socket Programming Neil Tang 09/08/2008
Presentation transcript:

Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University

Socket Interface Socket provides an interface for a programmer to write applications that communicate between two hosts across IP network There are different kinds of sockets Internet socket Unix socket Socket types of interest SOCK_STREAM  Maps to TCP in the AF_INET family SOCK_DGRAM  Maps to UDP in the AF_INET family

Client-Server Architecture Client requests service from server Server responds with sending service or error message to client ClientServer request response

Simple Client-Server Example ClientServer request response socket() connect() send() recv() close() socket() bind() listen() accept() recv() send() recv() close() Connection establishment Data response Data request End-of-file notification

Example: Client Programming Create stream socket ( socket() ) Connect to server ( connect() ) While still connected: send message to server (send() ) receive (recv() ) data from server and process it

Initializing Socket Getting the file descriptor int cSock; if ((cSock = socket(AF_INET, SOCK_STREAM,NULL)) < 0) { perror("socket"); printf("Failed to create socket\n"); abort (); } 1.parameter specifies protocol/address family 2.parameter specifies the communication type 3.parameter specifies the protocol

Connecting to Server struct hostent *host = gethostbyname(argv[1]); unsigned int svrAddr = *(unsigned long *) host->h_addr_list[0]; unsigned short svrPort = atoi(argv[2]); struct sockaddr_in sin; memset (& sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = svrAddr; sin.sin_port = htons (svrPort); if (connect(cSock, (struct sockaddr *) & sin, sizeof (sin)) < 0) { fprintf(stderr, "Cannot connect to server\n"); abort(); }

Sending Packets int send_packets(char *buffer, int buffer_len) { sent_bytes = send(cSock, buffer, buffer_len, 0); if (send_bytes < 0) { fprintf(stderr, “cannot send. \n”); } return 0; } Needs socket descriptor, Buffer containing the message, and Length of the message

Receiving Packets int receive_packets(char *buffer, int bytes) { int received = 0; int total = 0; while (bytes != 0) { received = recv(cSock, buffer[total], bytes); if (received == -1) return –1; if (received == 0) return total; bytes = bytes – received; total = total + received; } return total; }

Example: Server Programming create stream socket (socket() ) Bind port to socket (bind() ) Listen for new client (listen() ) user connects (accept() ) data arrives from client (recv() ) data has to be send to client (send() )

Why bind? Server application needs to call bind() to tell operating system (i.e. network layer) which port to listen Client application does not need bind() The server application will get the port number of the client application through the UDP/TCP packet header Port number of server must be known by client application in order to connect to the server How to handle if a port has been used by another application?

bind() struct hostent *host = gethostbyname (argv[1]); unsigned int svrAddr = *(unsigned long *) host->h_addr_list[0]; unsigned short svrPort = atoi (argv[2]); struct sockaddr_in sin; memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = svrAddr; sin.sin_port = htons (svrPort); int svrSock = socket( AF_INET, SOCK_STREAM, 0 ); if (bind(svrSock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { fprintf(stderr, "Cannot bind to network\n"); abort(); } listen(svrSock, 5); int cltSock = accept(svrSock, (struct sockaddr *)&cli_addr, &clilen );

Summary Socket Programming in C PF_INET  SOCK_STREAM  SOCK_DGRAM Client/Server using stream sockets