1 / 15

Computer Networks Lab Programming sockets in Java

Computer Networks Lab Programming sockets in Java. Prepared By: Eng. Ola M. Abd El- Latif May/2010. Programming sockets in Java. In this Lab we will answer the most frequently asked questions about programming sockets in Java.

alton
Download Presentation

Computer Networks Lab Programming sockets in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Networks Lab Programming sockets in Java Prepared By: Eng. Ola M. Abd El-Latif May/2010

  2. Programming sockets in Java • In this Lab we will answer the most frequently asked questions about programming sockets in Java. • Then we will show some examples of how to write client and server applications.

  3. How do I open a socket? • If you are programming a client, then you would open a socket like this: Socket MyClient; MyClient = new Socket("Machine name", PortNumber); Machine name :is the machine you are trying to open a connection to PortNumber : is the port (a number) on which the server you are trying to connect to is running Note: that port numbers between 0 and 1,023 are reserved for privileged users , select one that is greater than 1,023!

  4. it is a good idea to handle exceptions In the previous example, we didn't make use of exception handling, however, it is a good idea to handle exceptions. The above can be written as: Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { System.out.println(e); } (From now on, all our code will handle exceptions!)

  5. Server Side: • If you are programming a server, then this is how you open a socket: ServerSocketMyServer; try { MyServer= new ServerSocket(PortNumber); } catch (IOException e) { System.out.println(e); }

  6. accept connections from clients • When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. Socket clientSocket = null; try { clientSocket = MyServer.accept(); } catch (IOException e) { System.out.println(e); }

  7. How do I create an input stream? • On the client side, you can use the ObjectInputStreamclass to create an input stream to receive response from the server: ObjectInputStream input; Object Data; try { input = new ObjectInputStream(clientSocket.getInputStream()); Data = input.readObject(); } catch (IOException e) { System.out.println(e); }

  8. How do I create an input stream? • On the server side, you can use ObjectInputStreamto receive input from the client: ObjectInputStream input; Object Data; try { input = new ObjectInputStream(MyServer.getInputStream()); Data = input.readObject(); } catch (IOException e) { System.out.println(e); }

  9. How do I create an output stream? • On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStreamorObjectInputStream of java.io: ObjectOutputStream output; try { output = new ObjectOutputStream(clientSocket.getOutputStream()); output.writeObject(ObjectIWantToSend); } catch (IOException e) { System.out.println(e); }

  10. How do I close sockets? • You should always close the output and input stream before you close the socket. • On The client Side.. try { output.close(); input.close(); clientSocket.close(); } catch (IOException e) { System.out.println(e); }

  11. How do I close sockets? • On the server side: try { output.close(); input.close(); MyServer.close(); } catch (IOException e) { System.out.println(e); }

  12. Notes • When programming a Server/Client, you must follow these four steps: • Open a socket. • Open an input and output stream to the socket. • Read from and write to the socket according to the server's/client’s protocol. • Clean up: • close the output stream • close the input stream • close the socket

  13. Example 1 • Now we will write two applications: a simple SMTP (simple mail transfer protocol) client, and a simple echo server. • SMTP client • Let's write an SMTP (simple mail transfer protocol) client -- one so simple that we have all the data encapsulated within the program. • Echo server • Basically, the server receives text from the client and then sends that exact text back to the client. • This is just about the simplest server you can write. Note that this server handles only one client. • Try to modify it to handle multiple clients using threads.

  14. Example 2 • In this Example, we use socket programming in Java to implement a pair of client and server that can achieve simple password verification. • The client will send a pair of username and password to the server and the server will verify whether the pair of username and password is legitimate or not. • This Example handle multiple clients using threads.

  15. Now;; Let’s Start challenging and fun (Client/Server) Socket Programming ......  ......

More Related