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.

Slides:



Advertisements
Similar presentations
Referring to Java API Specifications
Advertisements

Java Sockets Source:
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 TCP socket application Architecture of Client-Server Applications Java Socket Programming Client Application Server Application.
System Programming Practical session 10 Java sockets.
Java sockets. From waiting.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
1 Network Layers Application Transport Network Data-Link Physical bits.
Network/Socket Programming in Java Rajkumar Buyya.
1 Inter-Process Communication: Network Programming using TCP Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept.
Socket Programming in Java -First Step of Network Programming-
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.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
DBI Representation and Management of Data on the Internet.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
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.
Socket Programming Lee, Sooyong
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
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.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
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.
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.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
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.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket 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,
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
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 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.
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.
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.
Socket Programming in Java -First Step of Network Programming-
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
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.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Java Packages Thread Example Java Socket Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Source: Java Sockets Source:
MCA – 405 Elective –I (A) Java Programming & Technology
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
CSI 4118 – UNIVERSITY OF OTTAWA
„Networking”.
UNIT-6.
CSI 4118 – UNIVERSITY OF OTTAWA
CSCD 330 Network Programming
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

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 accepts. A socket is one endpoint of a two-way communication link between two programs running on the network.

Sockets in Java Java.net package provides a class, Socket, that implements one side of a two way communication. Sits on a platform-dependent implementation, hiding the details of any particular system. Also has ServerSocket class.

Communication What the server needs to do.  Create a server socket.  Listen for incoming connection attempts.  Communicate with client.  Interact until it is time to close the connection.  Server or client can close the connection.  Go back to listening.

Communication What the client needs to do  Connect to server.  Send data.  Receive data.  Close the connection.

java.net.Socket Constructors:  Socket(): Creates an unconnected socket.  Socket(InetAddress address, int port) Creates a stream socket and connects to the specified port at the specified address.  Socket(InetAddress address, int port, InetAddress localAddr, int localPort): Creates a socket and connects it to the specified remote address on the specified remote port.

Things I didn’t tell you The actual work of the socket is performed by an instance of the SocketImpl class. The abstract class SocketImpl is a common superclass of all classes that actually implement sockets. It is used to create both client and server sockets.

Methods of java.net.Socket Bind(SocketAddress) close() connect(SocketAddress ) isConnected() isClosed() isBound()

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client = new Socket( server, port_id ); server = new ServerSocket( PORT );

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket DataOutputStream os; DataInputStream is;

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket Socket client = server.accept();

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); String line = is.readLine(); os.writeBytes("Hello\n");

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client.close();

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket is = new DataInputStream(client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() );

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket String line = is.readLine(); os.writeBytes("Hello\n");

What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client.close();

Sample Program import java.io.*; import java.net.*; public class echo3 { public static void main(String args[]) { // declaration section: // declare a server socket and a client socket for the server // declare an input and an output stream ServerSocket echoServer = null; String line; DataInputStream is; PrintStream os; Socket clientSocket = null; // Try to open a server socket on port 9999 // Note that we can't choose a port less than 1023 if we are not // privileged users (root) try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } // Create a socket object from the ServerSocket to listen and accept // connections. // Open input and output streams try { clientSocket = echoServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); // As long as we receive data, echo that data back to the client. while (true) { line = is.readLine(); os.println(line); } } catch (IOException e) { System.out.println(e); } } }

Client Program import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to:taranis."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); }

TCP/IP and UDP/IP communications Datagram communication:  Connectionless Stream communication:  Connection oriented UDP or TCP?  UDP is unreliable  Setup time for TCP

Sockets and Security Means by which computers communicate Possible attacks  DOS  Buffer overflow Machine becomes a mule. Firewall  Blocks or restricts access to ports.

Just scratching the surface! Multiple clients Multi-threaded server sockets Proxies

If you didn’t believe what I said, go here…(References) working/sockets/index.html working/sockets/index.html /jw-12-sockets.html /jw-12-sockets.html 35/socket_tut.html