Network Programming by Vlad Costel Ungureanu for Learn Stuff
Java Network Protocols 2
Java Network Protocols 3
Addresses Innet4Address (32-bit) 85.122.23.145 - numeric pentalog.com symbolic Innet6Address (128-bit) 2002:0:0:0:0:0:557a:1791 Types of addresses unicast multicast localhost (127.0.0.1) 4
Ports A port is a 16 bit number that uniquely identifies a process that provides network services Port number values: Possible values: 0 65535 Reserved values: 0 1023(usually for OS services or well known applications) 5
Server Client Model Server offers network services responds at a specific port needs to be able to handle concurrent requests Client initiates the conversation with the server needs to know the network address and the port of the server requests a specific service from the server 6
Sockets 7
Sockets Socket = software abstraction describing the edges of a connection TCP: Socket, ServerSocket UDP: DatagramSocket InetSocketAddress IP address, port host name, port 8
TCP Servers public class SimpleServer { public SimpleServer() throws IOException { ServerSocket serversocket = null ; try { serversocket = new ServerSocket(8100); while ( true ) { Socket socket = serversocket.accept(); // execute client request new ClientThread(socket).start(); } } catch ( IOException e) { System.err. println ( You should log this error! ); } finally { serversocket.close(); } } public static void main ( String [] args ) throws IOException { SimpleServer server = new SimpleServer (); }} 9
TCP Response class ClientThread extends Thread { private Socket socket = null ; public ClientThread ( Socket socket ) { this.socket = socket ; } public void run () { try { BufferedReader in = new BufferedReader ( new InputStreamReader (socket.getinputstream())); String request= in.readline (); PrintWriter out = new PrintWriter (socket.getoutputstream()); String response = " Hello " + request+ "!"; out.println (response ); out.flush (); } catch (IOException e) { System.err.println("Eroare IO \n" + e); } finally { try { socket.close(); } catch ( IOException e) { System.err. println (e); } }}} 10
TCP Client public class SimpleClient { public static void main (String[] args) throws IOException { Socket socket = null ; PrintWriter out = null ; BufferedReader in = null ; try { socket = new Socket ("127.0.0.1", 8080); out = new PrintWriter (socket.getoutputstream(), true); out.println ( Vlad ); out.close(); in = new BufferedReader ( new }} InputStreamReader(socket.getInputStream ())); raspuns = in.readline (); in.close(); } catch ( UnknownHostException e) { System.err.println (e); } finally { socket.close(); } 11
UPD Server DatagramSocket socket = new DatagramSocket(8200); byte buf[] = new byte[256]; DatgramPacket request = new DatagramPacket(buf, buf.length ); socket.receive(request ); InetAddress clientaddress= request.getaddress(); int clientport= request.getport(); String message= " Hello " + new String(clientPort.getData()); buf = message.getbytes(); DatagramPacket response= new DatagramPacket(buf, buf.length, clientaddress, clientport); socket.send(response); 12
UPD Client InetAddress serveraddress= InetAddress.getByName("127.0.0.1"); int serverport = 8200; DatagramSocket socket = new DatagramSocket(); byte buf[] = Vlad".getBytes (); DatagramPacket request = new DatagramPacket(buf, buf.length, serveraddress, serverport); socket.send(request); buf[] = new byte [256]; DatagramPacket response= new DatagramPacket(buf, buf.length ); socket.receive(response); System.out.println(new String(response.getData())); 13
Reading URL content try { URL url = new URL ( some_url_address ); InputStream in = url.openstream(); br = new BufferedReader (new InputStreamReader (in)); String linie ; while (( linie = br. readline ())!= null ) { System.out.println (linie); } } catch ( MalformedURLException e) { System. err. println ( You should log this error! ); } finally { if (br!= null) { br. close (); } } 14
Connecting to an URL try { URL url = new URL("http://localhost:8080/App/HelloWorld); URLConnection connection = url.openconnection(); connection.setdooutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); String param = URLEncoder.encode("Duke & World", "UTF-8"); out.write("string=" + param); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String response; while ((response = in.readline())!= null) { System.out.println(response); } in.close(); } catch (Exception e) { System. err. println ( You should log this error! ); } } 15
JNDI 16
RMI 17
Assignments Using Swing, Threads and Networking create a chat application that allows for users to send users to one another For simplicity use a Thread on the client that checks for new messages on the server Create a simple application that reads the content of a site and extracts all the domain related links. Extend the application to recursively follow links and creates a site map. 18
THANK YOU! Vlad Costel Ungureanu ungureanu_vlad_costel@yahoo.com This is a free course from LearnStuff.ro not for commercial use 19