Networked Graphics Building Networked Virtual Environments and Networked Games Distributed Objects, Exchange of Information, Socket Programming.

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.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Project 1 Web Client and Server Jan 2006.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
ECE 4110 – Internetwork Programming Client-Server Model.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
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.
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,
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Sockets The building blocks of Internet communications.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
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.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
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.
Networked Graphics Building Networked Virtual Environments and Networked Games Chapter 2: One on one (101)
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
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 PROGRAMMING Presented By : Divya Sharma.
Sockets and Beginning Network Programming
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Transport layer API: Socket Programming
Socket Programming in C
TCP Sockets Programming
Socket Programming.
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Networked Graphics Building Networked Virtual Environments and Networked Games Distributed Objects, Exchange of Information, Socket Programming

An example of moving objects and information sharing BOIDS

Three Forces Acting on a Boid CohesionAlignmentSeparation

PiPi ViVi A P’iP’i V’iV’i At time t At time t+  t One Time Step of a Boid A boid has: Position P Velocity V Acceleration A These 3 descriptors change over time depending on the forces acting on it.

Data Model Simulation Renderer Software Client On a single Client  The client software calculates the positions of its boids based upon a predefined relationship/interactio n between the flock of boids being rendered locally.

Adding a second client with its own flock of boids DISTRIBUTED BOIDS: CONCEPTS

Data-Push Client B Client A Client A shares its flock with Client B B has no flock

Client A Client B Data-Sharing Client A and Client B each share their flocks

Client B Client A Data-Coupled Client A and Client B share their flocks The Boids in the respective flocks are influenced by both local and remote boids

Adding the Network Component DISTRIBUTED BOIDS: IMPLEMENTATION

Data Model Simulation NetworkRenderer Software Modem Client Router Software Modem Client Two Hosts

Boids Implementation Data model Local Flock Remote Flock Screen (Renderer Output) Network Internet Router Simulation The Data Sharing Model

Create socket local port X _socket = DatagramSocket(); Read packet _packet = _socket.read() Write packet to remote address and port _socket.write(_packet) Close _socket Write packet to remote address and port _socket.write(_packet) Read packet _packet = _socket.read() Close _socket Create socket local port X _socket = DatagramSocket(); Create Datagram Packet to receive and send _packet = DatagramPacket(); Create Datagram Packet to receive and send _packet = DatagramPacket(); A UDP Communication Example – Using Sockets

Create socket to listen to requests on local port X _listenSocket = ServerSocket(); Wait for incoming connection requests _connectionSocket = _listenSocket.accept(); Read messages on _connectionSocket Write messages on _connectionSocket Close _connectionSocket Connect to remote address and port _clientSocket = _Socket(); Write messages on _clientSocket Read messages on _clientSocket Close _clientSocket Setup streams _connectionSocket Setup streams _connectionSocket A TCP Communication Example – Using Sockets Receiving (Server) Host Client Host

X Client A t X Client B t+t Network Boid i wall clock time Trajectory of Boid at Client A (local) and Client B (remote)

X wall clock time Client A t X wall clock time Client B t+t Network Boid i t+t Network t Boid j Boid i Two Boids: i (client A) & j (client B)

t X wall clock time Client A t X wall clock time Client B t Network Boid j Boid i t Network Boid j Boid i Information displayed with Network Delay

a) At time t b) At time t+t Network c) At time t+2.t Network ClientAClientB ClientAClientB ClientAClientB How Network Delay creates a lag in Display at the remote client

Using the TCP/IP model Socket Programming

Steps to Socket Programming  Creating a Socket:  The analogy of creating a socket is that of requesting a telephone line from the phone company.  Identifying/Naming a Socket:  The analogy is that of assigning a phone number to the line that you requested from the phone company in step 1 or that of assigning an address to a mailbox.  Connecting from a Client to a Server  Accepting Connections on a Server  Communicate – Exchange Data  Close the Connection

Creating a Socket  A socket, s, is created with the socket system call: int s = socket(domain, type, protocol)  All the parameters as well as the return value are integers:  domain, or address family — communication domain in which the socket should be created. Example of address families are AF_INET (IP), AF_INET6 (IPv6), AF_UNIX (local channel, similar to pipes), AF_ISO (ISO protocols).  type — type of service. This is selected according to the properties required by the application: SOCK_STREAM (virtual circuit service), SOCK_DGRAM (datagram service), SOCK_RAW (direct IP service). Check with your address family to see whether a particular service is available.  protocol — indicates a specific protocol to use for the sockets operation. This is useful in cases where some families may have more than one protocol to support a given type of service. The return value is a file descriptor (a small integer).

Demo Code: Creating a Socket /* demo-01: create a TCP/IP socket usage:demo-01 create a socket. Doesn't do much */ #include /* defines printf */ #include /* defines exit and other sys calls */ #include

Demo Contd. Returns fd: a file descriptor main(int argc, char **argv) { int fd; /* create a tcp/ip socket */ /* request the Internet address protocol */ /* and a reliable 2-way byte stream (TCP/IP) */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket"); return 0; } printf("created socket: descriptor=%d\n", fd); exit(0); }

Identify/Name a Socket  When we talk about naming a socket, we are talking about assigning a transport address to the socket (a port number in IP networking). In sockets, this operation is called binding an address and the bind system call is used for this.

Assigning an Address You can explicitly assign an address or allow the system to assign one. The address is defined in a socket address structure. Applications find addresses of well-known services by looking up their names in a database (e.g., the file /etc/services. E.g., ftp 23). The system call for binding is: int error = bind(s, addr, addrlen) s is the socket descriptor obtained in step 1, addr is the address structure (struct sockaddr *) addrlen is an integer containing the address length

Socket Address (struct sockaddr *) The address family determines what variant of the sockaddr struct * to use that contains elements that make sense for that specific communication type. For IP networking, we use struct sockaddr_in, which is defined in the header netinet/in.h. This structure defines: struct sockaddr_in { __uint8_tsin_len; sa_family_tsin_family; in_port_tsin_port; struct in_addr sin_addr; Charsin_zero[8]; }; Before calling bind, we need to fill out this structure.

The struct sockaddr_in The three key parts we need to set are: sin_family The address family we used when we set up the socket. In our case, it's AF_INET. sin_port The port number (the transport address). You can explicitly assign a transport address (port) or allow the operating system to assign one. If you're a client and won't be receiving incoming connections, you'll usually just let the operating system pick any available port number by specifying port 0. If you're a server, you'll generally pick a specific number since clients will need to know a port number to connect to.

struct sockaddr_in contd. sin_addr The address for this socket. This is just your machine's IP address. With IP, your machine will have one IP address for each network interface. For example, if your machine has both Wi-Fi and Ethernet connections, that machine will have two addresses, one for each interface. Most of the time, we don't care to specify a specific interface and can let the operating system use whatever it wants. The special address for this is , defined by the symbolic constant INADDR_ANY. Since the address structure may differ based on the type of transport used, the third parameter: sin_zero[8] specifies the length of that structure. This is simply sizeof(struct sockaddr_in).

Demo Code: Binding a Socket /* demo-01: create a TCP/IP socket usage:demo-01 create a socket. Bind it to any available port. Then use getsockname to print the port. */ #include /* defines system calls */ #include /* needed for printf */ #include /* needed for memset */ #include #include /* needed for sockaddr_in */

Demo Contd. main(int argc, char **argv) { struct sockaddr_in myaddr;/* our address */ int fd;/* our socket */ unsigned int alen;/* length of address (for getsockname) */ /* create a tcp/ip socket */ /* request the Internet address protocol */ /* and a reliable 2-way byte stream (TCP/IP) */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket"); return 0; }

Demo Contd. printf("created socket: descriptor = %d\n", fd); /* bind to an arbitrary return address */ /* in this case, assume it's the client side, so we won't /* care about the port number as no application will connect here */ /* INADDR_ANY is the IP address and 0 is the socket */ /* htonl converts a long integer (e.g. address) to a network */ /* representation (IP-standard byte ordering) */ memset((void *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(0);

Dem Contd. - Printing if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); return 0; } alen = sizeof(myaddr); if (getsockname(fd, (struct sockaddr *)&myaddr, &alen) < 0) { perror("getsockname failed"); return 0; } printf("bind complete. Port number = %d\n", ntohs(myaddr.sin_port)); exit(0); }

Connecting to a Socket (Client side) For connection-based communication, the client initiates a connection with the connect system call: int error = connect(s, serveraddr, serveraddrlen) Where: s is the socket (type int) serveraddr is a pointer to a structure containing the address of the server (struct sockaddr *) Serveraddrlen is a parameter containing the size of struct sockaddr *. Since the structure may vary with different transports. For connectionless service, the operating system will send datagrams and maintain an association between the socket and the remote address.

Accepting a Connection (on Server) For connection-based communication, the server has to first state its willingness to accept connections. This is done with the listen system call: int error = listen(s, backlog) The backlog is an integer specifying the upper bound on the number of pending connections that should be queued for acceptance. After a listen, the system is listening for connections to that socket. The connections can now be accepted with the accept system call, which extracts the first connection request on the queue of pending connections. It creates a new socket with the same properties as the listening socket and allocates a new file descriptor for it. By default, socket operations are synchronous, or blocking, and accept will block until a connection is present on the queue.

Accepting The syntax of accept is: int s; struct sockaddr *clientaddr; int clientaddrlen = sizeof(struct sockaddr); int snew = accept(s, clientaddr, &clientaddrlen); The clientaddr structure allows a server to obtain the client address. accept returns a new file descriptor snew that is associated with a new socket. The address length field initially contains the size of the address structure and, on return, contains the actual size of the address. Communication takes place on this new socket. The original socket is used for managing a queue of connection requests (you can still listen for other requests on the original socket).

Exchanging Data Data can now be exchanged with the regular file system read and write system calls (referring to the socket descriptor). The send/recv calls are similar to read/write but support an extra flags parameter that lets one peek at incoming data and to send out-of-band data. The sendto/recvfrom system calls are like send/recv but also allow callers to specify or receive addresses of the peer with whom they are communicating (most useful for connectionless sockets). sendmsg/recvmsg support a full IPC interface and allow access rights to be sent and received.

Exchanging Data Contd. Summary: read/write or send/recv calls must be used for connection- oriented communication sendto/recvfrom or sendmsg/recvmsg must be used for connectionless communication. Note that: With stream virtual circuit service (SOCK_STREAM) and with datagram service (SOCK_DGRAM) the other side may have to perform multiple reads to get results from a single write (because of fragmentation of packets) or vice versa (a client may perform two writes and the server may read the data via a single read) depending on how receiving end is processing the data.

Close Connection The system call close (s) will shutdown a socket s. The shutdown system call may be used to stop all further read and write operations on a socket s: shutdown(s); The shutdown system call with “how”. #include int shutdown(int s, int how); Where the second parameter, how, allows us to tell the socket s what part of the full-duplex connection to shut down: A value of SHUT_RD will disallow further receives on that socket. A value of SHUT_WR will disallow further sends on that socket. A value of SHUT_RDWR will disallow both further sends and receives on that socket.

Synchronous and Asynchronous Network communication (or file system access in general) system calls may operate in two modes: synchronous or asynchronous. In the synchronous mode, socket routines return only when the operation is complete. For example, accept returns only when a connection arrives. In the asynchronous mode, socket routines return immediately: system calls become non-blocking calls (e.g., read does not block). You can change the mode with the fcntl system call. For example: fcntl(s, F_SETFF, FNDELAY); sets the socket s to operate in asynchronous mode.