Basics of Socket Programming in Java
Client/Server
A server is anything that has some resources that can be shared.
A client is simply any other entity that wants to gain access to a particular server. The interaction between client and server is just like the interaction between a lamp and an electrical socket. The power grid of the house is the server and the lamp is a power client.
What exactly is a socket?
Can say, a network socket is lot like an electrical socket.
The notion of a socket only allows a single computer to serve many different clients at once, as well as serving many different types of information. This is managed by the introduction of a port which is a numbered socket on a particular machine. A server process is said to âlistenâ to a port until a client connects to it. A server is allowed to accept multiple clients connected to the same port number, although each session is unique. To manage multiple client connections, a server process must be multithreaded or must have some other means of multiplexing simultaneous I/O.
For more information about it check the following link. It is very informative for those who are interested in this topic. 😀
#-Link-Snipped-#
TCP/IP Client Sockets
These ae used to implement reliable,bidirectional, persistent, point-to-point stream based connections between hosts on the Internet. A Socket can be used to connect Java's I/O system to other programs that may reside either on the local or any other machine on the internet.
There are two kind of TCP/IP sockets in Java
1. For Servers- ServerSocket
ServerSocket class is designed to be a "listener" whcih wait for clients to connect before doing anything. This class is used to create servers that listen for either local or remote client programs to connect to them on published ports.
Constructors of this class:
a) ServerSocket( int port)
Creates server socket on the specified port with a queue length of 50.
b) ServerSocket(int port,int maxQueue)
Create a server socket on the specified port with a maximum queue length of maxQueue
c) ServerSocket(int port, int maxQueue, InetAddress localAddress)
Create a server socket on the specified port with a maximum queue length of maxQueue .On a multihomes host, localAddress specifies the IP address to which this socket binds.
All constructors above reflect the port number that we want to accept connections on and optionally how long we want the queue for said port to be.
ServerSocket has a method called as accept() which is a blocking call that will wait for a client to intiate communications and the return the normal Socket that is then used for communication with the client.
2. for Clients - Socket
The Socket class is designed to connect server sockets and initiate protocol exchanges.
The Socket object establishes a connection between the client and server. There are two constructors that can be used to create client sockets.
a) Socket(String hostName, int port)
Creates a socket connecting the local host to the named host and port
b) Socket(InetAddress ipAddress, int port)
Creates a port using a preexisting InetAddress object and a port
Few more things related to the Socket
a) InetAddress getInetAddress()->Returns the InetAddress associated with the Socket object .
b) int getPort()->Returns the remote port to which the invoking Socket object is connected.
c) int getLocalPort() -> Returns the local port to which the invoking Socket object is connected.
d) nputStream geInputStream() -> Returns the InputStream associated with the invoking socket.
e) OutputStream geOutputStream() -> Returns the OutputStream associated with the invoking socket.
Source: Complete Reference -by Herbert Schildt
Programming part is yet to come. Please wait. 😀
Hope the given link will clear all your doubts about this topic. If still any Please feel free to ask and add more information here if you have anything to share here.😀