Java 13. Networking public class SumTest {

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
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.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Network/Socket Programming in Java Rajkumar Buyya.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
1 Inter-Process Communication: Network Programming using TCP Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
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 Sockets Programming
Java Socket programming. Socket programming with TCP.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
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.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
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.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
SOCKET PROGRAMMING.
Echo Networking COMP
Lecture 21 Sockets 1 (Not in D&D) Date.
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
Socket Programming Cal Poly Pomona Young CS380.
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Java Network Programming
„Networking”.
CSCD 330 Network Programming
UNIT-6.
Networking.
Outline Introduction Networking Basics Understanding Ports and Sockets
Programming TCP Sockets
CS18000: Problem Solving and Object-Oriented Programming
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Java 13. Networking public class SumTest { public static void main(String a1[]) { int a, b, sum; a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System.out.println("두수의 합은 " + sum + "입니다"); }

Client/Server Communications After the server accepts the connection, communication between server and client is conducted the same as for I/O streams. The server must be running when a client starts. The server waits for a connection request from a client. To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections. After a server socket is created, the server can use this statement to listen for connections. The client issues this statement to request a connection to a server.

Data Transmission through Sockets InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream();

A Client/Server Example Problem: Write a client to send data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle.

A Client/Server Example, cont.

A Client/Server Example, cont.

Server.java

Client.java

Client.java

ServerSocket Class - Constructor - Method 형식 Method Description ServerSocket(int port) throws IOException) 형식 Method Description Socket accept() throws IOException Listens for a connection to be made to this socket and accepts it. void close() throws IOException Closes this socket.

Socket Class - Constructor 형식 Socket(String hostName, int port) throws UnknownHostException, IOException 형식

Method Method Description OutputStream getOutputStream( ) throws IOException Returns an output stream for this socket. void close( ) throws IOException Closes this socket. InetAddress getInetAddress( ) Returns the address to which the socket is connected. InetAddress getLocalAddress( ) Gets the local address to which the socket is bound. int getPort( ) Returns the remote port number to which this socket is connected. int getLocalPort( ) Returns the local port number to which this socket is bound. InputStream getInputStream( ) throws IOException Returns an input stream for this socket.

TCP Socket Network Network Client Server Client Server

Excercises ServerSide.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 import java.io.*; import java.net.*; class ServerSide { public static void main(String args[]) throws Exception { int port = Integer.parseInt(args[0]); int times = Integer.parseInt(args[1]); ServerSocket ss = new ServerSocket(port); int i = 1; while( i <= times) { Socket s = ss.accept(); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); for(int j = 1 ; j <= 10 ; j++) dos.writeInt(j); s.close(); ++i; }

Excercises ClientSide.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 import java.io.*; import java.net.*; public class ClientSide { public static void main(String args[]) throws Exception { String server = args[0]; int port = Integer.parseInt(args[1]); Socket c = new Socket(server, port); InputStream is = c.getInputStream(); DataInputStream dis = new DataInputStream(is); for(int i=1 ; i <= 10 ; i++) { int j = dis.readInt(); System.out.println (j); } c.close();

SimpleServer.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 import java.io.*; import java.net.*; public class SimpleServer { public static void main(String[] arsg) { ServerSocket s=null; try { s = new ServerSocket(5432); } catch (IOException e) { e.printStackTrace(); } System.out.println("Server Running.. Waiting Connetion .."); while (true) { try { Socket s1 = s.accept(); OutputStream s1out = s1.getOutputStream(); BufferedWriterbw = new BufferedWriter( new OutputStreamWriter(s1out)); bw.write("Server Message\n"); bw.close(); s1.close(); }

SimpleServer.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 import java.io.*; import java.net.*; public class SimpleClient { public static void main(String[] args) { try { Socket s1 = new Socket("127.0.0.1", 5432); InputStreamir = s1.getInputStream(); BufferedReaderbr = new BufferedReader(new InputStreamReader(ir)); int a = 0; while((a = br.read())!= -1){ System.out.print((char)a); } ir.close(); s1.close(); catch (ConnectExceptionconnExc) { System.err.println("Could not connect ro the server"); catch (IOException e) {

Server Message