Reputation: 77
I am relatively new to programming and am attempting to self learn socket programming. As per my understanding, a socket is needed at both endpoints if a process (say server process) needs to communicate with another process (say client process) over the network.
If my server and client processes are on the same machine, then why do I need sockets because the streams or datagrams are not going over the network? It's within the same machine. Can anybody please clarify the reason for this?
Upvotes: 6
Views: 12578
Reputation: 1969
What OS are you using? If it's Windows then you have all of these to do IPC
The following inter process communications (IPC) mechanisms are supported by Windows:
Before going on, let us know your operating system and we can give you some clues about inter process communication.
Upvotes: 1
Reputation: 96266
There are several ways to communicate between different processes, sockets is one of them.
As always, there are trade-offs.
There are faster, less resource expensive ways to do IPC.. Some of them add extra code complexity and, if not used correctly, can add possible bugs to your application.
The main benefit of sockets is that they are transparent. If you later decide you want one or more processes to run on a different machine, you don't have to change the code.
Upvotes: 0
Reputation: 7854
If you are sure that they will always be in the same machine, then there is no need for sockets.
There are other data sharing mechanisms between processes on the same machine like sharing through files. But sockets make it transparent to the user whether it is on the same machine or on different machine by providing an abstraction, so if you may require it on different machines, socket is a good way.
Upvotes: 3
Reputation: 7
Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.
When the connection is made, the server creates a socket object on its end of the communication. The client and server can now communicate by writing to and reading from the socket.
The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them.
The following steps occur when establishing a TCP connection between two computers using sockets:
The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.
After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.
The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server.
On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.
After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream.
TCP is a twoway communication protocol, so data can be sent across both streams at the same time. There are following usefull classes providing complete set of methods to implement sockets.
Upvotes: 0
Reputation: 522091
Then how do two processes on the same machine communicate without using sockets?
...
That's right, sockets are a way for two processes to communicate regardless of whether it's over the network or within the same machine. You could invent other mechanisms for communication within the same machine (and there are plenty), but why if sockets already serve that purpose perfectly fine?
Upvotes: 11