Sockets
110 likes | 323 Vues
This guide explores client-server communication using Java sockets, where one computer acts as a server while others function as clients. With examples demonstrating socket creation, data streaming, and proper server-client interaction, you'll learn to set up communication effectively. The tutorial covers socket implementation, reading and writing data, managing exceptions, and the importance of threading to avoid blocking issues. Discover how to maintain robust connections and properly manage resources, ensuring smooth network operations. Enhance your Java networking skills and build reliable server-client applications.
Sockets
E N D
Presentation Transcript
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
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
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
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( );
Creating the server socket • import java.net.*; • int PORT = 5654; // Same number as before • ServerSocket ss = new ServerSocket(PORT); • Socket s = ss.accept( );
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!
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
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
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