SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.

Slides:



Advertisements
Similar presentations
Network Programming and Java Sockets
Advertisements

Inter-Process Communication: Network Programming using TCP Java Sockets Dr. Rajkumar Buyya Cloud Computing and Distributed Systems (CLOUDS) Laboratory.
1 Network Programming and Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Network Programming and Java Sockets
Network Programming Chapter 11 Lecture 6. Networks.
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.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
1 Java Networking – Part I CS , Spring 2008/9.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Network Programming and Java Sockets 1 Dr. Christian Vecchiola Postdoctoral Research Fellow Cloud Computing and Distributed Systems.
Network/Socket Programming in Java Rajkumar Buyya.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
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.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Lector: Aliyev H.U. Lecture №14: Telecommun ication network software design for data bases and servers. TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
 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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
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.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
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.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
1 Network Communications A Brief Introduction. 2 Network Communications.
Networking Mehdi Einali Advanced Programming in Java 1.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Echo Networking COMP
MCA – 405 Elective –I (A) Java Programming & Technology
Network Programming Introduction
„Networking”.
UNIT-6.
Outline Introduction Networking Basics Understanding Ports and Sockets
Review Communication via paired sockets, one local and one remote
Presentation transcript:

SOCKET PROGRAMMING

Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as shown in Fig: A computer running a program that makes a request for services is called client machine. A computer running a program that offers requested services from one or more clients is called server machine. The media for communication can be wired or wireless network.

TCP is a connection-oriented protocol that provides a reliable flow of data between two computers. Example applications that use such services are HTTP, FTP, and Telnet. UDP is a protocol that sends independent packets of data, called data grams, from one computer to another with no guarantees about arrival and sequencing. Example applications that use such services include Clock server and Ping. The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. Port is represented by a positive (16-bit) integer value. Some ports have been reserved to support common/well known services: – ftp 21/tcp – telnet 23/tcp – smtp 25/tcp – login 513/tcp – http 80/tcp,udp – https 443/tcp,udp – User-level process/services generally use port number value >= 1024.

Hosts Identification and Service Ports IP address : Every computer on the Internet is identified by a unique, 4-byte IP address. This is typically written in dotted quad format like where each byte is an unsigned value between 0 and 255. Port : A logical access point, represented by a 16-bit integer number is used. That means, each service offered by a computer is uniquely identified by a port number.

SOCKET PROGRAMMING AND JAVA.NET CLASS A socket is an endpoint of a two-way communication link between two programs running on the network. Socket is bound to a port number and ip address so that the TCP layer can identify the application that data is destined to be sent. Java provides a set of classes, defined in a package called java.net, to enable the rapid development of network applications. Key classes, interfaces, and exceptions in java.net package simplifying the complexity involved in creating client and server programs are:

THE INTERFACES ContentHandlerFactory FileNameMap SocketImplFactory URLStreamHandlerFactory THE CLASSES ContentHandler DatagramPacket DatagramSocket DatagramSocketImpl HttpURLConnection InetAddress MulticastSocket ServerSocket Socket SocketImpl URL URLConnection URLEncoder URLStreamHandler EXCEPTIONS BindException ConnectException ProtocolException SocketException UnknownHostException UnknownServiceException

TCP/IP SOCKET PROGRAMMING The two key classes from the java.net package used in creation of server and client programs are: – ServerSocket – Socket A server program creates a specific type of socket that is used to listen for client requests (server socket),In the case of a connection request, the program creates a new socket through which it will exchange data with the client using input and output streams. The socket abstraction is very similar to the fi le concept: developers have to open a socket, perform I/O, and close it

A simple Server Program in Java The steps for creating a simple server program are: 1. Open the Server Socket: ServerSocket server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket client = server.accept(); 3. Create I/O streams for communicating to the client DataInputStream is = new DataInputStream(client.getInputStream()); DataOutputStream 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 socket: client.close();

An example program illustrating creation of a server socket, waiting for client request, and then responding to a client that requested for connection by greeting it is given below: Program // 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 1254 ServerSocket s = new ServerSocket(1254); 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(); }

A simple Client Program in Java The steps for creating a simple client program are: 1. Create a Socket Object: Socket 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();

An example program illustrating establishment of connection to a server and then reading a message sent by the server and displaying it on the console is given below: // 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 1254 Socket s1 = new Socket(“localhost”,1254); // 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(); }

try { c=s.accept(); is=new DataInputStream(c.getInputStream()); ps=new PrintStream(c.getOutputStream()); while(true) { line=is.readLine(); ps.println(line); } } catch(IOException e) { System.out.println(e); } } } SERVER SIDE import java.net.*; import java.io.*; public class EServer { public static void main(String args[]) { ServerSocket s=null; String line; DataInputStream is; PrintStream ps; Socket c=null; try { s=new ServerSocket(9000); } catch(IOException e) { System.out.println(e); }

try { os=new PrintStream(c.getOutputStream()); is=new DataInputStream(System.in); is1=new DataInputStream(c.getInputStream()); while(true) { System.out.println("Client:"); line=is.readLine(); os.println(line); System.out.println("Server:" + is1.readLine()); } } catch(IOException e) { System.out.println("Socket Closed!"); } } } CLIENT SIDE import java.net.*; import java.io.*; public class EClient { public static void main(String arg[]) { Socket c=null; String line; DataInputStream is,is1; PrintStream os; try { c=new Socket(" ",9000); } catch(IOException e) { System.out.println(e); }

OUTPUT Server C:\Program Files\Java\jdk1.5.0\bin>javac EServer.java Note: EServer.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\Program Files\Java\jdk1.5.0\bin>java EServer C:\Program Files\Java\jdk1.5.0\bin>Client Client C:\Program Files\Java\jdk1.5.0\bin>javac EClient.java Note: EClient.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\Program Files\Java\jdk1.5.0\bin>java EClient Client:Hai Server Server:Hai Server Client:Hello Server:Hello Client:end Server:end Client:ds Socket Closed!