1 / 6

Sockets

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. A socket has two streams, one for input and one for output. In a client/server application:

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