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



Similar documents
Lesson: All About Sockets

Advanced Network Programming Lab using Java. Angelos Stavrou

Creating a Simple, Multithreaded Chat System with Java

Network/Socket Programming in Java. Rajkumar Buyya

Division of Informatics, University of Edinburgh

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

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

Socket Programming in Java

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

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

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Assignment 4 Solutions

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

Network Communication

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

INPUT AND OUTPUT STREAMS

Network Programming using sockets. Distributed Software Systems Prof. Sanjeev Setia

Java Network. Slides prepared by : Farzana Rahman

An Android-based Instant Message Application

Corso di Reti di Calcolatori

JAVA - FILES AND I/O

Socket-based Network Communication in J2SE and J2ME

What is an I/O Stream?

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

Stream Classes and File I/O

Files and input/output streams

Building a Multi-Threaded Web Server

Mail User Agent Project

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

Event-Driven Programming

Communicating with a Barco projector over network. Technical note

B.Sc (Honours) - Software Development

Corso di Reti di Calcolatori. java.net.inetaddress

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

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

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

Network Programming using sockets

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

Threads in der Client/Server-Programmierung mit Java

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

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

WRITING DATA TO A BINARY FILE

Manual. Programmer's Guide for Java API

Socket Programming. Announcement. Lectures moved to

Programming in Java

13 File Output and Input

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

Different types of resources are specified by their scheme, each different pattern is implemented by a specific protocol:

Simple Java I/O. Streams

Local Area Networks; Ethernet

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

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

Course Intro Instructor Intro Java Intro, Continued

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

Data Communication & Networks G

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

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

An Overview of Java. overview-1

Sophos Mobile Control Network Access Control interface guide

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

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

Liferay, Alfresco, SSO and LDAP Full Integration

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

Object-Oriented Programming in Java

Overview of Web Services API

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

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Lecture J - Exceptions

NetCom SMS Bedrift API Technical Documentation

Design of Cloud based Instant Messaging System on Android Smartphone using Internet

Building a Java chat server

Distributed Applications - Verteilte Anwendungen

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

Input / Output Framework java.io Framework

Java Programming: Sockets in Java

D06 PROGRAMMING with JAVA

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

Apéndice C: Código Fuente del Programa DBConnection.java

AVRO - SERIALIZATION

Android Persistency: Files

Socket programming. Complement for the programming assignment INFO-0010

Network Programming. CS 282 Principles of Operating Systems II Systems Programming for Android

Getting Started with the Internet Communications Engine

Logging in Java Applications

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

Tutorial for Creating Resources in Java - Client

MSDG Services Integration Document Draft Ver 1.2

Mobila applikationer och trådlösa nät

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

European Access Point for Truck Parking Data

Module 13 Implementing Java EE Web Services with JAX-WS

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

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

TP N 10 : Gestion des fichiers Langage JAVA

public class demo1swing extends JFrame implements ActionListener{

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

How to use the Eclipse IDE for Java Application Development

Introduktion til distribuerede systemer uge 37 - fil og webserver

Transcription:

Introduction to Java Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233 ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 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 are used to represent the connection between a client program and a server program. The java.net package provides two classes--socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively. Server p o r t Listen Connection request p o r t Client Server p o r t Listen Connection p o r t Client ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 2

Reading from and Writing to a Socket in Java Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then, how the client can send data to and receive data from the server through the socket.so, Download the code. Run the code. Observer the code. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 3

EchoClient package example; import java.io.*; import java.net.*; public class EchoClient { Socket echosocket = null; PrintWriter out = null; BufferedReader in = null; public EchoClient() { // TODO Auto-generated constructor stub // Create a new socket for the client echosocket = new Socket("localhost", 8080); // OutputStream to send messages to the Server out = new PrintWriter(echoSocket.getOutputStream(), true); // InputStream to get messages from the Server in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: localhost."); System.exit(1); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 4

EchoClient public void run() { // Create a buffer reader for the user input BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String userinput = ""; System.out.println(in.readLine()); while (!userinput.equals("bye")) { userinput = stdin.readline(); out.println(userinput); System.out.println("echo: " + in.readline()); catch (IOException e) { // TODO Auto-generated catch block e.printstacktrace(); // Housekeeping out.close(); in.close(); stdin.close(); echosocket.close(); catch (IOException e) { // TODO Auto-generated catch block e.printstacktrace(); public static void main(string[] args) throws IOException { EchoClient echoclient = new EchoClient(); echoclient.run(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 5

EchoServer package example; import java.io.*; import java.net.*; public class EchoServer { private ServerSocket server; public EchoServer(int portnum) { // Create new socket that listen any new requests server = new ServerSocket(portnum); catch (Exception err) { System.out.println(err); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 6

EchoServer public void run() { System.out.println("EchoServer is started!"); // Accept is a blocking operation while (true) { // Get client socket Socket client = server.accept(); BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter w = new PrintWriter(client.getOutputStream(), true); w.println("welcome to the Java EchoServer. Type 'bye' to close."); String line; // Actual Communication with the client do { line = r.readline(); if (line!= null) w.println(line); while (!line.trim().equals("bye")); client.close(); catch (Exception err) { System.err.println(err); public static void main(string[] args) { EchoServer s = new EchoServer(8080); s.run(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 7

Task Create multithreaded chat room based on the below protocol. Download the template code and follow the TODO comments inside the template code. C L I E N T S E R V E R C L I E N T Broadcast ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 8