Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by World Engineer Aug 21 '13 at 19:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
"Server" and "client" are intentionally vague. Typically the server offers some sort of service, and the client uses it. Beyond that, there are no rules. You can certainly come up with plenty of scenarios where the roles are ambiguous. In those cases, "client" and "server" may not be the appropriate names. Perhaps "peer" would be better. –  tylerl Jul 27 '13 at 23:53

2 Answers 2

up vote 7 down vote accepted

The client and server are basically two parts of a distributed computing model.

In this model, a user uses a client computer which sends requests to the server. The server then processes the request and creates the appropriate response which it sends back to the client. In this model, it is often the client that initiates the interaction and not the server. The server simply waits for requests to be serviced.

In the two examples that you gave above,

  1. First example, signifies the simple Request-Response operation.
  2. Second Example, implies the Client sends the request, does some transactions/operations and the sends the Response, (Successful or Failure.)

So it does send the response, in the second example.

In Summary:

  • A Server services the requests from the client.
  • The client is usually the initiator.
  • A single server accommodates multiple clients.
  • The server is responsible for the data processing/operations while the client handles the presentation.

Let me know if it helped.

share|improve this answer
    
The server is the one calling the client in the case of push methods. I think your description is too narrow. –  Florian Margaine Jul 28 '13 at 9:29
    
@FlorianMargaine I would appreciate, if you could elaborate the push methods. The explanation I gave was for the basic server-client calls. I would appreciate if you could elaborate on your comment. Tx –  JNL Jul 28 '13 at 19:38
    
Just see how the iPhones get their emails. They don't poll the email server, the server pushes new mails onto them when necessary. Also take a look at websockets for a push example. –  Florian Margaine Jul 28 '13 at 20:23

A server is a computational unit dedicated to providing services to one or more clients or other servers.

A client requests services from one or more servers or other clients.

A server can act as a client requesting services from other servers.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.