Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).



Similar documents
Creating a Simple, Multithreaded Chat System with Java

Java Network. Slides prepared by : Farzana Rahman

Lesson: All About Sockets

INPUT AND OUTPUT STREAMS

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Building a Multi-Threaded Web Server

Network/Socket Programming in Java. Rajkumar Buyya

Network Communication

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Socket Programming in Java

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

What is an I/O Stream?

CS 1302 Ch 19, Binary I/O

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Division of Informatics, University of Edinburgh

WRITING DATA TO A BINARY FILE

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

Assignment 4 Solutions

JAVA - FILES AND I/O

AVRO - SERIALIZATION

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Socket-based Network Communication in J2SE and J2ME

Building a Java chat server

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

Socket programming. Complement for the programming assignment INFO-0010

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Java Programming: Sockets in Java

Network Programming using sockets

Files and input/output streams

Advanced Network Programming Lab using Java. Angelos Stavrou

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

Mail User Agent Project

Data Communication & Networks G

Programming in Java

CS506 Web Design and Development Solved Online Quiz No. 01

SSC - Communication and Networking Java Socket Programming (II)

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Java from a C perspective. Plan

Communicating with a Barco projector over network. Technical note

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Socket Programming. Announcement. Lectures moved to

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

public static void main(string[] args) { System.out.println("hello, world"); } }

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Intranet, Extranet, Firewall

The difference between TCP/IP, UDP/IP and Multicast sockets. How servers and clients communicate over sockets

!"# $ %!&' # &! ())*!$

CS 121 Intro to Programming:Java - Lecture 11 Announcements

An Overview of Java. overview-1

Decorator Pattern [GoF]

JAVA Program For Processing SMS Messages

D06 PROGRAMMING with JAVA

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

The Java Sound Internet Phone

Stream Classes and File I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

Input / Output Framework java.io Framework

Manual. Programmer's Guide for Java API

TITLE NETWORK PROGRAMMING (SECURED CLIENT-SERVER CHAT APPLICATION) A PROJECT REPORT PRESENTED ACHILE UGBEDE JOHN CE/2007/165

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

Socket Programming. A er learning the contents of this chapter, the reader will be able to:

IT6503 WEB PROGRAMMING. Unit-I

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

Event-Driven Programming

Network-based Applications. Pavani Diwanji David Brown JavaSoft

Contents. 9-1 Copyright (c) N. Afshartous

Chapter 3. Internet Applications and Network Programming

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

An Android-based Instant Message Application

Transparent Redirection of Network Sockets 1

Network Programming. Writing network and internet applications.

NETWORKING FEATURES OF THE JAVA PROGRAMMING LANGUAGE

DC60 JAVA AND WEB PROGRAMMING JUNE b. Explain the meaning of the following statement public static void main (string args [ ] )

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Multi-Client Multi-Instance Secured Distributed File Server

1 of 1 24/05/ :23 AM

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Object-Oriented Programming in Java

Abhijit A. Sawant, Dr. B. B. Meshram Department of Computer Technology, Veermata Jijabai Technological Institute

Java Fundamental Classes Reference

Serializing Data with Protocol Buffers. Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2014.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Report of the case study in Sistemi Distribuiti A simple Java RMI application

Quick Introduction to Java

Transcription:

Java Network Programming The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol). The DatagramSocket class uses UDP (connectionless protocol). The java.net.socket class represents a single side of a socket connection on either the client or server. In addition, the server uses the java.net.serversocket class to wait for connections from clients. The server creates a ServerSocket object and waits, blocked in a call to its accept() method, until a connection arrives. When a connection request arrives, the accept() creates a Socket object. The server uses this Socket object to communicate with the client. E.g. of a client: try { Socket server = new Socket("foo.bar.com", 2500); InputStream in = server.getinputstream(); OutputStream out = server.getoutputstream(); //sends a string PrintStream pout = new PrintStream(out); Pout.println("Hello!"); //Read a string DataInputStream din = new DataInputStream(in); String response = din.readline(); Server.close(); catch (IOException e) { System.out.println("Error connecting to host");

E.g. of a server: try { ServerSocket listener = new ServerSocket(2500); while (!finished) { Socket aclient = listener.accept(); //waits for a //connection InputStream in = aclient.getinputstream(); OutputStream out = aclient.getoutputstream(); //Read a string DataInputStream din = new DataInputStream(in); String request = din.readline(); //Write a string PrintStream pout = new PrintStream(out); pout.println("goodbye!"); aclient.close(); listener.close(); catch(ioexception e) {

For Multithreaded Servers: import java.net.*; import java.io.*; import java.util.*; public class TinyHttpd { public static void main(string argv[]) throws IOException { int port = Integer.parseInt(argv[0]); Serversocket ss = new Serversocket(port); while (true) new TinyHttpdConn(ss.accept()); //accept() returns a new socket to handle client //create a thread to service each web client request class TinyHttpdConn extends Thread { Socket sock; TinyHttpdConn(Socket s) { public void run() { try { //constructor sock = s; setpriority(norm_priority - 1); start();

Java I/O Facilities All fundamental I/O in Java is based on streams. A stream represents a flow of data with a writer at one end and a reader at the other. InputStream and OutputStream are the lowest level interface for all streams. These are abstract classes that define the basic functionality for reading and writing an unstructured sequence of bytes. All other streams are built on top of these two streams. These two streams are sub-classed to provide DataInputStream and DataOutputStream that let you read or write strings and primitive data types. BufferedInputStream, BufferedOutputStream, PrintStream. PrintWriter and the equivalent Reader (refer to java.sun.com/docs) E.g. DataInputStream dis = new DataInputStream(System.in); //read from std input String line = dis.readline(); //This wraps the std input stream in a DataInputStream //and readline() reads bytes upto '\n' To read an int: int i = dis.readint(); Or String text = dis.readline(); int i = Integer.parseint(text); Or String text = dis.readline(); int i = new Integer(text).intValue(); //This creates an Int wrapper and calls its intvalue() method

The DataOutputStream class provides write methods that correspond to the read methods in DataInputStream. e.g. writeint() Files Example: //read from stdin and write to a file DataInputStream dis = new DataInputStream(System.in); String s = dis.readline(); File out = new File("/tmp/foo.txt"); if (out.canwrite()) { FileOutputStream fos = new FileOutputStream(out); PrintStream pfos = new PrintStream(fos); pfos.println(s); Example //read data from a file and send out the data read via a socket File file = new F ile(args[0]); FileInputStream fis = new FileInputStream(file); DataInputStream data = new DataInputStream(fis); OutputStream out = sock.getoutputstream(); PrintStream pfos = new PrintStream(out); while (num < fis.available()) { String s = data.readline(); num += s.length(); pfos.println(s);