Java Networking (sockets)

Similar documents
Network Programming Benoît Garbinato

Principles, Models and Applications for Distributed Systems M

Java Socket Workshop. July Purpose of this workshop:

Networking Basics. network communication.

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Socket Programming(TCP & UDP) Sanjay Chakraborty

JAVA Network API. 2 - Connection-Oriented vs. Connectionless Communication

Java Technologies. Lecture VII. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Advanced Java Programming. Networking

Unit 1 Java Networking

Networking and Security

Connecting to a Server Implementing Servers Making URL Connections Advanced Socket Programming

CS September 2017

CSCD 330 Network Programming Spring 2018

CSCD 330 Network Programming Winter 2019

Lecture 3: Socket Programming

JAVA - NETWORKING (SOCKET PROGRAMMING)

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Interprocess Communication

protocols September 15,

CSCD 330 Network Programming Spring 2018

CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

JAVA SOCKET PROGRAMMING

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP

CPSC 441 Assignment-3 Discussion. Department of Computer Science University of Calgary

Software Practice 1 - Socket

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.

31 Network Programming

CPSC 441 UDP Socket Programming. Department of Computer Science University of Calgary

Input from Files. Buffered Reader

Internet Technology 2/7/2013

CPSC 441 Assignment-3 Discussion. Department of Computer Science University of Calgary

Introduction to Sockets 9/25/14

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

C19: User Datagram and Multicast

Introduction to Socket Programming

Communication in Distributed Systems: Sockets Programming. Operating Systems

Distributed Systems. 3. Access to the Transport Layer. Werner Nutt

Java A.1 TCP/IP TCP. TCP_RO.java import java.net.*; import java.io.*;

Basic Java Network Programming. CS211 July 30 th, 2001

TCP Networking in Java. Some reminders

CS 351 Week Advanced UNIX Programming: Rochkind, Marc J. 1. To learn about System Interprocess Communication (IPC). 2. To learn about Sockets.

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

Chapter 2 Application Layer

Chapter 4: Processes

Java Socket Application. Distributed Systems IT332

OBJECT ORIENTED PROGRAMMING

Chapter 2 Applications and

Transport layer protocols. Lecture 15: Operating Systems and Networks Behzad Bordbar

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Principles, Models, and Applications for Distributed Systems M

Object-Oriented Programming Design. Topic : Streams and Files

Socket 101 Excerpt from Network Programming

Chapter 2: outline. 2.1 principles of network applications. 2.6 P2P applications 2.7 socket programming with UDP and TCP

Networking Code CSCI 201 Principles of Software Development

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary

Distributed Programming - Sockets

Introduction to Network Programming using Java

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Communication Paradigms

C18: Network Fundamentals and Reliable Sockets

Networking with java (socket programming) a brief study

Chapter 4: Processes. Process Concept. Process State

AJP: Chapter 2 Networking: 18 marks

IT101. File Input and Output

Principles, Models, and Applications for Distributed Systems M

Distributed Systems COMP 212. Lecture 8 Othon Michail

Java Programming Language Mr.Rungrote Phonkam

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

Project Report On. Multiple Client and Server ChatApplication using Multicast in Java WindowBuilder

Unix Network Programming

Verteilte Systeme UE Important Code

Networking. Lecture 25 ish? COP 3252 Summer July 11, 2017

CS 2113 Software Engineering

Java for Interfaces and Networks

Redesde Computadores(RCOMP)

Redes de Computadores (RCOMP)

Chapter 2 Application Layer

Internet Protocol. Chapter 5 Protocol Layering. Juho Kim Graduate School of Information & Technology Sogang University

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

Lecture 3. Java Socket Programming. TCP, UDP and URL

Java Basics 5 - Sockets. Manuel Oriol - May 4th, 2006

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

Internet Technology 2/1/2016

JAVA 프로그래밍 II. Lab #7 (Networking) TCP Java Socket. TCP vs UDP 2017 학년도 1 학기 년봄학기 5/24/2017 박경신. Client. Server

Object-Oriented Programming in Java

Reliability is an issue. You cannot afford to have any of the data lost or re-ordered.

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Distributed Programming in Java

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Chapter 11. Application-Layer Elements Ports

Internet Protocol (IP) TCP versus UDP

UDP Sockets TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these features are not required, since they do

Java networking using sockets

Layer 4 - Transport Layer. What do we need a Transport Layer for? Transport Protocols in the TCP/IP Reference Model. The Transport Layer TCP and UDP

Topic 10: Network Programming

Transcription:

Java Networking (sockets) Rui Moreira Links: http://java.sun.com/docs/books/tutorial/networking/toc.html#sockets http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets_p.html Networking Computers on the Internet communicate via: n Transmission Control Protocol (TCP) connection-oriented protocol (like a telephone call synchronous communication) that provides a reliable flow of data between 2 computers (the order of packets is guaranteed) n User Datagram Protocol (UDP) connectionless protocol (like a mail letter asynchronous communication) that uses independent packets of data (datagrams) with no guarantees about arrival Rui Moreira 2 1

Networking (java.net package) Java programmers work at Application level, i.e., do not need to concern about the TCP/UDP layers because the java.net package : n hides the network details n provides system-independent communication Rui Moreira 3 Socket (IP + Port #) Sockets are uniquely identified on the entire Internet with 2 numbers: n Internet Address (IP address): 32-bit integer that uniquely identifies one machine (host / node) IPv6 increases the size of the IP address to 128 bits n Port of the socket: 16-bit integer called the port number (0-65553); In practice Java represents ports with 32-bit integers; Port numbers below 1024 are reserved for predefined services (e.g. telnet, SMTP, FTP) Rui Moreira 4 2

InetAddress The InetAddress class encapsulates the concept of an IP address and has methods for: n byte[] getaddress(): Returns the raw IP address of this InetAddress object; n String gethostaddress() Returns the IP address string in textual presentation; n String gethostname() Gets the host name for this IP address; n static InetAddress getlocalhost() Returns the local host; Rui Moreira 5 Sockets Server App ServerSocket (port #) Client App Socket (host, port #) port # Packet data TCP / UDP 1. Server registers Socket on a given port number (port #) 2. Gets the OutputStream associated with the socket 3. Writes the data 1. Client opens connection to given host & port # 2. Gets the InputStream associated with the socket 3. Reads the data Rui Moreira 6 3

Minimal TCP/IP Server import java.net.*; import java.io.*; public class SimpleServer { public static void main(string[] args){ ServerSocket serv = null; Socket sock = null; String srt = Hello net world! ; OutputStream os = null; DataOutputStream dos = null; // Register service on port 5432 serv = new ServerSocket(5432); catch (IOException ioe) { // Run listen/accept loop while (true) { // Wait for connection and then get stream sock = serv.accept(); os = sock.getoutputstream(); dos = new DataOutputStream(os); // Send string in UTF (machine independent) format dos.writeutf(str); dos.close(); os.close(); sock.close(); catch (IOException ioe) { Rui Moreira 7 Minimal TCP/IP Client import java.net.*; import java.io.*; public class SimpleClient { public static void main(string[] args){ String localhost_ip =(InetAddress.getLocalHost()).getHostAddress(); Socket sock = null; InputStream is = null; DataInputStream dis = null; // Open connection to localhost on port 5432 sock = new Socket(localhost_ip, 5432); // Get Input Stream is = sock.getinputstream(); dis = new DataInputStream(is); // Read string String str = new String(dis.readUTF()); System.out.println( SimpleClient main(): str = +str); // Close streams and connection dis.close(); is.close(); sock.close(); catch (IOException ioe) { Rui Moreira 8 4

Minimal UDP Server public class QuoteServer { public static void main(string[] args){ DatagramSocket socket = null; BufferedReader in = null; boolean morequotes = true; socket = new DatagramSocket(4445); in = new BufferedReader(new FileReader("one-liners.txt")); catch () { while (morequotes) { byte[] buf = new byte[256]; // receive request DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); // figure out response String dstring = null; if (in == null) dstring = new Date().toString(); else dstring = getnextquote(); buf = dstring.getbytes(); // send response to client at "address" and "port" InetAddress address = packet.getaddress(); int port = packet.getport(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); catch (IOException e) { morequotes = false; socket.close(); protected String getnextquote() { /* Next slide */ Rui Moreira 9 Minimal UDP Server (cont.) public class QuoteServer { protected String getnextquote() { String returnvalue = null; if ((returnvalue = in.readline()) == null) { in.close(); morequotes = false; returnvalue = "No more quotes. Goodbye."; catch (IOException e) { returnvalue = "IOException occurred in server."; return returnvalue; Rui Moreira 10 5

Minimal UDP Client import java.net.*; import java.io.*; import java.util.*; public class QuoteClient { public static void main(string[] args) throws java.io.exception { if (args.length!= 1) { System.out.println("Usage: java QuoteClient <hostname>"); return; // get a datagram socket DatagramSocket socket = new DatagramSocket(); // send request byte[] buf = new byte[256]; InetAddress address = InetAddress.getByName(args[0]); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445); socket.send(packet); // get response packet = new DatagramPacket(buf, buf.length); socket.receive(packet); // display response String received = new String(packet.getData()); System.out.println("Quote of the Moment: " + received); socket.close(); Rui Moreira 11 6