Socket Programming Client/Server.

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.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
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.
Socket Programming.
1 Java Networking – Part I CS , Spring 2008/9.
Tutorial 8 Socket Programming
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE 4110 – Internetwork Programming Client-Server Model.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
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.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
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.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Remote Shell CS230 Project #4 Assigned : Due date :
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
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.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
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.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming Ameera Almasoud
Assignment 3 A Client/Server Application: Chatroom
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
MCA – 405 Elective –I (A) Java Programming & Technology
Chapter4 Elementary TCP Socket
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Tutorial on Socket Programming
CSI 4118 – UNIVERSITY OF OTTAWA
Transport layer API: Socket Programming
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Socket Programming(1/2)
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Socket Programming Client/Server

Network Socket A network socket is an endpoint of an inter-process communication across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. A socket API is an application programming interface (API), usually provided by the operating system, that allows application programs to control and use network sockets. Internet socket APIs are usually based on the Berkeley sockets standard. A socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread.

Characterized An Internet socket is characterized by at least the following : Local socket address: Local IP address and port number Protocol: A transport protocol (e.g., TCP, UDP, raw IP, or others). consequently, TCP port 53 and UDP port 53 are different, distinct sockets.

Socket Types Datagram sockets, also known as connectionless sockets, which use User Datagram Protocol (UDP). Stream sockets, also known as connection-oriented sockets, which use Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP). Raw sockets (or Raw IP sockets), typically available in routers and other network equipment. Here the transport layer is bypassed, and the packet headers are made accessible to the application.

Some famous port numbers Port Service Transport Protocol 21 FTP tcp,udp,sctp 22 SSH tcp,udp,sctp 23 Telnet tcp,udp 80 HTTP tcp,udp 443 HTTPS tcp,udp,sctp

Socket pairs Communicating local and remote sockets are called socket pairs. Each socket pair is described by a unique 4-tuple consisting of Source IP address Source Port number Destination IP address Destination port number

Linux system calls for Sockets bind() connect() listen() accept() read() write() close()

Server Side The steps involved in establishing a socket on the server side are as follows: 1.Create a socket with the socket() system call 2.Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the server machine. 3.Listen for connections with the listen() system call 4.Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. 5.Send and receive data

Client side The steps involved in establishing a socket on the client side are as follows: 1. Create a socket with the socket() system call 2. Connect the socket to the address of the server using the connect() system call 3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

Client-Server

Internal states of proesses

Accept is woken up

socket() socket() creates an endpoint for communication and returns a file descriptor for the socket. socket() takes three arguments: AF_INET for network protocol IPv4 or AF_INET6 for IPv6. AF_UNIX for local socket (using a file).

bind() bind() assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connections to other hosts. bind() takes three arguments: sockfd, a descriptor representing the socket to perform the bind on. my_addr, a pointer to a sockaddr structure representing the address to bind to. addrlen, a socklen_t field specifying the size of the sockaddr structure.

listen() listen() After a socket has been associated with an address, listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAM, SOCK_SEQPACKET). listen() requires two arguments: sockfd, a valid socket descriptor. backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value.

Accept() When an application is listening for stream-oriented connections from other hosts, it is notified of such events ( select() function) and must initialize the connection using the accept() function. The accept() function creates a new socket for each connection and removes the connection from the listen queue. It takes the following arguments: sockfd, the descriptor of the listening socket that has the connection queued. cliaddr, a pointer to a sockaddr structure to receive the client's address information. addrlen, a pointer to a socklen_t location that specifies the size of the client address structure passed to accept(). When accept() returns, this location indicates how many bytes of the structure were actually used.

Create a socket A socket, s, is created with the socket system call: #include <sys/socket.h> int s = socket(domain, type, protocol); listenfd = socket(AF_INET, SOCK_STREAM, 0);

Bind #include <sys/socket.h> int bind(int socket, const struct sockaddr *address, socklen_t address_len); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

Connect to a server from a client #include <sys/types.h> #include <sys/socket.h> int connect(int socket, const struct sockaddr *address, socklen_t address_len); socklen_t address_len); // example

Allow connections on the server #include <sys/socket.h> int listen(int socket, int backlog); listen(listenfd, 10);

Accept (wake up) input while(1) { connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); write(connfd, sendBuff, strlen(sendBuff)); close(connfd); sleep(1); }

C/C++ socket code #include <sys/types.h> #include <sys/socket.h> char buffer[MAXBUF]; int s = socket(domain, type, protocol) int bind(int socket, const struct sockaddr *address, socklen_t address_len); int connect(int socket, const struct sockaddr *address, socklen_t int listen(int socket, int backlog); nbytes = write(fd, buffer, 20); /* write 20 bytes in buffer */

Java Client code part 1 public class GreetingClient { public static void main(String [] args) String serverName = args[0]; int port = Integer.parseInt(args[1]); try // try code } catch(IOException e) // catch code

Java Client code part 2 try { System.out.println("Connecting to " + serverName + " on port " + port); Socket client = new Socket(serverName, port); System.out.println("Just connected to " + client.getRemoteSocketAddress()); OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeUTF("Hello from “ + client.getLocalSocketAddress()); InputStream inFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer); System.out.println("Server says " + in.readUTF()); client.close(); } catch(IOException e) { e.printStackTrace();}

Java Server Code part 1 // File Name GreetingServer.java import java.net.*; import java.io.*; public class GreetingServer extends Thread { private ServerSocket serverSocket; public GreetingServer(int port) throws IOException serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(10000); }

Java Server Code part 2 - catch catch(SocketTimeoutException s) { System.out.println("Socket timed out!"); break; } catch(IOException e) e.printStackTrace();

Java Server Code part 2 - try public void run() { while(true) try System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("Just connected to “ + server.getRemoteSocketAddress()); DataInputStream in = new DataInputStream(server.getInputStream()); System.out.println(in.readUTF()); DataOutputStream out = new DataOutputStream(server.getOutputStream()); out.writeUTF("Thank you for connecting to “ + server.getLocalSocketAddress() + "\nGoodbye!"); server.close(); }

Java Server Code part 3 public static void main(String [] args) { int port = Integer.parseInt(args[0]); try Thread t = new GreetingServer(port); t.start(); }catch(IOException e) e.printStackTrace(); }