1 / 6

Sockets

This article covers the concept of sockets in Java and their role in establishing TCP/IP connections. A socket is created on both the client and server ends of a connection, facilitating bi-directional communication through input and output streams. Topics include socket creation on a specified port, waiting for client connections, and code examples for both client and server applications. You'll learn how to implement a basic echo server, which responds to client messages until "goodbye" is received, and how to test it using Telnet.

shay-kim
Télécharger la présentation

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. A socket is an object that encapsulates a TCP/IP connection • There is a socket on both ends of a connection, the client side and the server side. Sockets

  2. A socket has two streams, one for input and one for output. • In a client/server application: • the client’ output stream is connected to the server’s input stream • the server’s output stream is connected to the client’s output stream • Typical scenerio: • Server program creates a socket at a certain port and waits until a client • requests a connection • Client program creates a socket and attempts to make a connection • Once the connection is established the client and server communicate • Client closes connection.

  3. A server application waits for the client to connect on a • certain port. We choose 8888 • To listen for incoming connections, use a server socket • To construct a server socket, provide the port number • ServerSocket server = new ServerSocket(8888); • Use the accept method to wait for client connection: • Socket s = server.accept(); • //accept() will wait until a connection is established .. Java will • notify when this happens • Now can open input/output to socket: • s.getInputStream provides reference to • the socket’s input stream A Server Program

  4. Syntax to create a socket in a Java program • Socket s = new Socket(hostname, portnumber); • Code to connect to the HTTP port of server, java.sun.com • final int HTTP_PORT = 80; • Socket s= new Socket("java.sun.com",HTTP_PORT); • again, now access this sockets input and output stream • InputStream in = s.getInputStream() • OutputSteam out = s.getOutputSTream • Client socket should close when done: • s.close(); • s. Client Program

  5. Let’s look at a Server program which ‘echo’s all text Receives until ‘goodbye’ is received. … we’ll test it with Telnet (localhost , port=55555) netSEx5.java

  6. Now, a client to go with it………………

More Related