On a high level, this is my understanding of the difference between a client and server:
- a server listens on a host and a port, receives requests (e.g. through a socket connection), and then sends a response to the client who initiated the socket connection.
- The client is what sends a request to that server socket, and waits for a response.
The definition is a bit broad and vague. Here is two situations in which both clients subscribe to the above definition, but behave differently:
1) In the first example, the program assumes a socket server is listening on port 2000. This client makes a tcp connection to socket server, waits for response, then prints the response to stdout of the currently running process, and closes connection. It itself does not write anything to the server:
require 'socket'
host, port = ARGV
s = TCPSocket.open(host, port)
while line = s.gets
puts "the data is #{line.chop}"
end
s.close
The socket server it talks to accepts tcp connections on port 2000. Once a connection is established, it sends data back to client, and then closes the socket thereby terminating connection with the client:
require 'socket'
server = TCPServer.open(2000)
loop {
client = server.accept
client.puts(Time.now.ctime)
client.close
}
Ok so the client program in the above example subscribes to the original definition delineated for client.
But in the next example, the client behaves differently. The clients establishes a tcp connection to a running instance of a redis socket server. The client creates a TCP socket connection to redis-server on port 6379 (the default redis port), plus establishes the new unified request protocol (redis protocol specification) - allowing the connection to communicate using redis-specific format messaging (e.g. multi-bulk replies). Just like the other client, this client sends a request to the server socket for a tcp connection, and waits for a response.
However, unlike the other example, this clients writes data to the server. Redis implement the Publish/Subscribe messaging paradigm. The client publishes to a channel which is stored in the key/value store on main memory that redis supports. So this client behaves significantly different from the other in that it just doesn't simply wait for some kind of response, it writes to the server and causes the server to do things, like push data to subscribed clients.
require 'rubygems'
require 'redis'
require 'json'
redis = Redis.new
data = {"user" => ARGV[1]}
loop do
msg = STDIN.gets
output = redis.publish ARGV[0], data.merge('msg' => msg.strip).to_json
puts "What output we get #{output}"
end
So I am wondering if someone could give me a more comprehensive definition of client and server.