SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Network Programming CS 3331 Fall 2007
Outline ,[object Object],[object Object]
Socket Programming ,[object Object],[object Object],[object Object],[object Object],[object Object],logical connection sockets (end points)
Socket Programming (Cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Server vs. Client Sockets ,[object Object],[object Object],server socket connection request client socket
Server Sockets ,[object Object],Constructors ServerSocket(int port) ServerSocket(int port, int backlog) Methods Description accept()   Waits for a connection request and returns a Socket close() Stops waiting for requests from clients
Server Sockets (Cont.) ,[object Object],try { ServerSocket server = new ServerSocket(8888); while (true) { Socket incoming = server.accept();  // obtain a client socket // handle client request by reading from and writing to the socket … } } catch (IOException e) { // handle exception on creating a server socket }
Client Sockets ,[object Object],[object Object],[object Object],[object Object],Methods   Description getInputStream()  Returns an InputStream for receiving data getOutputStream() Returns an OutputStream to send data close()    Closes the socket connection
Client Sockets (Cont.) ,[object Object],try { Socket s = new Socket(“iguana.cs.utep.edu”, 8888); PrintWriter out = new PrinterWriter( new OutputStreamWriter( s.getOutputStream() )); BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream() )); // send and receive data by using out and in … in.close(); out.close();  s.close(); } catch (IOException e) { // handle exception … }
Example -- A Simple Echo Server import java.io.*; import java.net.*; public class EchoServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) {  Socket s = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader( s.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( s.getOutputStream())); << handle client by using in and out >> s.close(); } } catch (Exception e) { e.printStackTrace(); } } }
Echo Server (Cont.) << handle client by using in and out >>= out.print(“Hello! This is the Java EchoSever. ”); out.println(“Enter BYE to exit.”); out.flush(); String str = null; while ((str = in.readLine()) != null) { System.out.println(“Received: “ + str); out.println(“Echo: “ + str); out.flush(); if (str.trim().equals(“BYE”)) { break; } }
Testing Echo Server Testing with telnet client aspect% telnet localhost 8008 Trying 127.0.0.1 … Connected to localhost. Escape character is ‘^]’. Hello! This is the Java EchoServer. Enter BYE to exit. Hello? Echo: Hello? Where are you? Echo: Where are you? BYE Echo: BYE Connection to host lost.
A Simple Echo Client import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) { String host = (args.length > 0 ? host = args[0] : “localhost”; try { Socket socket = new Socket(host, 8008); BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( socket.getOutputStream())); << send and receive data by using in and out >> socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
Echo Client (Cont.) << send and receive data by using in and out >>= // send data to server for (int i = 1; i <= 10; i++) { System.out.println(“Sending: line “ + i); out.println(“line “ + i); out.flush(); } out.println(“BYE”); out.flush(); // receive data from server String str = null; while ((str = in.readLine()) != null) { System.out.println(str); }
Testing Echo Client << send and receive data by using in and out >>= sspect% java EchoClient Sending: line 1 Sending: line 2 … Sending: line 10 Hello! This is Java EchoServer. Enter BYE to exit. Echo: line 1 Echo: line 2 … Echo: line 10 Echo: BYE
Echo Server Revisited How to support multiple clients simultaneously? import java.io.*; import java.net.*; public class MultiEchoServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) {  Socket s = server.accept(); new ClientHandler(s).start(); } } catch (Exception e) { e.printStackTrace(); } } << class ClientHandler >> }
Multi Echo Server (Cont.) << class ClientHandler >>= private static class ClientHandler extends Thread { private Socket sock; public ClientHandler(Socket sock) { this.sock = sock; } public void run() { BufferedReader in = new BufferedReader(new InputStreamReader( sock.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( sock.getOutputStream())); out.println(“Hello! This is the Java EchoSever.Enter BYE to exit.”); out.flush(); String str = null; while ((str = in.readLine()) != null) { out.println(“Echo: “ + str); out.flush(); if (str.trim().equals(“BYE”)) { break; } }  } }
Exercise – Time Server Send the current local time to clients. (Use Calendar.getInstance().getTime() to get the current time.) public class TimeServer { }
Exercise – Time Client Take two arguments, host name and port number, and connect to the host to find its current time, e.g., java TimeClient iguana 8888 public class TimeClient { }
Outline ,[object Object],[object Object]
Remote Method Invocation ,[object Object],[object Object],[object Object],[object Object]
What’s RMI? ,[object Object],[object Object],[object Object],Client Server call return Stub Skeleton JVM JVM 6 5 4 3 2 1
Local vs. Remote Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Locating Remote Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Method   Description bind(name, obj)   Bind obj to name rebind(name, obj)   Bind obj to name even if already bound unbind(name)   Remove the binding lookup(url)   Return object bound to url list(url)   Return a list of all bindings
Writing RMI Programs ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Writing RMI Programs  (Cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Writing RMI Programs  (Cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example -- A Simple Time Server ,[object Object],[object Object],[object Object],[object Object],[object Object]
A Server Class, TimeServer import java.rmi.*; improt java.util.*; public class TimeServer extends java.rmi.server.UnicastRemoteObject  implements TimeService { public TimeServer() throws RemoteException {} public Date getTime() {  return Calendar.getInstance().getTime(); } public static void main(String [] args) { try { TimeServer server = new TimeServer(); Naming.rebind(&quot;TimeServer&quot;, server); } catch (Exception e) { e.printStackTrace(); } } }
A Client Class, TimeClient import java.rmi.*; improt java.util.*; public class TimeClient { public static void main(String [] args) { try { TimeService server =  (TimeService) Naming.lookup(&quot;rmi://localhost/TimeServer&quot;); System.out.println(server.getTime()); } catch (Exception e) { e.printStackTrace(); } } }
Compiling and Running ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Serialization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example (Cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example (Cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using Serialization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Spock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestSpock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestHoward Lewis Ship
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6Fiyaz Hasan
 
Protocol-Oriented Networking
Protocol-Oriented NetworkingProtocol-Oriented Networking
Protocol-Oriented NetworkingMostafa Amer
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
Reactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaReactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaHermann Hueck
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 
Python concurrency: libraries overview
Python concurrency: libraries overviewPython concurrency: libraries overview
Python concurrency: libraries overviewAndrii Mishkovskyi
 
The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212Mahmoud Samir Fayed
 
C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴명신 김
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sksureshkarthick37
 
Annotation processing and code gen
Annotation processing and code genAnnotation processing and code gen
Annotation processing and code genkoji lin
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Sergey Platonov
 
COScheduler In Depth
COScheduler In DepthCOScheduler In Depth
COScheduler In DepthWO Community
 

Was ist angesagt? (20)

Spock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestSpock: A Highly Logical Way To Test
Spock: A Highly Logical Way To Test
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6
 
Winform
WinformWinform
Winform
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Protocol-Oriented Networking
Protocol-Oriented NetworkingProtocol-Oriented Networking
Protocol-Oriented Networking
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Reactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaReactive Access to MongoDB from Scala
Reactive Access to MongoDB from Scala
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Python concurrency: libraries overview
Python concurrency: libraries overviewPython concurrency: libraries overview
Python concurrency: libraries overview
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212
 
C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Annotation processing and code gen
Annotation processing and code genAnnotation processing and code gen
Annotation processing and code gen
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
 
JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
 
COScheduler In Depth
COScheduler In DepthCOScheduler In Depth
COScheduler In Depth
 

Andere mochten auch

Week 1 student training intro
Week 1 student training introWeek 1 student training intro
Week 1 student training introJason Shoup
 
Week 2 student training customer care
Week 2 student training customer careWeek 2 student training customer care
Week 2 student training customer careJason Shoup
 
Best Industrial training report
Best Industrial training reportBest Industrial training report
Best Industrial training reportShivam Saxena
 
College Traders Student Programs
College Traders Student ProgramsCollege Traders Student Programs
College Traders Student Programssmbcapital
 
Ppt's project summer training in hcl cdc
Ppt's project summer training in hcl cdcPpt's project summer training in hcl cdc
Ppt's project summer training in hcl cdcsandy228
 
Project Report (Marketing) At Hcl
Project Report (Marketing) At HclProject Report (Marketing) At Hcl
Project Report (Marketing) At Hclguest4a0c325
 
Training And Development
Training And DevelopmentTraining And Development
Training And DevelopmentPremnath R
 
Fifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 MinutesFifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 Minutesglassfish
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

Andere mochten auch (11)

Week 1 student training intro
Week 1 student training introWeek 1 student training intro
Week 1 student training intro
 
Week 2 student training customer care
Week 2 student training customer careWeek 2 student training customer care
Week 2 student training customer care
 
Best Industrial training report
Best Industrial training reportBest Industrial training report
Best Industrial training report
 
College Traders Student Programs
College Traders Student ProgramsCollege Traders Student Programs
College Traders Student Programs
 
Ppt's project summer training in hcl cdc
Ppt's project summer training in hcl cdcPpt's project summer training in hcl cdc
Ppt's project summer training in hcl cdc
 
Project Report (Marketing) At Hcl
Project Report (Marketing) At HclProject Report (Marketing) At Hcl
Project Report (Marketing) At Hcl
 
Training ppt
Training pptTraining ppt
Training ppt
 
Training And Development
Training And DevelopmentTraining And Development
Training And Development
 
Fifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 MinutesFifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 Minutes
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Ähnlich wie Network

Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programmingashok hirpara
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client servertrilestari08
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Speed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsSpeed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsYakov Fain
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,AAlha PaiKra
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programmingAnung Ariwibowo
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopyayaria
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13Sasi Kala
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programgovindjha339843
 
Sockets in nach0s
Sockets in nach0sSockets in nach0s
Sockets in nach0snaniix21_3
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagarNitish Nagar
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxDhrumilSheth3
 

Ähnlich wie Network (20)

java sockets
 java sockets java sockets
java sockets
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client server
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Speed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsSpeed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSockets
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Sockets
SocketsSockets
Sockets
 
Java 1
Java 1Java 1
Java 1
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in program
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
 
Java rmi
Java rmiJava rmi
Java rmi
 
Ipc
IpcIpc
Ipc
 
Sockets in nach0s
Sockets in nach0sSockets in nach0s
Sockets in nach0s
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptx
 

Mehr von phanleson

Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Sparkphanleson
 
Firewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth FirewallsFirewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth Firewallsphanleson
 
Mobile Security - Wireless hacking
Mobile Security - Wireless hackingMobile Security - Wireless hacking
Mobile Security - Wireless hackingphanleson
 
Authentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless ProtocolsAuthentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless Protocolsphanleson
 
E-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server AttacksE-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server Attacksphanleson
 
Hacking web applications
Hacking web applicationsHacking web applications
Hacking web applicationsphanleson
 
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designHBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designphanleson
 
HBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - OperationsHBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - Operationsphanleson
 
Hbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBaseHbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBasephanleson
 
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibLearning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibphanleson
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streamingphanleson
 
Learning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLLearning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLphanleson
 
Learning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a ClusterLearning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a Clusterphanleson
 
Learning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark ProgrammingLearning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark Programmingphanleson
 
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your DataLearning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your Dataphanleson
 
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value PairsLearning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value Pairsphanleson
 
Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Sparkphanleson
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about LibertagiaHướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagiaphanleson
 
Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLphanleson
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Webphanleson
 

Mehr von phanleson (20)

Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Firewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth FirewallsFirewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth Firewalls
 
Mobile Security - Wireless hacking
Mobile Security - Wireless hackingMobile Security - Wireless hacking
Mobile Security - Wireless hacking
 
Authentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless ProtocolsAuthentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless Protocols
 
E-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server AttacksE-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server Attacks
 
Hacking web applications
Hacking web applicationsHacking web applications
Hacking web applications
 
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designHBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table design
 
HBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - OperationsHBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - Operations
 
Hbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBaseHbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBase
 
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibLearning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlib
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streaming
 
Learning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLLearning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQL
 
Learning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a ClusterLearning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a Cluster
 
Learning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark ProgrammingLearning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark Programming
 
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your DataLearning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your Data
 
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value PairsLearning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value Pairs
 
Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about LibertagiaHướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
 
Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XML
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Web
 

Network

  • 1. Network Programming CS 3331 Fall 2007
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. Example -- A Simple Echo Server import java.io.*; import java.net.*; public class EchoServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) { Socket s = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader( s.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( s.getOutputStream())); << handle client by using in and out >> s.close(); } } catch (Exception e) { e.printStackTrace(); } } }
  • 11. Echo Server (Cont.) << handle client by using in and out >>= out.print(“Hello! This is the Java EchoSever. ”); out.println(“Enter BYE to exit.”); out.flush(); String str = null; while ((str = in.readLine()) != null) { System.out.println(“Received: “ + str); out.println(“Echo: “ + str); out.flush(); if (str.trim().equals(“BYE”)) { break; } }
  • 12. Testing Echo Server Testing with telnet client aspect% telnet localhost 8008 Trying 127.0.0.1 … Connected to localhost. Escape character is ‘^]’. Hello! This is the Java EchoServer. Enter BYE to exit. Hello? Echo: Hello? Where are you? Echo: Where are you? BYE Echo: BYE Connection to host lost.
  • 13. A Simple Echo Client import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) { String host = (args.length > 0 ? host = args[0] : “localhost”; try { Socket socket = new Socket(host, 8008); BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( socket.getOutputStream())); << send and receive data by using in and out >> socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
  • 14. Echo Client (Cont.) << send and receive data by using in and out >>= // send data to server for (int i = 1; i <= 10; i++) { System.out.println(“Sending: line “ + i); out.println(“line “ + i); out.flush(); } out.println(“BYE”); out.flush(); // receive data from server String str = null; while ((str = in.readLine()) != null) { System.out.println(str); }
  • 15. Testing Echo Client << send and receive data by using in and out >>= sspect% java EchoClient Sending: line 1 Sending: line 2 … Sending: line 10 Hello! This is Java EchoServer. Enter BYE to exit. Echo: line 1 Echo: line 2 … Echo: line 10 Echo: BYE
  • 16. Echo Server Revisited How to support multiple clients simultaneously? import java.io.*; import java.net.*; public class MultiEchoServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) { Socket s = server.accept(); new ClientHandler(s).start(); } } catch (Exception e) { e.printStackTrace(); } } << class ClientHandler >> }
  • 17. Multi Echo Server (Cont.) << class ClientHandler >>= private static class ClientHandler extends Thread { private Socket sock; public ClientHandler(Socket sock) { this.sock = sock; } public void run() { BufferedReader in = new BufferedReader(new InputStreamReader( sock.getInputStream())); PrintWriter out = new PrinterWriter(new OutputStreamWriter( sock.getOutputStream())); out.println(“Hello! This is the Java EchoSever.Enter BYE to exit.”); out.flush(); String str = null; while ((str = in.readLine()) != null) { out.println(“Echo: “ + str); out.flush(); if (str.trim().equals(“BYE”)) { break; } } } }
  • 18. Exercise – Time Server Send the current local time to clients. (Use Calendar.getInstance().getTime() to get the current time.) public class TimeServer { }
  • 19. Exercise – Time Client Take two arguments, host name and port number, and connect to the host to find its current time, e.g., java TimeClient iguana 8888 public class TimeClient { }
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29. A Server Class, TimeServer import java.rmi.*; improt java.util.*; public class TimeServer extends java.rmi.server.UnicastRemoteObject implements TimeService { public TimeServer() throws RemoteException {} public Date getTime() { return Calendar.getInstance().getTime(); } public static void main(String [] args) { try { TimeServer server = new TimeServer(); Naming.rebind(&quot;TimeServer&quot;, server); } catch (Exception e) { e.printStackTrace(); } } }
  • 30. A Client Class, TimeClient import java.rmi.*; improt java.util.*; public class TimeClient { public static void main(String [] args) { try { TimeService server = (TimeService) Naming.lookup(&quot;rmi://localhost/TimeServer&quot;); System.out.println(server.getTime()); } catch (Exception e) { e.printStackTrace(); } } }
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.