Babak Esfandiari (based on slides by Qusay Mahmoud)

Slides:



Advertisements
Similar presentations
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Advertisements

UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
Client Design. Issues Server Identification Setting up a socket on client side TCP –Reading and writing with a socket –Closing a socket UDP –Reading and.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Socket Programming in Java -First Step of Network Programming-
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
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,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
TCP/IP: Basics1 User Datagram Protocol (UDP) Another protocol at transport layer is UDP. It is Connectionless protocol i.e. no need to establish & terminate.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
CS 424/524: Introduction to Java Programming Lecture 25 Spring 2002 Department of Computer Science University of Alabama Joel Jones.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 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.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Java Sockets Programming
UDP User Datagram Protocol. User Datagram Protocol (UDP) UDP is the protocol available to network programmers who wish to send datagrams UDP datagrams.
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.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
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.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
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,
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
Socket Programming in Java -First Step of Network Programming-
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Network Programming. These days almost all devices.
Distributed Systems1 Socket API  The socket API is an Interprocess Communication (IPC) programming interface originally provided as part of the Berkeley.
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
MCA – 405 Elective –I (A) Java Programming & Technology
CS 1652 Jack Lange University of Pittsburgh
Network Programming Introduction
Network Programming Introduction
Socket programming - Java
NETWORK PROGRAMMING CNET 441
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Presentation transcript:

Babak Esfandiari (based on slides by Qusay Mahmoud) Socket Programming Babak Esfandiari (based on slides by Qusay Mahmoud)

Sockets Programming Client-Server Computing What are Sockets Sockets Programming in Java Programming Examples

Client/Server Computing Simple idea: Some hosts (clients, typically desk top computers) are specialized to interact with users: Gather input from users Present information to users Other hosts (servers) are specialized to manage large data, process that data The Web is a good example: Client (Browser) & Server (HTTP server)

Client/Server Computing Other examples: E-mail Server Client Client

Client/Server Computing Other examples: Chatroom

Tiered Client/Server Architecture 1-tier: single program 2-tier: client/server (e.g. the Web) 3-tier: application logic and databases on different servers (e.g. the Web with CGI and databases)

Client/Server Communication Two related processes on a single machine may communicate through a pipe A pipe is a pseudo-file that can be used to connect two processes together

Client/Server Communication Two UNRELATED processes may communicate through files (process A write to a file and process B reads from it) But HOW two processes located on two different machines communicate? Solution: Berkeley sockets.

What are sockets A socket is an end point of a connection Or: the interface between user and network Two sockets must be connected before they can be used to transfer data (case of TCP) A number of connections to choose from: TCP, UDP, Multicast Types of Sockets SOCK_STREAM, SOCK_DGRAM, SOCK_RAW

Sockets Message destinations are specified as socket adresses Each socket address is a communication identifier: Internet address Port number The port number is an integer that is needed to distinguish between services running on the same machine Port numbers between 0 .. 1023 are reserved

Ports Some “well-known” ports: 21: ftp 23: telnet 80: http 161: snmp Check out /etc/services file for complete list of ports and services associated to those ports

Which transport protocol (TCP v. UDP) TCP -- Transmission Control Protocol UDP -- User Datagram Protocol What should I use? TCP is a reliable protocol, UDP is not TCP is connection-oriented, UDP is connectionless TCP incurs overheads, UDP incurs fewer overheads UDP has a size limit of 64k, in TCP no limit

Unix and C specific data structures The <netdb.h> library provides the following data structures: struct hostent { // for host info char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0] };

Unix and C specific data structures struct servent { //service info char *s_name; char **s_aliases; int s_port; char *s_proto; };

Unix and C specific data structures The following functions return information about a given host or service: struct hostent *gethostbyname (char *hostname) struct servent *getservbyname (char *service, char *protocol)

UDP Socket programming UDP is simple and efficient, but not reliable Communication takes place in a symmetric manner: both ends send and receive messages following an agreed upon protocol

UDP Socket Programming Since no connection is created, each message should contain the address of the recipient. In Java, messages are contained in instances of class DatagramPacket

The DatagramPacket class Constructors: One for sending datagrams: DatagramPacket{byte buffer[], int length, InetAddress, int port} One for receiving datagrams: DatagramPacket{byte buffer[], int length}

The DatagramPacket class The useful methods are the accessors: InetAddress getAddress() Int getPort() Byte[] getData() Int getLength()

Sending and receiving Datagram packets: using sockets Both ends need to create sockets to communicate Sockets will be “bound” to a given host and port number The receiver needs to “listen” on a port number that is known by the sender The sender can a priori use any port number

The DatagramSocket class Constructors: One for the sender (randomly chosen port number): DatagramSocket() throws SocketException One for the receiver (port needs to be specified): DatagramSocket(int port) throws SocketException

The DatagramSocket class Sending and receiving messages: void send(DatagramPacket packet) throws IOException void receive(DatagramPacket packet) throws IOException Close the socket when you’re done! void close()

Receiving UDP packets DatagramSocket socket = new DatagramSocket(port); Byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); Socket.receive(packet); InetAddress fromAddress = packet.getAddress(); int fromPort = packet.getPort(); int length = packet.getLength(); byte[] data = packet.getData(); socket.close();

Sending UDP Packets DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(“eureka.sce.carleton.ca”), 1728); socket.send(packet); socket.close();

TCP Socket Communication Sequence of steps normally taken to set up socket communication and exchange data between C/S

Sockets Programming in Java Streams The basic of all I/O in Java is the data stream A pipeline of data put info into the pipeline (write) and get it (read) Programming with Sockets (TCP) Opening a Socket Creating a data input stream Creating a data output stream Closing the socket(s)

Opening a socket Client-side: Socket myClient = null; try { MyClient = new Socket(“host”, PotNumber); } catch (UnknownHostException uhe) { uhe.printStackTrace(); } “host” can be symbolic name or IP address

Opening a socket Server-side ServerSocket myService = null; try { myService = new ServerSocket(portNumber); } catch (UnknownHostException uhe) { uhe.printStackTrace(); } Socket serviceSocket; serviceSocket = myService.accept();

Creating an input stream Client-side: BufferedReader is = null; try { is = new BufferedReader(new InputStreamReader(myClient.getInputStream())); } catch (IOException ioe) { ioe.printStackTrace(); }

Creating an input stream Server-side: BufferedReader is = null; try { is = new BufferedReader(new InputStreamReader(serviceClient.getInputStream())); } catch(IOException ioe) { ioe.printStackTrace(); }

Creating an output stream Client-side: DataOutputStream os = null; try { os = new DataOutputStream(myClient.getOutputStream()); } catch (IOException e) { e.printStrackTrace(); }

Creating an output stream Server-side: DataOutputStream os = null; try { os = new DataOutputStream(serviceClient.getOutputStream()); } catch(IOException e) { e.printStackTrace(); }

Closing sockets Client-side: try { os.close(); is.close(); myClient.close(); } catch(IOException e) { e.printStrackTrace(); }

Closing sockets Server-side: try { os.close(); is.close(); serviceSocket.close(); myService.close(); } catch(IOException e) { e.printStackTrace(); }

An Example See the “Counter” class example

Multi-threaded Servers A server should be able to serve multiple clients simultaneously

Multi-threaded Servers See the MTEchoServer example