Title: 1 Java networking
1???? ?????? ??????
- ????? 1Java networking
- ??????? ?"? ??? ?????, ???? ???? ???????? ????
????? ? - http//www.cs.biu.ac.il/freskom1/AlgProg2/Main.ht
m - (????? ??? ???? 3 Networking Issues in Java)
2????????????
- ???????
- ???? ????, ????? ?????? (banetworks2008_at_gmail.com
) - ?????, ?????
- ?? ????? ????? ?????? ?????? ? zoombut.
- ????? ?????? ?????? ????? ?? ????? ????? ?? ?????
(?????). - ?? ?? ????? ????? ?????? ????? ????? ????? ??
????????, ??? ????? ????? ?????? ?????. ?????
??????? ?????? ??????? ????? ????? ?????? ????
?????? please ask in zoombut" - ???? ???? ??? ???, 1300-1400 (?????? ???????
?????? ????? ?????? ?? ????) - ??? ??????
- http//www.cs.biu.ac.il/89-350/
- ???????
- 6 ??????? (???? ?????) ??????? 10 ?????? ?????.
- ????? ????? ??? 5 ???????? ?????? ?????.
- ???????
- ??????? (??????) ?????? 10 ?????? ????? ?????
????? - ????? ? 2 ????? ??????
- ??? ????? Java
3Addressing IP address and Port
- Sending to application (process) on remote
computer - Identify source, dest. computers by IP (Internet
Protocol) address - Include source and destination port numbers
- Ports identify the application (process)
- 16 bit number
- Well-known ports 0..1023 are reserved ports
- E.g. HTTP 80, DNS 53,
- Custom Use gt 1024
- Two main transport protocols TCP and UDP
protocols - Both use ports to identify destination process,
but differently
4Ports and Processes TCP vs. UDP
10.0.2.83
10.0.2.177
TCP Client Appl., port 2222
TCP Server Appl., port 80
TCP
TCP
UDP Server Appl., port 53
UDP Client Appl., port 4444
UDP
UDP
Whats the difference? Add another client to see
5Ports and Processes TCP vs. UDP
10.0.1.1
10.0.2.83
TCP Client Appl., port 1111
TCP
UDP Client Appl., port 2222
UDP
TCP Server process for (10.0.1.1,1111,80)
TCP
TCP Server process for (10.2.2.2,3333,80)
10.2.2.2
UDP Server Appl., port 5555
UDP
TCP Client Appl., port 3333
TCP
UDP Client Appl., port 4444
UDP same server process handles all requests to
same port
UDP
6TCP vs UDP
- TCP Transmission Control Protocol
- Connection - based
- Reliable (arrival, ordering)
- Transmission guaranteed, or error is reported.
- Communication in input / output streams.
- Example
- HTTP, FTP, SMTP, TELNET,
- UDP User Datagram Protocol
- Not connection based
- Non Reliable.
- Communication with Datagrams (chunks of data)
- The order of Datagrams is not guaranteed.
- Example
- Radio, Clock Server, Ping, Video stream.
7TCP
8Sockets
- Socket End point of a two-way communication link
between two programs running on the network. - Socket is a software abstraction to represent the
terminals of a connection. - A socket is bound to a port number so that the
TCP layer can identify the application that data
is destined to be sent. - Two stream-based Socket classes
- ServerSocket For server
- On connection returns a new Socket
- Socket For client
- Have getInputStream() and getOutputStream()
functions
9Socket Connection for Client
- Basic program flow for Client
- Open Socket
- Open an input stream and output stream to the
socket - Read from and write to the stream according to
the servers protocol - Close the streams
- Close the socket
- Only step 3 differs from client to client,
depending on the server
10Socket Client Example
- import java.io.
- import java.net.
- public class SocketClient
- public static void main(Stringargs) throws
IOException - Socket socket null PrintWriter out null
BufferedReader in null String host
"localhost" - try
- socket new Socket(localhost,7777) //
Port number 7777 - out new PrintWriter(socket.getOutputStream(),
true) - in new BufferedReader(new
InputStreamReader(socket.getInputStream()))
- // client code..
-
- catch (UnknownHostException e)
- System.err.println("Dont know host "host)
System.exit(-1) -
- catch (IOException e)
- System.err.println("Cannot get IO for
connection to "host) System.exit(-1) -
- finally
- // close connection (Order important)
11Socket Connection for Server
- Basic program flow for Server
- Create a ServerSocket
- Call ServerSocket.accept() to get a Socket
connection - Open input stream and output stream to that
socket - Read from and write to streams according to the
Protocol - Close the streams
- Close the socket
- ( Optional Return to 2 to get another connection
) - For Server allowing multiple connections, 3-6
must be in another thread
12Socket Server Example
- import java.net.
- import java.io.
- public class SocketServer
-
- public static void main(Stringargs) throws
IOException - ServerSocket serverSocket null
PrintWriter out null BufferedReader in null - try
- serverSocket new ServerSocket(4444)
-
- catch (IOException e)
- System.err.println("Cannot listen on port
4444") System.exit(-1) - Socket clientSocket null
- try
- clientSocket serverSocket.accept()
- out new PrintWriter(clientSocket.getOutputStr
eam(),true) - in new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()
)) -
- // server code here.
-
13Supporting Multiple Clients
- In the Server Side
- while (true)
- accept a connection
- create a thread to deal with the client
14Server Thread - MultiServerThread
- import java.net.
- import java.io.
- public class MultiServerThread extends Thread
- private Socket socket null
- public MultiServerThread(Socket socket)
- this.socket socket
-
- public void run()
- BufferedReader in null PrintWriter out
null - try
- in new BufferedReader(new
InputStreamReader(socket.getInputStream())
) - out new PrintWriter(socket.getOutputStr
eam(),true) - String inputLine
- while ((inputLinein.readLine())!null)
- // processing code here...
-
-
- catch (IOException e)
- e.printStackTrace()
15Server Class - MultiServer
- import java.net.
- import java.io.
- public class MultiServer
- public static void main(Stringargs) throws
IOException - ServerSocket serverSocket null
- boolean listening true
- try
- serverSocket new ServerSocket(4444)
- catch (IOException e)
- System.err.println("Could not listen on port
4444") - System.exit(-1)
-
- while (listening)
- new MultiServerThread(serverSocket.accept()).st
art() - serverSocket.close()
-
-
16UDP
17Identifying a Machine
- import java.net.
- public class WhoAmI
- public static void main(String args) throws
Exception - if(args.length ! 1)
- System.err.println("Usage WhoAmI
MachineName") - System.exit(1)
-
- InetAddress a InetAddress.getByName(args0)
- System.out.println(a)
-
-
- To run
- java WhoAmI myMachinemyMachine/127.0.0.1
18Remote Connections
- Identifying a Machine
- IP (Internet Protocol) adress
- DNS (Domain Name System) www.cs.biu.ac.il
- Dotted Quad Form 123.255.28.120
- InetAddress iaInetAddress.getByName()
- Local Host (for testing)
- All these three forms connect to local host
- InetAddress iaInetAddress.getByName(null)
- InetAddress iaInetAddress.getByName(localhost)
- InetAddress iaInetAddress.getByName(127.0.0.1)
- InetAddress iaInetAddress.getLocalHost()
19UDP communication
- UDP Delivers independent packages whose arrival
and order of arrival is not guaranteed. - Packets sent by UDP protocol are called
Datagrams. - DataGram Is an independent, self-contained
message sent over the network whose arrival, and
content are not guaranteed. - Classes for UDP connection
- DatagramPacket
- DatagramSocket
- MulticastSocket
20Example UDPServer
- import java.io.
- import java.net.
- import java.util.
- public class UDPServerExample
- protected DatagramSocket socket null
protected BufferedReader in null protected
boolean moreQuotes true - public void run() throws IOException
- socket new DatagramSocket(4445)
- try
- byte buf new byte256
- // Request
- DatagramPacket packet new
DatagramPacket(buf,buf.length) - socket.receive(packet)
- // generate Response
- String response Response
- buf response.getBytes()
- // Send the response
- InetAddress address
packet.getAddress() - int port packet.getPort()
- packet new DatagramPacket(buf,buf.leng
th,address,port)
21Example UDPClient
- import java.io.
- import java.net.
- import java.util.
- public class UDPClientExample
- public static void main(Stringargs) throws
IOException - try
- DatagramSocket socket new DatagramSocket()
- byte bufnew byte256
- InetAddress address InetAddress.getByName(10
.0.2.83) - DatagramPacket packet
- new DatagramPacket(buf,buf.length,address,4445
) - socket.send(packet)
- packetnew DatagramPacket(buf,buf.length)
- socket.receive(packet)
- String receivednew String(packet.getData())
- System.out.println("Received "received)
- finally