1 / 11

Sockets

Sockets. Clients and servers. Computers can communicate with one another in many ways The best approach is the client-server model One computer acts as a server The other computers (clients) connect to it

galya
Download Presentation

Sockets

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. Sockets

  2. Clients and servers • Computers can communicate with one another in many ways • The best approach is the client-server model • One computer acts as a server • The other computers (clients) connect to it • Think of it as a telephone call--client(s) have to know the phone number (IP address) of the server • Once the connection is made, there is no necessary difference between client and server

  3. Creating the client socket • import java.net.*; • Socket s; // This is a "client" socket • int PORT = 5654; • can be any number between 1024 and 9999 • s = new Socket("localhost", PORT); • or • s = new Socket("153.104.220.117", PORT); • use ipconfig to get the IP address of your computer

  4. Aside: Java I/O • Java I/O is very flexible but very complicated • Everything is done as a "stream" (a flow of data) • It's tricky to figure out how to set up a stream to do what you want • Once you have it figured out, it's "cookbook"--you can just do it the same way every time • I wanted to connect sockets, and to read and write lines

  5. Using the client socket • BufferedReader input = new BufferedReader( new InputStreamReader( s.getInputStream( ) ) ); • PrintStream output = new PrintStream(s.getOutputStream( ) ); • output.println("Hello, Server!"); • String response = input.readLine( );

  6. Creating the server socket • import java.net.*; • int PORT = 5654; // Same number as before • ServerSocket ss = new ServerSocket(PORT); • Socket s = ss.accept( );

  7. Using the server socket • BufferedReader input = new BufferedReader( new InputStreamReader( s.getInputStream( ) ) ); • PrintStream output = new PrintStream(s.getOutputStream( ) ); • String message = input.readLine( ); • output.println("Hello, Client!"); • This is just like using the client socket!

  8. Complications I • You have to do things in the proper order • The server must be started before the client • ss.accept( ) (to accept a call from a client) and input.readLine( ) (to get data) both block • You must be very careful to write only when the other program is reading • Proper use of Threads can solve some of these problems

  9. Complications II • There are many possible Exceptions to be caught • For simple programs, it's OK to just exit when an Exception occurs • Remember to close all your streams • The server socket must be closed, or it may persist "forever" • This will mess up later runs of the programs • The finally clause of a try statement is the best place to close the server socket

  10. Summary • Network communication is complicated, but it's a lot simpler in Java than in other languages • You have to be extremely careful to do things in the correct order (Threads can help) • Chapter 8 of your textbook explains all this in the context of a High Score Server • The textbook uses deprecated methods in setting up streams--use mine instead

  11. The End

More Related