Professional Documents
Culture Documents
Para Metros Interfaz Red!!
Para Metros Interfaz Red!!
Topics
The ServerSocket and the Socket Class The MulticastSocket and the DatagramPacket Class
2
The Internet
A global network of computers connected together in various ways Remains functional despite of diversity of hardware and software connected together
Possible through communication standards defined and conformed to Guarantee compatibility and reliability of communication
Each computer connected to the Internet has a unique IP address A 32-bit number used to uniquely identify each computer connected to the Internet
192.1.1.1 docs.rinet.ru
4
Why protocols?
Different types of communication occurring over the Internet Each type of communication requires a specific and unique protocol
Definition
Set of rules and standards that define a certain type of Internet communication Describes the following information:
Social protocol used in a telephone conversation Gives us confidence and familiarity of knowing what to do
6
Used to transfer HTML documents on the Web More general compared to HTTP Allows you to transfer binary files over the Internet
Both protocols have their own set of rules and standards on how data is transferred Java provides support for both protocols
7
HTTP protocol is used when you are providing Web content through an HTTP service Each computer on the Internet can provide a variety of services
Why Ports?
Definition:
Standard ports
The FTP service is located on port 21 The HTTP service is located on port 80
Client
Machine in need of some type of information Machine storing information and waiting to give it out
Server
Scenario:
12
Definitions:
Software abstraction for an input or output medium of communication Communication channels that enable you to transfer data through a particular port An endpoint for communication between two machines A particular type of network communication used in most Java network programming Java performs all of its low-level network communication through sockets
13
The java.net package Provides classes useful for developing networking applications Some classes in the package:
15
16
import java.net.*; import java.io.*; public class EchoingServer { public static void main(String [] args) { ServerSocket server = null; Socket client; try { server = new ServerSocket(1234); //1234 is an unused port number } catch (IOException ie) { System.out.println("Cannot open socket."); System.exit(1); } //continued...
17
while(true) { try { client = server.accept(); OutputStream clientOut = client.getOutputStream(); PrintWriter pw = new PrintWriter(clientOut, true); InputStream clientIn = client.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(clientIn)); pw.println(br.readLine()); } catch (IOException ie) {}}} }
18
19
20
import java.io.*; import java.net.*; public class MyClient { public static void main(String args[]) { try { /* Socket client = new Socket("133.0.0.1", 1234); */ Socket client = new Socket(InetAddress.getLocalHost(), 1234); //continued...
21
InputStream clientIn = client.getInputStream(); OutputStream clientOut = client.getOutputStream(); PrintWriter pw = new PrintWriter(clientOut, true); BufferedReader br = new BufferedReader(new InputStreamReader(clientIn)); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Type a message for the server: "); //continued...
22
pw.println(stdIn.readLine()); System.out.println("Server message: "); System.out.println(br.readLine()); pw.close(); br.close(); client.close(); } catch (ConnectException ce) { System.out.println("Cannot connect to the server."); } catch (IOException ie) { System.out.println("I/O Error."); } } }
23
Useful for applications that implement group communication IP addresses for a multicast group lies within the range 224.0.0.0 to 239.255.255.255
24
25
Should be a member of the multicast group by using the joinGroup method Use the send method Once done, can use the leaveGroup method
26
Used to deliver data through a connectionless protocol Problem: delivery of packets is not guaranteed Has six constructors
27
28
import java.net.*; public class ChatServer { public static void main(String args[]) throws Exception { MulticastSocket server = new MulticastSocket(1234); InetAddress group = InetAddress.getByName("234.5.6.7"); //getByName- returns IP address of given host server.joinGroup(group); boolean infinite = true; //continued
29
/* Continually receives data and prints them */ while(infinite) { byte buf[] = new byte[1024]; DatagramPacket data = new DatagramPacket(buf, buf.length); server.receive(data); String msg = new String(data.getData()).trim(); System.out.println(msg); } server.close(); } }
30
import java.net.*; import java.io.*; public class ChatClient { public static void main(String args[]) throws Exception { MulticastSocket chat = new MulticastSocket(1234); InetAddress group = InetAddress.getByName("234.5.6.7"); chat.joinGroup(group); String msg = ""; //continued...
31
System.out.println("Type a message for the server:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); msg = br.readLine(); DatagramPacket data = new DatagramPacket(msg.getBytes(), 0, msg.length(), group, 1234); chat.send(data); chat.close(); } }
32
Summary
33
Summary
34