Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Leave Comments, Critiques, and Suggestions Here?

Using Sockets: a general introduction to BSD socket programming

General Network Stuff

Endianness

For various reasons beyond the scope of this document, different types of computers store and send data differently. The gist of it it, do you store/send the big end first, or the little end first. Why all this is important is that network address are always in "Network byte order". Never mind what that is exactly, but for portability sake, never directly set a network port number or IP address. Use either the host &lt-&gt network conversion function or some library facility.

Another gotcha on this is that sending raw data across a network connection only works if the two computers use the same byte order. Don't count on that. Pick a byte order (if one hasn't already been standardized) and convert the data to it from whatever is native to the host system. Similarly on receiving data convert it from the chosen byte order to the host order. This isn't as bad as it may seem, most platforms do this fairly well in hardware and many programming environments have high level constructs to do the conversion automatically. (Tango has high level abstractions to take care of this.)

IP, ports, TCP/UDP

(TBA)

Connected Sockets (TCP)

Connecting to a server

Create a socket

socket(...)

Connect a socket

connect(...)

Accepting sockets as a server

Create a socket

socket(...)

Binding a socket

bind(...)

Listening to a socket

listen(...)

Accepting connections from a socket

accept(...)

Connectionless Sockets (UDP)

Uh...