Networking Code. Jeffrey Miller, Ph.D. CSCI 201L USC CSCI 201L

Similar documents
Creating a Simple, Multithreaded Chat System with Java

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

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

Lesson: All About Sockets

Java Network. Slides prepared by : Farzana Rahman

Socket-based Network Communication in J2SE and J2ME

Advanced Network Programming Lab using Java. Angelos Stavrou

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

Object-Oriented Programming in Java

Socket Programming in Java

Question R11.3. R11.3 How do you open a file whose name contains a backslash, like c:\temp\output.dat?

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

Division of Informatics, University of Edinburgh

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

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

Network/Socket Programming in Java. Rajkumar Buyya

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Assignment 4 Solutions

Input / Output Framework java.io Framework

Building a Multi-Threaded Web Server

Network Communication

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

Files and input/output streams

An Android-based Instant Message Application

Transparent Redirection of Network Sockets 1

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

Lecture J - Exceptions

JAVA Program For Processing SMS Messages

Socket Programming. Announcement. Lectures moved to

Manual. Programmer's Guide for Java API

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

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

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Transparent Redirection of Network Sockets 1

Building a Java chat server

Network Programming using sockets

Data Communication & Networks G

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

Socket programming. Complement for the programming assignment INFO-0010

Mail User Agent Project

TP N 10 : Gestion des fichiers Langage JAVA

13 File Output and Input

Corso di Reti di Calcolatori. java.net.inetaddress

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Java Programming: Sockets in Java

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

Course Intro Instructor Intro Java Intro, Continued

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

Database Access from a Programming Language:

ResellerPlus - Bulk Http API Specification. (This Document gives details on how to send messages via the Bulk HTTP API for the RouteSms SMPP System)

Communicating with a Barco projector over network. Technical note

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

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

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

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

What is an I/O Stream?

WRITING DATA TO A BINARY FILE

Network Programming. Writing network and internet applications.

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

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

How To Write A Mapreduce Program In Java.Io (Orchestra)

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

gomobi Traffic Switching Guide Version 0.9, 28 September 2010

Module 13 Implementing Java EE Web Services with JAX-WS

CS 121 Intro to Programming:Java - Lecture 11 Announcements

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

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

The MultiHttpServer A Parallel Pull Engine

Threads & Tasks: Executor Framework

Word Count Code using MR2 Classes and API

Multi-Client Multi-Instance Secured Distributed File Server

Logging in Java Applications

Stream Classes and File I/O

INPUT AND OUTPUT STREAMS

XII. Distributed Systems and Middleware. Laurea Triennale in Informatica Corso di Ingegneria del Software I A.A. 2006/2007 Andrea Polini

Mobila applikationer och trådlösa nät

Programming in Java

Lab 4: Socket Programming: netcat part

Using Files as Input/Output in Java 5.0 Applications

EVALUATION OF TOOLS FOR CYBER SECURITY

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

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

Remote Method Invocation

Remote Method Invocation (RMI)

Android Persistency: Files

Event-Driven Programming

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

WebSphere and Message Driven Beans

Introduction. How does FTP work?


Summer Internship 2013

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

Transcription:

Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/~CSCI201 USC CSCI 201L

Outline Server Networking Client Networking Program USC CSCI 201L 2/11

Server Software A server application is only able to serve requests that are addressed to the computer on which the server application is running A server program cannot listen to a port on another physical server So, the only data the server application needs is the port on which to listen The ServerSocket constructor only takes a port as a parameter Multiple networked applications can be running on the same computer as long as they are all using different ports Server Networking USC CSCI 201L 3/11

Multi-Threading with Networking Multi-threading is usually necessary with networking since there are two things that often are done at the same time The ability to send data The ability to receive data If sending and receiving are not performed in series, multi-threading will be needed Some applications may only need one program to send data then wait for a response that would not require multi-threading Server Networking USC CSCI 201L 4/11

Server Networking Example (no multi-threading) 1 import java.io.bufferedreader; 2 import java.io.ioexception; 3 import java.io.inputstreamreader; 4 import java.io.printwriter; 5 import java.net.serversocket; 6 import java.net.socket; 7 8 public class NetworkingServer { 9 10 public NetworkingServer() { 11 try { 12 System.out.println("Starting Server"); 13 ServerSocket ss = new ServerSocket(6789); 14 Socket s = ss.accept(); 15 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 16 PrintWriter pw = new PrintWriter(s.getOutputStream()); 17 18 String line = br.readline(); 19 System.out.println("Line Received: " + line); 20 String str = "CSCI 201"; 21 System.out.println("Sending: " + str); 22 pw.println(str); 23 pw.flush(); 24 pw.close(); 25 br.close(); 26 s.close(); 27 ss.close(); 28 } catch (IOException ioe) { 29 System.out.println("IOE: " + ioe.getmessage()); 30 } 31 } 32 public static void main(string [] args) { 33 new NetworkingServer(); 34 } 35 } Server Networking USC CSCI 201L 5/11

Flushing Operating systems try to optimize networking similar to how they optimize file I/O A buffer exists where data is written into before sending along the socket The buffer will not be transmitted over the network until it fills up unless we explicitly flush the data Never forget to flush! Server Networking USC CSCI 201L 6/11

Outline Server Networking Client Networking Program USC CSCI 201L 7/11

Client Software A client application is able to connect to any server application to which it has access, whether running on the same computer as the client or a different computer The client application needs both the IP address of the server and the port on which the server application with which it wants to communicate is listening Remember that multiple server applications can be running on the same computer as long as they are listening on different ports A Socket is the combination of the IP address and port number needed by the client Client Networking USC CSCI 201L 8/11

Client Networking Example (no multi-threading) 1 import java.io.bufferedreader; 2 import java.io.ioexception; 3 import java.io.inputstreamreader; 4 import java.io.printwriter; 5 import java.net.socket; 6 7 public class NetworkingClient { 8 9 public NetworkingClient() { 10 try { 11 System.out.println("Starting Client"); 12 Socket s = new Socket("localhost", 6789); 13 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 14 PrintWriter pw = new PrintWriter(s.getOutputStream()); 15 16 String str = "Line being sent"; 17 System.out.println("Sending: " + str); 18 pw.println(str); 19 pw.flush(); 20 String line = br.readline(); 21 System.out.println("Line Received: " + line); 22 pw.close(); 23 br.close(); 24 s.close(); 25 } catch (IOException ioe) { 26 System.out.println("IOE: " + ioe.getmessage()); 27 } 28 } 29 public static void main(string [] args) { 30 new NetworkingClient(); 31 } 32 } Client Networking USC CSCI 201L 9/11

Outline Server Networking Client Networking Program USC CSCI 201L 10/11

Program Write a multi-threaded chat program that allows multiple clients to communicate with each other in an asynchronous manner. The clients should communicate with a server program. C:>java ChatClient localhost 6789 hello Them: how are you? fine, and you? Them: good, thanks C:>java ChatClient localhost 6789 Them: hello how are you? Them: fine, and you? good, thanks Program USC CSCI 201L 11/11