Sockets A popular API for client-server interaction.

Slides:



Advertisements
Similar presentations
CS 4700 / CS 5700 Network Fundamentals
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A TCP Connection is based on 6 crucial.
Socket Programming Application Programming Interface.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
1 Generic Transport Service Primitives Listen –notify Transport layer a call is expected Connect –establish Transport layer connection Send (or Write)
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.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A ‘reliable’ (but not secure!) protocol.
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.
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Final presentation- Part B Avi Urman, Kobi Maltinsky Supervisor: Inna Rivkin Linux on SOPC – LFSR Decryption on Linux Based System.
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.
Sun RPC also called ONC (Open Network Computing) RPC originally designed for client-server communication for Sun Network File System (NFS) provides an.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
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.
Distributed Computing A Programmer’s Perspective.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
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 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.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
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.
Socket Program Training 10/29/ TCP Client Socket ( ) Connect ( ) send ( ) Close ( ) send ( ) Read ( ) Accept ( ) recv ( ) Listen ( ) Bind ( ) Socket.
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.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Sockets A popular API for client-server interaction.
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.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Sockets and Beginning Network Programming
Assignment 3 A Client/Server Application: Chatroom
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Introduction to Information Security
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Chapter 3: Windows7 Part 4.
Transport layer API: Socket Programming
Internet and Intranet Protocols and Applications
30.
Socket Programming.
Socket Programming(1/2)
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Sockets A popular API for client-server interaction

Complete view of a socket Application Presentation Session Transport Network Data Link Physical Application Presentation Session Transport Network Data Link Physical Applications talk to each other via APIs like sockets. Sockets provide the interface to the other layers.

Programmer View of a socket Application socket The networking aspects of a socket are transparent Similar to writing to and reading from a file /7055 IP PORT /1234 IP PORT server client

Socket API Three popular variations –berkley - older outdated unix API –TLI - newer unix version –winsock - microsoft All very similar Most examples in c but available in other languages

General Approach SERVER CLIENT Sets up a connection to wait for client (port) Asks for connection to server port Responds & sets up a different connection for the dialog Client asks for service Server responds time

SERVER CLIENT Sets up a connection to wait for client (port) Asks for connection to server port Responds & sets up a different connection for the dialog Client asks for service Server responds time /* get an internet domain socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* bind the socket to the port number */ if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) { /* show that we are willing to listen */ if (listen(sd, 5) == -1) { /* grab an Internet domain socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* connect to PORT on HOST */ if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) { /* wait for a client to talk to us */ if ((sd_current = accept(sd, (struct sockaddr *) &pin, &addrlen)) == -1) { /* send a message to the server PORT on machine HOST */ if (send(sd, argv[1], strlen(argv[1]), 0) == -1) { /* wait for a message to come back from the server */ if (recv(sd, dir, DIRSIZE, 0) == -1) { /* get a message from the client */ if (recv(sd_current, dir, sizeof(dir), 0) == -1) { /* get the directory contents */ read_dir(dir); /* acknowledge the message, reply w/ the file names */ if (send(sd_current, dir, strlen(dir), 0) == -1) {

#define PORT 0x1234 #define DIRSIZE 8192 main() { char dir[DIRSIZE]; int sd, sd_current, cc, fromlen, tolen; int addrlen; struct sockaddr_in sin; struct sockaddr_in pin; /* get an internet domain socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } /* complete the socket structure */ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(PORT); /* bind the socket to the port number */ if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) { perror("bind"); exit(1); } /* show that we are willing to listen */ if (listen(sd, 5) == -1) { perror("listen"); exit(1); } /* wait for a client to talk to us */ if ((sd_current = accept(sd, (struct sockaddr *) &pin, &addrlen)) == -1) { perror("accept"); exit(1); } /* get a message from the client */ if (recv(sd_current, dir, sizeof(dir), 0) == -1) { perror("recv"); exit(1); } /* get the directory contents */ read_dir(dir); /* acknowledge the message, reply w/ the file names */ if (send(sd_current, dir, strlen(dir), 0) == -1) { perror("send"); exit(1); } /* close up both sockets */ close(sd_current); close(sd); /* give client a chance to properly shutdown */ sleep(1); } SERVER

CLIENT #define PORT 0x1234 /* REPLACE with your server machine name*/ #define HOST "america3" #define DIRSIZE 8192 main(argc, argv) int argc; char **argv; { char hostname[100]; char dir[DIRSIZE]; intsd; struct sockaddr_in sin; struct sockaddr_in pin; struct hostent *hp; strcpy(hostname,HOST); if (argc>2) { strcpy(hostname,argv[2]); } /* go find out about the desired host machine */ if ((hp = gethostbyname(hostname)) == 0) { perror("gethostbyname"); exit(1); } /* fill in the socket structure with host information */ memset(&pin, 0, sizeof(pin)); pin.sin_family = AF_INET; pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; pin.sin_port = htons(PORT); /* grab an Internet domain socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } /* connect to PORT on HOST */ if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) { perror("connect"); exit(1); } /* send a message to the server PORT on machine HOST */ if (send(sd, argv[1], strlen(argv[1]), 0) == -1) { perror("send"); exit(1); } /* wait for a message to come back from the server */ if (recv(sd, dir, DIRSIZE, 0) == -1) { perror("recv"); exit(1); } /* spew-out the results and bail out of here! */ printf("%s\n", dir); close(sd); }

Disconnect between Sending and Receiving CLIENTSERVER char one[]=“one”; char three[]=“3”; if (send(sd, one, 3, 0) == -1) {.. if (send(sd, three, 1, 0) == -1) {.. one 3 OS 3one (3eno) 3one Char recvbuf[100] if (recv(sd_current, recvbuf, 100, 0)... 3one A question of how the two synchronize their respective activities!

Not what you expect! CLIENTSERVER char one[]=“one”; char three[]=“3”; if (send(sd, one, 3, 0) == -1) {.. if (send(sd, three, 1, 0) == -1) {.. if (send(sd, one, 3, 0) == -1) {.. if (send(sd, three, 1, 0) == -1) {.. Char recvbuf[100] if (recv(sd_current, recvbuf, 100, 0) … if (recv(sd_current, recvbuf, 100, 0)... NOT a recv for every send! (but not as bad as you think)

Something like this CLIENTSERVER char one[]=“one#”; char three[]=“3#”; if (send(sd, one, 4, 0) == -1) {.. if (send(sd, three, 2, 0) == -1) {.. if (send(sd, one, 4, 0) == -1) {.. if (send(sd, three, 2, 0) == -1) {.. Char recvbuf[100] bool notdone=true; while (notdone) { if (recv(sd_current, recvbuf, 100, 0) … // iterate char by char if (recvbuf[i] == “#”) notdone = false; // only read when you exhaust all // of the recvbuff ……... Actually like if (i==bufflen-1) if (recv…. else i++;

Framing! Message1 Message2 Message3 Should be sent as Message1# Message2# Message3# And will be received as Message1#Message2#Message3# Sometimes the application is less demanding! When the input is predictable in both size and nature of data (like 230 programs)

C issues (Main) routine / parameter declaration –main(argc, argv) –int argc; char **argv; –{ Parameters by reference –int addone(a) – int *a; – {…} –called by x=7; addone (&x) I/O –scanf (“%d %d”,%x,%y) –printf (“Value of x is %d and y is %d”,&x,&y);

Adapting the example to your problem Setup and disconnect is very similar Interaction is not as simple as the example read/write are basically executed in your main loop.

Using C++ No standard library A library available online from Rob Tougher.. Linux Gazette See – sockets.html

How about java? Good support in java Will not be covered (at least now) You’re encouraged to investigate on your own as CS611 will use java extensively

Fundamental difference with your programming experience Your programs have predictable input output sequences. Typically defined by order of data in a file or which button was clicked. Can NOT control order in which peer queries or responds. Requires an event-driven paradigm

Event-driven apps While (NOT A TERMINATE EVENT) get next event if a reasonable event occurred process it else error or terminate Consider deadlocks: A waits for B to send a message X B waits for A to send message Y neither does -> deadlock! Like windows programs! Will address this more later when we cover protocols.

Problems with socket apps Timing problems are new for you Apps typically will deadlock Since input is less predictable, harder to debug. Will need to send/get info through socket and echo to a standard output device whatever is sent/read to aid in debugging

See socket code examples Check out the lab activity on the Course Resources part of the web page

Non-socket APIs Other than the three types of sockets, one will typically find RPC in use. RPC stands for Remote Procedure Call RPC effectively extracts a function from your code and runs it on another machine X = sqrt(y) Calc sqrt(y) return results y x

Additional considerations Setting up and launching a server on your OS How many simultaneous servers can run How to handle queues of clients waiting for service These and many others are addressed in CS611