Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.

Slides:



Advertisements
Similar presentations
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Advertisements

Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Referring to Java API Specifications
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.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Network Read/Write. Review of Streams and Files java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Socket programming with UDP and TCP. Socket Programming with TCP Connection oriented – Handshaking procedure Reliable byte-stream.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Variable Scope Brackets, brackets…. brackets. Variable Scope /** * HelloWorld --- program that prints something to the screen. Blanca Polo */
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
Announcements Quiz 2 Grades Posted on blackboard.
Saravanan.G.
Io package as Java’s basic I/O system continue’d.
ECE5650: Network Programming
DBI Representation and Management of Data on the Internet.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Java Socket programming. Socket programming with TCP.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
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.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Esercizio RMI. Server import java.io.*; class Item implements Serializable { private String category, itemNumber, descr; private int value, cost, bidder,
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
SOCKET PROGRAMMING.
Java 13. Networking public class SumTest {
Threads in Java Two ways to start a thread
CSE 341, S. Tanimoto Java networking-
Block 15 Developing the Calculator application
CS1101: Programming Methodology Recitation 7 – Exceptions
Client-server Programming
Socket programming with TCP
„Networking”.
Operators Laboratory /11/16.
הרצאה 12: קבצים וחריגות (Exceptions)
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
class PrintOnetoTen { public static void main(String args[]) {
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Web Design & Development Lecture 8
Chapter 2: Application layer
Programming TCP Sockets
Presentation transcript:

Client/Server example

Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static final int PORT = 1301; // port out of the range of public static void main(String args[]) throws IOException { PrimeServer server = new PrimeServer(); server.go(); } public void go() throws IOException { Socket soc = null; sSoc = new ServerSocket(PORT);

while(true) { soc = sSoc.accept(); // blocks until a connection occurs PrintWriter pw = new PrintWriter( //creating an OutputStream object new OutputStreamWriter( soc.getOutputStream()),true); BufferedReader br = new BufferedReader( new InputStreamReader( soc.getInputStream())); int num = Integer.parseInt( br.readLine() ); pw.println( prime(num) ); pw.close(); br.close(); soc.close(); } String prime( int num ) { for(int i=2; i*i<= num; i++) if( num%i==0 ) return(num +" is not a primary number."); return(num +" is a primary number."); } } //close class

Client import java.net.*; import java.io.*; public class PrimeClient { public static final int PORT = 1301;// port out of the range of String hostName; Socket soc; public static void main(String args[]) throws IOException { //replace localhost =>args[0] or with url PrimeClient client = new PrimeClient("localhost"); client.go(); } public PrimeClient(String hostString) { this.hostName = hostString; } String readInput() throws IOException { BufferedReader in =new BufferedReader( new InputStreamReader(System.in)); return( in.readLine() ); }

public void go() throws IOException { soc = new Socket(hostName, PORT); BufferedReader ibr = new BufferedReader( new InputStreamReader( soc.getInputStream())); PrintWriter pw = new PrintWriter( new OutputStreamWriter( soc.getOutputStream()),true); System.out.println("************** Check Prime *************"); System.out.println("Enter a number."); pw.println( readInput() ); System.out.println(ibr.readLine()); ibr.close(); pw.close(); soc.close(); } } //close class