Sockets : Sockets are basically used to perform InterProcess Communication (IPC) Message destinations are specified as socket addresses;

each socket address is a communication identifier that consists of a port number and an Internet address.
In Simple words socket is the basic communication interface between networked computers. Sockets allow you the programmer to treat the network connection as you would any other I/O. In Java, sockets are the lowest level of network coding.

Socket programming using Stream communication: The stream communication protocol is known as TCP (transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions. Requirements to establish a socket connection 1.Yyou need a host to connect to. We may or may not run the client(s) and the server on the same machine.. 2.TCP/IP port that the program is going to be communicating on. TCP/IP uses ports because it is assumed that servers will be doing more than one network function at a time and ports provide a way to maange this. Note:TCP/IP uses ports because it is assumed that servers will be doing more than one network function at a time and ports provide a way to maange this. For example: a server may be serving up web pages (port 80), it may have a FTP (port 21) server, and it may be handling a SMTP mail server (port 25). Ports are assigned by the server. The client needs to know what port to use for a particular service. In the TCP/IP world, each computer with a TCP/IP address has access to 65,535 ports. Keep in mind that ports are not physical devices like a serial, parallel, or USB port. They are an abstraction in the computer’s memory running under the TCP/IP transport layer protocol. Client programming:When programming a client, you must follow these four steps:
• •

Open a socket. Open an input and output stream to the socket.

Machine name is the machine you are trying to open a connection to. so it is necessary to import that package when you program sockets. you can use DataInputStream to receive input from the client: . How to create an input stream?On the client side. readInt.net package. • • • On the server side.• • • • Read from and write to the socket according to the server's protocol. All the input/output stream classes are in the java. readDouble.io package..023! When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. you can use the DataInputStream class to create an input stream to receive response from the server:The class DataInputStream allows you to read lines of text and Java primitive data types in a portable way. include How to open a socket?If you are programming a client. algorithm for TCP client o Find the IP address and port number of server o Create a TCP socket o Connect the socket to server (Server must be up and listening for new requests) o Send/ receive data with server using the socket o Close the connection algorithm for TCP server o Find the IP address and port number of server o Create a TCP server socket o Bind the server socket to server IP and Port number (this is the port to which clients will connect) o Accept a new connection from client  returns a client socket that represents the client which is connected o Send/ receive data with client using the client socket o Close the connection with client All the classes related to sockets are in the java. then you would create an object of Socket class. readChar. Clean up. and readLine. PortNumber is the port (a number) on which the server you are trying to connect to is running. It has methods such as read. select one that is greater than 1.

• • How to create an output stream?On the client side. you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io: How to close sockets?You should always close the output and input stream before you close the socket • .

Sign up to vote on this title
UsefulNot useful