Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tcp sockets

856 views

Published on

TCP Sockets

Published in: Education
  • Be the first to comment

Tcp sockets

  1. 1. TCP Sockets A socket is a connection between two hosts. It can perform seven basic operations: • Connect to a remote machine • Send data • Receive data • Close a connection • Bind to a port • Listen for incoming data • Accept connections from remote machines on the bound port
  2. 2. Client Sockets • The program creates a new socket with a Socket( ) constructor. • The socket attempts to connect to the remote host. • Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other. • This connection is full-duplex; both hosts can send and receive data simultaneously. • What the data means depends on the protocol; different commands are sent to an FTP server than to an HTTP server. There will normally be some agreed-upon hand-shaking followed by the transmission of data from one to the other. • When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP 1.0, require the connection to be closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.
  3. 3. Constructors Constructor creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host
  4. 4. Continued.. public Socket(String host, int port) throws UnknownHostException, IOException • public Socket(String host, int port, InetAddress interface, int localPort) throws IOException • public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException if 0 is passed for the local port number, java randomly selects port number between 1024 and 65,535.
  5. 5. Getting Information About a Socket • public InetAddress getInetAddress( ) • public int getPort( ) • public int getLocalPort( ) • public InetAddress getLocalAddress( ) public String toString( ) (Object Method)
  6. 6. Sockets for Servers • Java provides a ServerSocket class to allow programmers to write servers. • A ServerSocket runs on the server and listens for incoming TCP connections. • Each ServerSocket listens on a particular port on the server machine. • When a client Socket on a remote host attempts to connect to that port, the server wakes up, negotiates the connection between the client and the server, and opens a regular Socket between the two hosts.
  7. 7. • The operating system stores incoming connection requests addressed to a particular port in a first-in, first-out queue. • The default length of the queue is normally 50, though this can vary from operating system to operating system.
  8. 8. The basic life cycle of a server 1. A new ServerSocket is created on a particular port using a ServerSocket( ) constructor. 2. The ServerSocket listens for incoming connection attempts on that port using its accept( ) method. 3. accept( ) blocks until a client attempts to make a connection, at which point accept( ) returns a Socket object connecting the client and the server. 4. Socket's getInputStream( )method, getOutputStream( ) method, are called to get input and output streams that communicate with the client. 5. The server and the client interact according to an agreed-up on protocol until it is time to close the connection. 6. The server, the client, or both close the connection. 7. The server returns to step 2 and waits for the next connection
  9. 9. Constructors • public ServerSocket(int port) throws IOException, BindException • public ServerSocket(int port, int queueLength)throws IOException, BindException • public ServerSocket(int port, int queueLength,InetAddress bindAddress) throws IOException • An IOException when creating a ServerSocket almost always means one of two things. – Either another server socket is already using the requested port, – or you're trying to connect to a port from 1 to 1023 on Unix without root (superuser) privileges.
  10. 10. get Methods • public InetAddress getInetAddress( ) • public int getLocalPort( )

×