Lecture 21 Sockets 1 (Not in D&D) Date.

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

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.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
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.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
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.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
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
Java Server Programming Web Interface for Java Programs.
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.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
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();
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.
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
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
Echo Networking COMP
Sockets and Beginning Network Programming
Networking COMP
MCA – 405 Elective –I (A) Java Programming & Technology
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
NETWORK PROGRAMMING CNET 441
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Sockets and URLs 17-Sep-18.
Sockets and URLs 3-Dec-18.
Starting TCP Connection – A High Level View
TCP/IP Sockets in Java: Practical Guide for Programmers
CS18000: Problem Solving and Object-Oriented Programming
Exceptions and networking
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Lecture 21 Sockets 1 (Not in D&D) Date

Goals By the end of this lesson, you should: Understand the difference in the role of a server and client on a network. Be able to configure a server socket and a client socket in Java. Understand the basics of a TCP connection from an application point of view. Be able to implement a simple server and client program to exchange text-based information via a network such as the Internet.

Terminology Terminology Java Sockets Simple Client Simple Server Notes Summary On a TCP/IP based network such as the Internet, each machine is known as a host. Think of it as a building. A host may have one or more network interfaces. Note that these have nothing to do with the Java interface concept. These are interfaces such as Ethernet interfaces or WiFi. Think of them as entrances to the building. Each network interface has an IP address (usually IP version 4 but increasingly IPv6 as well). IPv4 addresses are 4 integers between 0 and 255 separated by dots, e.g., 130.216.159.127. Think of them as the “street address” of the building. Each host can run multiple applications that use the network. Applications come in two flavours: servers and clients.

Terminology Terminology Java Sockets Simple Client Simple Server Notes Summary A server is an application on a host that waits for incoming connection requests from a client. Example: A web server waits for incoming connections from browser clients. A client is an application on (usually a different host) that initiates the connection to a server. Example: A web browser is a client program that initiates connections to web servers. On the network, data travels in packets, a kind of serialized object. Think of them as snail mail. Each packet carries an IP header that identifies source and destination hosts (via their IP addresses). TCP packets travel inside the payload of IP packets.

Terminology Terminology Java Sockets Simple Client Simple Server Notes Summary To find out which application the payload of a TCP packet is intended for, each TCP packet carries a TCP header with the port numbers of the sending and the destination application. Port numbers are integers between 1 and 65535. Think of them as the apartment numbers of the actual sender & receiver in the building. A server binds to a particular port number and listens on that port. This port number must be known to any client wanting to connect to the server. A client connects to the server address/port using its own address and an arbitrary port supplied by the client’s operating system. To do so, it sends a TCP connection request.

Terminology Terminology Java Sockets Simple Client Simple Server Notes Summary The listening server decides whether to accept the connection request. Once accepted, both sides can send and receive data on the connection. The data streams in each direction are reliable byte streams, meaning either side can be assured that data it sends will be received in the order it was sent. Either server or client may close the connection.

Sockets Terminology Java Sockets Simple Client Simple Server Notes Summary In order to communicate via TCP connections, applications use data structures called sockets. Java knows two types of sockets: Sockets of the Socket class. We instantiate a Socket object by passing the Socket() constructor an IP address and port number to connect to. The constructor either establishes the connection and returns a Socket object, or throws an IOException if it is unable to connect (e.g., network problem, server (host or application) not running). Sockets of the ServerSocket class. We instantiate a ServerSocket by passing its constructor a port number to bind to and listen on. We also specify a maximum number of connections that are allowed to wait for acceptance.

ServerSockets Terminology Java Sockets Simple Client Simple Server Notes Summary A ServerSocket throws an IOException if it is unable to bind to the specified port. This usually happens if the port is already in use (something that often happens during development!) Once the ServerSocket listens, we call its accept() method. This method blocks (does not return) until there is a connection request from a client. The method returns an already-connected Socket object.

Connected Sockets Terminology Java Sockets Simple Client Simple Server Notes Summary Once both sides have a connected Socket object, they can communicate with each other. Each side can find out who the remote side is by calling the getRemoteSocketAddress() method on its own socket. This returns the IP address and port number used by the socket on the other side. On each socket, we can get an InputStream (for reading data from the other side) with its getInputStream() method. We can use this stream with Scanner, ObjectInputStream, DataInputStream etc. Similarly, we can get an OutputStream (for writing data to the other side) with getOutputStream(). We can use this stream with PrintStream, Formatter, ObjectOutputStream, DataOutputStream etc.

Example: Simple Client Terminology Java Sockets Simple Client Simple Server Notes Summary import java.net.Socket; import java.io.PrintStream; import java.util.Scanner; public class SimpleClient { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // IP address and port to connect to: String serverHost = "localhost"; int serverPort = 9876; Socket socket = null; try { socket = new Socket(serverHost, serverPort); } catch (Exception e1) { System.err.print("Unable to connect to " + serverHost); System.err.println(" on " + serverPort); System.err.println(e1.getMessage()); System.exit(1); } System.out.print("Connected to " + serverHost); System.out.println(" on port " + serverPort); …

Example: Simple Client Terminology Java Sockets Simple Client Simple Server Notes Summary … try { Scanner input = new Scanner(socket.getInputStream()); PrintStream output = new PrintStream(socket.getOutputStream()); String message = ""; while (!message.trim().equals("quit")) { System.out.println("Enter a string to send to the server:"); message = scanner.nextLine(); output.println(message); System.out.println("Done. Waiting for server response."); String response = input.nextLine(); System.out.print("The server responded with a "); System.out.println(response.length() + " byte message: "); System.out.println("\"" + response.trim() + "\""); } System.out.println("Disconnecting from " + serverHost); socket.close(); System.out.print("Done."); } catch (Exception e2) { System.err.print("Something went wrong with the socket communication."); System.err.println(e2.getMessage()); System.exit(1);

Example: Simple Server Terminology Java Sockets Simple Client Simple Server Notes Summary import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; import java.io.PrintStream; import java.util.Scanner; public class SimpleServer { public static void main(String[] args) { // Some configuration int port = 9876; // port number between 1024 and 65535 int backlog = 1; // allow 1 pending connection request // Try to create a server socket for the given port ServerSocket server = null; try { server = new ServerSocket(port, backlog); } catch (IOException ioExc1) { System.err.println(“Can’t create server socket"); System.err.println(ioExc1.getMessage()); System.exit(1); …

Example: Simple Server Terminology Java Sockets Simple Client Simple Server Notes Summary … Socket socket = null; // ...only null for now while (true) { try { // accept() waits for an incoming connection socket = server.accept(); System.out.println("Accepted connection from client " + socket.getRemoteSocketAddress()); // Let's communicate: String inputString = ""; Scanner input = new Scanner(socket.getInputStream()); // output PrintStream lets us send data to the server PrintStream output = new PrintStream(socket.getOutputStream()); // Now communicate until the client sends "quit": while (!inputString.trim().equals("quit")) { inputString = input.nextLine(); // Communication here just means echoing the // client input back: System.out.println("Client says: " + inputString); String outputString = "You said: " + inputString + "\n"; output.println(outputString); } …

Example: Simple Server Terminology Java Sockets Simple Client Simple Server Notes Summary … try { … catch (IOException ioExc2) { // Ignore and continue System.err.println("Something went wrong with the connection"); System.err.println(ioExc2.getMessage()); } finally { try { // Closing the socket prevents a resource leak if (socket != null) socket.close(); catch (IOException ioExc3) { System.err.println("Couldn't close socket properly, ignoring."); System.err.println(ioExc3.getMessage()); System.out.println("Disconnected from client!"); // Ready for the next connection Proper cleanup is important here as our server might be handling many connections over its lifetime, and sockets are a scarce resource (a system can typically have at most 65536 of them).

Why is this “simple”? Terminology Java Sockets Simple Client Simple Server Notes Summary Note that our simple server doesn’t get to call its accept() method again until it the connection has closed. This means that the simple server can only handle one connection at a time. Most “real” servers need to be able to accept connections pretty much as soon as they come in, and handle them there and then, no matter how long they will last. This is typically done with threads, a concept that we will revisit at length shortly. In the next lecture, we will take a sneak preview at a threaded server application that lets our server take multiple simultaneous client connections.

Note: Trying this out Terminology Java Sockets Simple Client Simple Server Notes Summary The client code example for this lecture uses a hardwired IP address (or rather, a hostname called “localhost”, which your computer knows to translate to IP address 127.0.0.1). This is a special IP address that points back at your own machine. If you want to experiment between different machines, you need to know the IP address of the server and need to change this in the client code. In order to run both client and server on your own machine, you need to run one of them (the server) from a command line window rather than Eclipse. Open a command line window and change to the src directory in your Eclipse. Build the server with: javac SimpleServer.java Then run java SimpleServer and run the client from Eclipse. You can stop the server in the command line window with CTRL+C. If you run the server in Eclipse, you will not be able to start the client there, or re-start the server (unless you restart Eclipse).

What do we know Terminology Java Sockets Simple Client Simple Server Notes Summary Java server sockets are a ServerSocket object. They are configured with a port and a maximum backlog and start to listen upon instantiation. Java client sockets are a Socket object. They are configured with the server address and port to connect to, and attempt to connect on instantiation. They throw an exception if they cannot connect. The server socket waits for an incoming connection when its accept() method is called. The method returns a Socket object for the incoming connection. We can use the input and output streams of the Socket object with our usual stream tools for writing and reading to/from console and file.

Resources Terminology Java Sockets Simple Client Simple Server Notes Summary https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html https://docs.oracle.com/javase/8/docs/api/java/io/ObjectOutputStream.html https://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html https://docs.oracle.com/javase/8/docs/api/java/io/DataInputStream.html

Next Lecture Sockets 2 (not in D&D)