ClientServer Using Sockets Joe Komar 12142021 Komar Associates










![Datagram Server while(true) { byte[] buffer = new byte[10]; in. Packet = new Datagram. Datagram Server while(true) { byte[] buffer = new byte[10]; in. Packet = new Datagram.](https://slidetodoc.com/presentation_image_h2/066a7ce95081f1e5e1364a4108885a8d/image-11.jpg)





![Datagram Client System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); long start. Datagram Client System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); long start.](https://slidetodoc.com/presentation_image_h2/066a7ce95081f1e5e1364a4108885a8d/image-17.jpg)


























![Client Data Stream (Buffered) System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); Client Data Stream (Buffered) System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value();](https://slidetodoc.com/presentation_image_h2/066a7ce95081f1e5e1364a4108885a8d/image-44.jpg)



- Slides: 47
Client/Server Using Sockets Joe Komar 12/14/2021 Komar Associates 1
Client/Server Computing n n One computer carrying on a conversation with another One has a role as a “server” providing resources One has a role as a “client” using the resources provided Java has many classes for handling this situation 12/14/2021 Komar Associates 2
Sockets n n n Berkley sockets provide peer-to-peer over TCP/IP Introduced in 1981 for Unix-to-Unix interprocess communications (IPC) Software provided by all major vendors of operating systems 12/14/2021 Komar Associates 3
Sockets n Datagram Sockets u UDP (User Datagram Protocol) u No guarantees of quality of transmission u Programs at either end to own checking u Extremely fast with little overhead u Net. BIOS is an example u Often used for quick “discoveries” or acknowledgments 12/14/2021 Komar Associates 4
Sockets n Stream Sockets u Interface to TCP transport protocol u Session based service u Does flow control, packet reassembly, and connection maintenance u Guarantee sending of receiving of packets in proper order and quality u Slower than UDP 12/14/2021 Komar Associates 5
Sockets n Raw Sockets u Interface to lower-layer protocols such as IP network layer and the Internet Control Message Protocol (ICMP) u Used to test new protocols and access advanced features of existing protocols 12/14/2021 Komar Associates 6
Socket Address n IP address -- e. g. 140. 209. 64. 36 u can also be its name (komar. cs. stthomas. edu) n Port -- 16 -bit number u Many ports already commonly used and well known u HTTP uses 8000, Oracle uses 1521, etc. 12/14/2021 Komar Associates 7
Java’s Inet. Address Class n n n Encapsulates an IP address and creates an object with informational methods boolean equals(Object o) byte[] get. Address() static Inet. Address get. By. Name(String host) String get. Host. Name() 12/14/2021 Komar Associates 8
Datagram Server import java. net. *; import java. io. *; public class Server { public static void main(String[] args) { int sum = 0; String sum. String = ""; Datagram. Socket datagram. Socket = null; Datagram. Packet in. Packet = null; Datagram. Packet out. Packet = null; Inet. Address addr = null; 12/14/2021 Komar Associates 9
Datagram Server try { // create socket datagram. Socket = new Datagram. Socket(251); // wait for request packets System. out. println("Waiting for client requests"); // execute client requests 12/14/2021 Komar Associates 10
Datagram Server while(true) { byte[] buffer = new byte[10]; in. Packet = new Datagram. Packet(buffer, buffer. length); datagram. Socket. receive(in. Packet); buffer = in. Packet. get. Data(); addr = in. Packet. get. Address(); // addr for reply packet String my. Operation = new String(buffer, 0, in. Packet. get. Length()); 12/14/2021 Komar Associates 11
Datagram Server if (my. Operation. equals("increment")){ sum. String = String. value. Of(++sum); } else // perform set sum operation if(my. Operation. equals("set_sum")) { sum = 0; sum. String = String. value. Of(sum); System. out. println("Sum = " + sum. String); } 12/14/2021 Komar Associates 12
Datagram Server buffer = sum. String. get. Bytes(); out. Packet = new Datagram. Packet(buffer, sum. String. length(), addr, 250); datagram. Socket. send(out. Packet); } } catch (Exception e) { System. err. println("Closing Datagram. Socket"); if (datagram. Socket != null) datagram. Socket. close(); } } } 12/14/2021 Komar Associates 13
Datagram Client import java. net. *; import java. io. *; public class Client { public static void main(String args[]) { byte[] buffer = new byte[10]; String sum. String = ""; String my. Operation; Datagram. Packet in. Packet = null; Datagram. Packet out. Packet = null; 12/14/2021 Komar Associates 14
Datagram Client try { if(args. length != 2) { System. out. println("Usage java Client <host> <count>"); return; } 12/14/2021 Komar Associates 15
Datagram Client Datagram. Socket datagram. Socket = new Datagram. Socket(250); Inet. Address addr = Inet. Address. get. By. Name(args[0]); System. out. println("Setting sum to 0"); my. Operation = "set_sum"; buffer = my. Operation. get. Bytes(); out. Packet = new Datagram. Packet(buffer, my. Operation. length(), addr, 251); datagram. Socket. send(out. Packet); in. Packet = new Datagram. Packet(buffer, buffer. length); datagram. Socket. receive(in. Packet); buffer = in. Packet. get. Data(); sum. String = new String(buffer, 0, in. Packet. get. Length()); System. out. println("sum = " + sum. String); 12/14/2021 Komar Associates 16
Datagram Client System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); long start. Time = System. current. Time. Millis(); 12/14/2021 Komar Associates 17
Datagram Client for(int i = 0; i < count; i++) { my. Operation = "increment"; buffer = my. Operation. get. Bytes(); out. Packet = new Datagram. Packet(buffer, my. Operation. length(), addr, 251); datagram. Socket. send(out. Packet); in. Packet = new Datagram. Packet(buffer, buffer. length); datagram. Socket. receive(in. Packet); buffer = in. Packet. get. Data(); sum. String = new String(buffer, 0, in. Packet. get. Length()); } 12/14/2021 Komar Associates 18
Datagram Client long stop. Time = System. current. Time. Millis(); System. out. println("Avg Ping = " + ((stop. Time - start. Time) / (float)count) + " msecs"); System. out. println("Sum = " + sum. String); } catch(Exception e) { System. err. println(e); } } } 12/14/2021 Komar Associates 19
Datagram Output D: qm 490socketsdatagram>java Client 140. 209. 64. 36 35 Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation Setting sum to 0 sum = 0 Incrementing Avg Ping = 15. 714286 msecs Sum = 35 12/14/2021 Komar Associates 20
Stream Socket Classes n Socket class -- used by both server and client u eight constructors to identify address and port n Server. Socket class -- server-side only u n constructor completes when connection arrives and then accepts connection Socket. Impl interface -- for those who wan to write other socket implementations 12/14/2021 Komar Associates 21
Stream Sockets n Use the same “streams” we saw with using files u Data. Output. Stream, Data. Input. Stream u Buffered. Output. Stream, Buffered. Input. Stream u Buffered. Writer, Buffered. Reader n Server and Client each have a pair of streams for reading and writing to the socket 12/14/2021 Komar Associates 22
Server Buffered Stream Sockets import java. net. *; import java. io. *; public class Server { public static void main(String[] args) { Server. Socket server. Socket = null; Socket socket = null; byte[] read. Buffer = new byte[40]; // buffer for sockets transfers byte[] write. Buffer = new byte[40]; // buffer for sockets transfers int bytes. Read; // bytes read to/from buffer int sum = 0; // current sum value String sum. String = ""; // for byte to string conversion 12/14/2021 Komar Associates 23
Server Buffered Stream Sockets try { server. Socket = new Server. Socket(250); } catch (Exception e) { System. err. println("Exception Creating Socket"); } 12/14/2021 Komar Associates 24
Server Buffered Stream Sockets while (true) { try { // wait for connection then create streams System. out. println("Waiting for client connection"); socket = server. Socket. accept(); Buffered. Output. Stream ostream = new Buffered. Output. Stream(socket. get. Output. Stream()); Buffered. Input. Stream istream = new Buffered. Input. Stream(socket. get. Input. Stream()); 12/14/2021 Komar Associates 25
Server Buffered Stream Sockets while(true) { bytes. Read = istream. read(read. Buffer, 0, 40); String my. Operation = new String(read. Buffer, 0, bytes. Read); if(my. Operation. equals("increment")) sum. String = String. value. Of(++sum); else if(my. Operation. equals("set_sum")) { sum = 0; sum. String = String. value. Of(sum); System. out. println("Sum = " + sum. String); } 12/14/2021 Komar Associates 26
Server Buffered Stream Sockets write. Buffer = sum. String. get. Bytes(); ostream. write(write. Buffer, 0, sum. String. length()); ostream. flush(); } 12/14/2021 Komar Associates 27
Server Buffered Stream Sockets } catch (Exception e) { System. err. println(e); System. err. println("Closing Socket connection"); if (socket != null) try { socket. close(); server. Socket = new Server. Socket(250); } catch (IOException ex) {} } } 12/14/2021 Komar Associates 28
Client Buffered Stream import java. net. *; import java. io. *; public class Client { public static void main(String args[]) { byte[] buffer = new byte[10]; // buffer for sockets transfers int bytes. Read; // variable for bytes read String sum. String = ""; // variable for storing current sum String my. Operation; // variable for byte to string conversion 12/14/2021 Komar Associates 29
Client Buffered Stream try { if(args. length != 2) { System. out. println("Usage java Client <host> <count>"); return; } // Create socket connection System. out. println("Opening socket and creating streams"); String host = args[0]; Socket socket = new Socket(host, 250); Buffered. Output. Stream ostream = new Buffered. Output. Stream(socket. get. Output. Stream()); Buffered. Input. Stream istream = new Buffered. Input. Stream(socket. get. Input. Stream()); 12/14/2021 Komar Associates 30
Client Buffered Stream System. out. println("Setting sum to 0"); my. Operation = "set_sum"; buffer = my. Operation. get. Bytes(); ostream. write(buffer, 0, my. Operation. length()); ostream. flush(); bytes. Read = istream. read(buffer, 0, 10); sum. String = new String(buffer, 0, bytes. Read); System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); long start. Time = System. current. Time. Millis(); 12/14/2021 Komar Associates 31
Client Buffered Stream for(int i = 0; i < count; i++) { my. Operation = "increment"; buffer = my. Operation. get. Bytes(); ostream. write(buffer, 0, my. Operation. length()); ostream. flush(); bytes. Read = istream. read(buffer, 0, 10); sum. String = new String(buffer, 0, bytes. Read); } long stop. Time = System. current. Time. Millis(); System. out. println("Avg Ping = " + ((stop. Time - start. Time) / (float)count) + " msecs") System. out. println("Sum = " + sum. String); 12/14/2021 Komar Associates 32
Client Buffered Stream } catch(Exception e) { System. err. println(e); } } } 12/14/2021 Komar Associates 33
Buffered Stream Output D: qm 490socketsbuffered>java Client komar. cs. stthomas. edu 35 Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation Opening socket and creating streams Setting sum to 0 Incrementing Avg Ping = 15. 714286 msecs Sum = 35 12/14/2021 Komar Associates 34
Sockets Hands-On n n Copy the four files in \Nt 431publickomarsockets to your diskette Copy the Server. * files to a temporary directory on the hard drive of machine A Put the diskette in the drive of machine B Log onto the Alpha from machine A, type “show users first-letters-of-username”, and make note of the IP address shown 12/14/2021 Komar Associates 35
Sockets Hands-On n On machine A: u Open an MS-DOS window, change directory to where Server. * are, and run the Server. class program (java Server) n On machine B: u Open an MS-DOS window, change directory to the A: drive, type in: F java n Client Machine-A-IP-address 35 Note interaction messages on both machines 12/14/2021 Komar Associates 36
Server Data Stream (Buffered) import java. net. *; import java. io. *; public class Server { public static void main(String[] args) { Server. Socket server. Socket = null; Socket socket = null; int sum = 0; // current sum value String sum. String = ""; 12/14/2021 Komar Associates 37
Server Data Stream (Buffered) try { // create socket server. Socket = new Server. Socket(250); } catch (Exception e) { System. err. println("Error Creating Socket"); } 12/14/2021 Komar Associates 38
Server Data Stream (Buffered) while (true) { try { // wait for connection then create streams System. out. println("Waiting for client connection"); socket = server. Socket. accept(); Data. Output. Stream ostream = new Data. Output. Stream(new Buffered. Output. Stream( socket. get. Output. Stream())); Data. Input. Stream istream = new Data. Input. Stream( new Buffered. Input. Stream( socket. get. Input. Stream())); 12/14/2021 Komar Associates 39
Server Data Stream (Buffered) while(true) { String my. Operation = istream. read. UTF(); if(my. Operation. equals("increment")) sum. String = String. value. Of(++sum); else if(my. Operation. equals("set_sum")) { sum = 0; sum. String = String. value. Of(sum); System. out. println("Sum = " + sum. String); } ostream. write. UTF(sum. String); ostream. flush(); 12/14/2021 } Komar Associates 40
Server Data Stream (Buffered) } catch (Exception e) { System. err. println("Closing Socket connection"); if (socket != null) try { socket. close(); } catch (IOException ex) {} } } 12/14/2021 Komar Associates 41
Client Data Stream (Buffered) import java. net. *; import java. io. *; public class Client { public static void main(String args[]) { String sum. String = ""; // variable for storing current sum try { if(args. length != 2) { System. out. println("Usage java Client <host> <count>"); return; } 12/14/2021 Komar Associates 42
Client Data Stream (Buffered) System. out. println("Opening socket and creating streams"); String host = args[0]; Socket socket = new Socket(host, 250); Data. Output. Stream ostream = new Data. Output. Stream(new Buffered. Output. Stream( socket. get. Output. Stream())); Data. Input. Stream istream = new Data. Input. Stream(new Buffered. Input. Stream( socket. get. Input. Stream())); System. out. println("Setting sum to 0"); ostream. write. UTF("set_sum"); ostream. flush(); sum. String = istream. read. UTF(); 12/14/2021 Komar Associates 43
Client Data Stream (Buffered) System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); long start. Time = System. current. Time. Millis(); for(int i = 0; i < count; i++) { ostream. write. UTF("increment"); ostream. flush(); sum. String = istream. read. UTF(); } long stop. Time = System. current. Time. Millis(); System. out. println("Avg Ping = " + ((stop. Time - start. Time) / (float)count) + " msecs") System. out. println("Sum = " + sum. String); } catch(Exception e) { System. err. println(e); } } 12/14/2021 } Komar Associates 44
Data Stream (Buffered) Output D: qm 490socketsdata_buff>java Client komar. cs. stthomas. edu 35 Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation Opening socket and creating streams Setting sum to 0 Incrementing Avg Ping = 6. 285714 msecs Sum = 35 12/14/2021 Komar Associates 45
Sockets Assignment n Write a Server and Client Java program using buffered streams that does the following interaction: u The client sends an arithmetic operator (+ - / *) and two double values separated by commas ( *, 2. 5, 3. 6) u The server does the calculation indicated with the numbers sent and sends back the result u The client displays the result on the screen 12/14/2021 Komar Associates 46
Sockets Assignment n n n Have the client start up from the DOS prompt and accept one parameter for the IP address of the Server Prompt the user for the arithmetic operator and the two values Have the Server display its status (what it is doing) on the screen while it is running 12/14/2021 Komar Associates 47