Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prepared By: Eng. Ola M. Abd El-Latif May/2010.  In this Lab we will answer the most frequently asked questions about programming sockets in Java. "— Presentation transcript:

1 Prepared By: Eng. Ola M. Abd El-Latif May/2010

2  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  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 Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { System.out.println(e); } 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: (From now on, all our code will handle exceptions!)

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

6  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  On the client side, you can use the ObjectInputStream class 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  On the server side, you can use ObjectInputStream to 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  On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream or ObjectInputStream of java.io: ObjectOutputStream output; try { output = new ObjectOutputStream(clientSocket.getOutputStream()); output.writeObject(ObjectIWantToSend); } catch (IOException e) { System.out.println(e); }

10  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  On the server side: try { output.close(); input.close(); MyServer.close(); } catch (IOException e) { System.out.println(e); }

12  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  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  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


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

Similar presentations


Ads by Google