~ / archivo / stackoverflow / 44373115
In socket programming, why are the "client" and "server" reversed?
-
Why in socket programming does the "server" receive packets, and the "client" send them?
I'm taking a deep-dive into socket programming in both C/C++ and Nodejs; everywhere, in literature and examples alike, the "server" receives packets, while the "client" transmits them.
This does not make sense to me. Should not the "server" transmit packets, and the "client" receive them? Why is the naming convention this way?
EDIT: A couple examples.
1. Beej's Datagram Example for C/C++
http://beej.us/guide/bgnet/output/html/multipage/clientserver.html#datagram
2. A nodejs example:
https://www.hacksparrow.com/node-js-udp-server-and-client-example.html- Some programmer dude 2017-06-05 16:12 In networking a server program is one that passively waits for connections to come in, and a client is a program that actively initiates a connection. Then once a connection is established both parties can send and receive data.
- kmiklas 2017-06-05 16:15 @Someprogrammerdude Ah, I see. The client/server labels are based on the connection, not the direction of the UDP packets.
- cHao 2017-06-05 16:16 Eh. As far as UDP goes, it's quite common for both sides to send and receive. The client generally sends first, because it's the one that needs service and has to ask for it somehow.
- nos 2017-06-05 16:17 Normally a server also sends packets back to the client. The client asks the server for something, the the server performs the job and serves the result back.
- cHao 2017-06-05 16:19 And yes, IP and TCP/UDP are peer-to-peer protocols. At that level, there are no "servers" and "clients". It's not til you get to things like TLS that distinctions are made.
- aggaton 2017-06-05 16:19 Well, in most cases a multi-step protocol is involved. i.e. the client initiates a session by telling the server what it is looking for. Then the server sends the requested data back to the client. So both sides are sending and receiving at different phases in the process.
- kmiklas 2017-06-05 16:22 Look at the nodejs example that I added in the edit. See how the client is sending the "My Kungfu is Good" message? Doesn't it stand to reason that the server should be sending this?
- user207421 2017-06-05 18:51 Both sides send and both sides receive. Neither of the conventions you refer to exists.
-
These terms are part of the client-server architecture model which runs across many different types of systems, according Wikipedia this dates from 1964...
Although servers and clients have capabilites to send/receive information a server has resources and computing capabilities to process information that a client don't.
A client is just a lightweight util which package and send information to a server in the way that server expects to receive information or to receive certain instructions, but a client do not require as much resources as a server does.
Examples of server-client relationships:
- Puppet Server (Manages all rules, send information and order to clients, etc...)
Puppet Agent (Receives orders from Puppet Servers and is installed in several nodes and send information about statuses)
Web Servers (HTTP Apache or Ngnix i.e) (process requests/code to deliver HTML results)
- Web Clients (wget, Chrome, Firefox, CURL, links) send requests to the Web Server to be processed.
Having that in mind check your own example the UDP Server is listening (waiting for information):
server.bind(PORT, HOST);and the UDP Client is the one with the function to "send" information:
client.sendWhich answer your own question:
Should not the "server" transmit packets, and the "client" receive them? Why is the naming convention this way?