Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming in Java -First Step of Network Programming-

Similar presentations


Presentation on theme: "Socket Programming in Java -First Step of Network Programming-"— Presentation transcript:

1 Socket Programming in Java -First Step of Network Programming-

2 BufferedReader BufferedReader BufferedReader BufferedReader in = new BufferedReader( new InputStreamReader( )); BufferedReader in = new BufferedReader( new InputStreamReader( )); Byte to charatert to buffer for speed Byte to charatert to buffer for speed IP IP Port Port TCP/IP TCP/IP

3 IP IP Protocol Protocol Number Number Advertise Advertise

4 Class Socket Class Socket Class Socket Class Socket getInputStream() Returns an input stream for this socket. Getoutputsteram getInputStream() Returns an input stream for this socket. Getoutputsteram getInputStream getOutputStream() Returns an output stream for this socket getOutputStream() Returns an output stream for this socket getOutputStream

5 How to Open a Socket? -Client- Machine name is the machine you are trying to open a connection to(ex: ip address or workstation name), and PortNumber is the port (a number) on which the server you are trying to connect to is running. Machine name is the machine you are trying to open a connection to(ex: ip address or workstation name), and PortNumber is the port (a number) on which the server you are trying to connect to is running. When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root).

6 How to Open a Socket? -Client- These port numbers are reserved for standard services, such as email, FTP, and HTTP. These port numbers are reserved for standard services, such as email, FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023! When selecting a port number for your server, select one that is greater than 1,023!

7 How to Open a Socket? -Client- Socket MyClient; MyClient = new Socket("Machine name", PortNumber); MyClient = new Socket("Machine name", PortNumber);

8 How to Open a Socket? -Client- With exception handling, the code look like following: With exception handling, the code look like following: Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

9 How to Open a Socket? -Server- ServerSocket MyService; try { MyServerice = new ServerSocket(PortNumber); MyServerice = new ServerSocket(PortNumber); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

10 How to Open a Socket? -Server- 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. 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 { serviceSocket = MyService.accept(); serviceSocket = MyService.accept(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

11 How Do I Create an Input Stream? -Client- On the client side, you can use the DataInputStream class to create an input stream to receive response from the server: On the client side, you can use the DataInputStream class to create an input stream to receive response from the server: DataInputStream input; DataInputStream input; try { try { input = new DataInputStream(MyClient.getInputStream()); input = new DataInputStream(MyClient.getInputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

12 How Do I Create an Input Stream? -Client- The class DataInputStream allows you to read lines of text and Java primitive data types in a portable way. The class DataInputStream allows you to read lines of text and Java primitive data types in a portable way. It has methods such as read, readChar, readInt, readDouble, and readLine,. It has methods such as read, readChar, readInt, readDouble, and readLine,. Use whichever function you think suits your needs depending on the type of data that you receive from the server. Use whichever function you think suits your needs depending on the type of data that you receive from the server.

13 How Do I Create an Input Stream? -Server- On the server side, you can use DataInputStream to receive input from the client On the server side, you can use DataInputStream to receive input from the client DataInputStream input; DataInputStream input; try { try { input = new DataInputStream(serviceSocket.getInputStream()); input = new DataInputStream(serviceSocket.getInputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

14 How do I Create an Output Stream? -Client- On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io: On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io:

15 How do I Create an Output Stream? -Client- PrintStream output; try { try { output = new PrintStream(MyClient.getOutputStream()); } output = new PrintStream(MyClient.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

16 How do I Create an Output Stream? -Client- The class PrintStream has methods for displaying textual representation of Java primitive data types. The class PrintStream has methods for displaying textual representation of Java primitive data types. you may use the DataOutputStream you may use the DataOutputStream

17 How do I Create an Output Stream? -Client- DataOutputStream output; DataOutputStream output; try { try { output = new DataOutputStream(MyClient.getOutputStream()); output = new DataOutputStream(MyClient.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

18 How do I Create an Output Stream? -Client- The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The method writeBytes is a useful one. The method writeBytes is a useful one.

19 How do I Create an Output Stream? -Server- On the server side, you can use the class PrintStream to send information to the client. On the server side, you can use the class PrintStream to send information to the client. PrintStream output; PrintStream output; try { try { output = new PrintStream(serviceSocket.getOutputStream()); output = new PrintStream(serviceSocket.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

20 How do I Create an Output Stream? -Server- You can use the class DataOutputStream as mentioned You can use the class DataOutputStream as mentioned DataOutputStream output; DataOutputStream output; try { try { output = new DataOutputStream(serviceSocket.getOutputStream()); output = new DataOutputStream(serviceSocket.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

21 How Do I Close Sockets? -Client- You should always close the output and input stream before you close the socket. You should always close the output and input stream before you close the socket. try { try { output.close(); output.close(); input.close(); input.close(); MyClient.close(); MyClient.close(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

22 How Do I Close Sockets? -Server- try { try { output.close(); output.close(); input.close(); input.close(); serviceSocket.close(); serviceSocket.close(); MyService.close(); MyService.close(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

23 Examples -Client- When programming a client, you must follow these four steps: When programming a client, you must follow these four steps: 1.Open a socket. 2.Open an input and output stream to the Socket. the Socket. 3.Read from and write to the socket according to the server's protocol. according to the server's protocol. 4.Clean up. These steps are pretty much the same for all clients. The only step that varies is step three, since it depends on the server you are talking to. These steps are pretty much the same for all clients. The only step that varies is step three, since it depends on the server you are talking to. Echo Client Example Echo Client Example Echo Client Example Echo Client Example

24 Examples -Server- In following example, Basically, the echo server receives text from the client and then sends that exact text back to the client. In following example, Basically, the echo server receives text from the client and then sends that exact text back to the client. Echo Server Example Echo Server Example Echo Server Example Echo Server Example

25 Thank you…. Discussion & Comments


Download ppt "Socket Programming in Java -First Step of Network Programming-"

Similar presentations


Ads by Google