Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.

Slides:



Advertisements
Similar presentations
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r Sockets are explicitly created, used, released by applications.
Advertisements

Network Programming and Java Sockets
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
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.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
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.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Socket programming with UDP and TCP. Socket Programming with TCP Connection oriented – Handshaking procedure Reliable byte-stream.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Network Layers Application Transport Network Data-Link Physical bits.
Protocols Rules for communicating between two entities (e.g., a client and a server) “A protocol definition specifies how distributed system elements interact.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
Network Programming and Java Sockets 1 Dr. Christian Vecchiola Postdoctoral Research Fellow Cloud Computing and Distributed Systems.
JAVA Socket Programming Joonbok Lee KAIST.
2: Application Layer 1 Socket Programming TCP and UDP.
1 Inter-Process Communication: Network Programming using TCP Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
ECE5650: Network Programming
1 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.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
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.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Java Socket programming. Socket programming with TCP.
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.
NETWORK PROGRAMMING.
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 Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
1 All rights reserved to Chun-Chuan Yang Upon completion you will be able to: The OSI Model and the TCP/IP Protocol Suite Understand the architecture of.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Topic: Network programming
Socket Programming Socket Programming Overview
Transport layer (last part) Application layer
DNS: Domain Name System
Socket programming with TCP
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
CSCD 330 Network Programming
Outline Introduction Networking Basics Understanding Ports and Sockets
Socket Programming Socket Programming Overview
Socket Programming.
CSCD 330 Network Programming
Java Socket Programming
Socket Programming 2: Application Layer.
DNS: Domain Name System
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
DNS: Domain Name System
DNS: Domain Name System
Socket Programming with UDP
Presentation transcript:

Socket-Programming

 Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.

3 Network Request Result a client, a server, and network Client Server Client machine Server machine

 Other examples:  Chatroom

 Connection-oriented  Setup the link before communication.  Similar to the phone call. We need the phone number and receiver.  Connectionless  No link needed to be set up before communication.  Similar to send a letter. We need the address and receiver.

6  UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival.  Example applications:  Clock server  Ping  TCP/IP Stack Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

7  The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. server PORT PORT Client TCP TCP or UDP port app port#data Data Packet

Figure 2.6-2: Client socket, welcoming socket and connection socket

 Server waits for client to request a connection.  Client contacts server to establish a connection.  Client sends request.  Server sends reply.  Client and/or server terminate connection.

 Socket  The combination of an IP address and a port number. (RFC 793,original TCP specification)  The name of the Berkeley-derived application programming interfaces (APIs) for applications using TCP/IP protocols.  Two types  Stream socket : reliable two-way connected communication streams  Datagram socket  Socket pair  Specified the two end points that uniquely identifies each TCP connection in an internet.  4-tuple: (client IP address, client port number, server IP address, server port number)

 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  Port numbers between are reserved  User level process/services generally use port number value >= 1024

 UDP  Connectionless and unreliable service.  There isn’t an initial handshaking phase.  Doesn’t have a pipe.  transmitted data may be received out of order, or lost  Socket Programming with UDP  No need for a welcoming socket.  No streams are attached to the sockets.  the sending hosts creates “packets” by attaching the IP destination address and port number to each batch of bytes.  The receiving process must unravel to received packet to obtain the packet’s information bytes.

 SOCK_STREAM  TCP  connection oriented, bidirectional  reliable, in-order delivery SOCK_DGRAM  UDP  no connection  unreliable delivery, no guarantee on the order  can send/receive

socket() connect()‏ send() recv() close()... socket() bind() listen() accept() recv() send() close() wait for connection request from next client Client Server

 The package java.net provides support for sockets programming (and more).  Typically you import everything defined in this package with: import java.net.*; Java Socket Programming 16

 The Java API provides datagram communication by means of two classes:  DatagramPacket - Datagram packets are used to implement a connectionless packet delivery service.  DatagramSocket - A datagram socket is the sending or receiving point for a packet delivery service.  DatagramPacket:  getData - Returns the data buffer.  getPort - Returns the port number on the remote host.  getAddress - Returns the IP address.

 DatagramSocket:  send - Sends a datagram packet from this socket.  receive - Receives a datagram packet from this socket.  setSoTimeout - Enable/disable the specified timeout, in milliseconds.  connect - Connects the socket to a remote address for this socket.

InetAddress Socket ServerSocket DatagramSocket DatagramPacket Java Socket Programming 19

 DatagramSocket class  DatagramPacket class needed to specify the payload  incoming or outgoing Java Socket Programming 20

 In Package java.net  java.net.DatagramSocket  A socket for sending and receiving datagram packets.  Constructor and Methods  DatagramSocket(int port): Constructs a datagram socket and binds it to the specified port on the local host machine.  void receive( DatagramPacket p)  void send( DatagramPacket p)  void close() Java Socket Programming 21

DatagramSocket(); DatagramSocket(int port); DatagramSocket(int port, InetAddress a); All can throw SocketException or SecurityException Java Socket Programming 22

void connect(InetAddress, int port); void close(); void receive(DatagramPacket p); void send(DatagramPacket p); Java Socket Programming 23

 Contain the payload  (a byte array  Can also be used to specify the destination address  when not using connected mode UDP Java Socket Programming 24

For receiving: DatagramPacket( byte[] buf, int len); For sending: DatagramPacket( byte[] buf, int len InetAddress a, int port); Java Socket Programming 25

 Corresponds to active TCP sockets only!  client sockets  socket returned by accept();  Passive sockets are supported by a different class:  ServerSocket  UDP sockets are supported by  DatagramSocket Java Socket Programming 26

CPSC Application Layer 27 Client process client TCP socket Stream  A stream is a sequence of characters that flow into or out of a process.  An input stream is attached to some input source for the process, e.g., keyboard or socket.  An output stream is attached to an output source, e.g., monitor or socket.

close clientSocket Server (running on hostid) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create, address (hostid, port=x, send datagram request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port umber

CPSC Application Layer 29 import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket

30  A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request. server Client Connection request port

31  If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client. server Client Connection port

 socket (): Create a socket  bind(): bind a socket to a local IP address and port #  listen(): passively waiting for connections  connect(): initiating connection to another socket  accept(): accept a new connection  Write(): write data to a socket  Read(): read data from a socket  sendto(): send a datagram to another UDP socket  recvfrom(): read a datagram from a UDP socket  close(): close a socket (tear down the connection)

 In Package java.net  java.net.DatagramSocket  A socket for sending and receiving datagram packets.  Constructor and Methods  DatagramSocket(int port): Constructs a datagram socket and binds it to the specified port on the local host machine.  void receive( DatagramPacket p)  void send( DatagramPacket p)  void close()

import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }

import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData());

InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); }

38 1. Open the Server Socket: ServerSocket server; DataOutputStream os; DataInputStream is; server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket client = server.accept(); 3. Create I/O streams for communicating to the client is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); 4. Perform communication with client Receive from client: String line = is.readLine(); Send to client: os.writeBytes("Hello\n"); 5. Close sockets: client.close(); For multithreaded server: while(true) { i. wait for client requests (step 2 above) ii. create a thread with “ client ” socket as parameter (the thread creates streams (as in step (3) and does communication as stated in (4). Remove thread once service is provided. }

39 1. Create a Socket Object: client = new Socket( server, port_id ); 2. Create I/O streams for communicating with the server. is = new DataInputStream(client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); 3. Perform I/O or communication with the server:  Receive data from the server: String line = is.readLine();  Send data to the server: os.writeBytes("Hello\n"); 4. Close the socket when done: client.close();

40 // SimpleServer.java: a simple server program import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) throws IOException { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); }

41 // SimpleClient.java: a simple client program import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]) throws IOException { // Open your connection to a server, at port 1234 Socket s1 = new Socket("mundroo.cs.mu.oz.au",1234); // Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); }

42  Run Server on mundroo.cs.mu.oz.au  java SimpleServer &  Run Client on any machine (including mundroo):  java SimpleClient Hi there  If you run client when server is not up:  sockets [1:147] java SimpleClient Exception in thread "main" java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:320) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:133) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:120) at java.net.Socket. (Socket.java:273) at java.net.Socket. (Socket.java:100) at SimpleClient.main(SimpleClient.java:6)

43  A socket is an endpoint of a two-way communication link between two programs running on the network.  A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent.  Java’s.net package provides two classes:  Socket – for implementing a client  ServerSocket – for implementing a server

44 1. Open the Server Socket: ServerSocket server; DataOutputStream os; DataInputStream is; server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket client = server.accept(); 3. Create I/O streams for communicating to the client is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); 4. Perform communication with client Receive from client: String line = is.readLine(); Send to client: os.writeBytes("Hello\n"); 5. Close sockets: client.close(); For multithreaded server: while(true) { i. wait for client requests (step 2 above) ii. create a thread with “ client ” socket as parameter (the thread creates streams (as in step (3) and does communication as stated in (4). Remove thread once service is provided. }

45 1. Create a Socket Object: client = new Socket( server, port_id ); 2. Create I/O streams for communicating with the server. is = new DataInputStream(client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); 3. Perform I/O or communication with the server:  Receive data from the server: String line = is.readLine();  Send data to the server: os.writeBytes("Hello\n"); 4. Close the socket when done: client.close();

46 // SimpleServer.java: a simple server program import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) throws IOException { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); }

47 // SimpleClient.java: a simple client program import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]) throws IOException { // Open your connection to a server, at port 1234 Socket s1 = new Socket("mundroo.cs.mu.oz.au",1234); // Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); }

48  Run Server on mundroo.cs.mu.oz.au  java SimpleServer &  Run Client on any machine (including mundroo):  java SimpleClient Hi there  If you run client when server is not up:  sockets [1:147] java SimpleClient Exception in thread "main" java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:320) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:133) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:120) at java.net.Socket. (Socket.java:273) at java.net.Socket. (Socket.java:100) at SimpleClient.main(SimpleClient.java:6)

49  TCP (Transport Control Protocol) is a connection- oriented protocol that provides a reliable flow of data between two computers.  Example applications:  HTTP  FTP  Telnet  TCP/IP Stack Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)