Beej's Socket Examples Ported to Windows

I am taking a class on network programming using TCP sockets. Beej's Guide to Network Programming Using Internet Sockets was recommended by the instructor and has proven to be very useful. It contains some execellent examples for Unix/Linux systems. However, I do most of my work on Microsoft Windows. Windows, being Windows, does not handle sockets in the same way as Unix/Linux. So, I am in the process of porting the examples to work in Windows. This web page contains links to the Windows versions of Beej's examples.

This is very much a work in progress and changes will being occuring frequently. My main goal at this point is to port Beej's examples to Windows. When that is done, I will swing back around and work on a more user friendly web site. I am working on Windows 10 using Visual Studio 2022. The last version of Winsock, 2.2, was released around the time of Windows Vista and has been stable since so these ports should work on any version of Windows since Vista. If you have any questions, comments, or suggestions, please send them to WinsockExamples@tallyhawk.net . I cannot promise to reply to every email with my current schedule and workload but I will read them.

In general, each of Beej's example programs has two links on this page. The _README.txt file summarizes the changes necessary to port Beej's Unix source code to Windows source code. The WS*.c file is the source code modified for Windows. You can find the Unix/Linux source code on Beej's web site https://beej.us/guide/bgnet .

ShowIP

Obtain a list of IP addresses for a host.
https://www.tallyhawk.net/WinsockExamples/showip_README.txt
https://www.tallyhawk.net/WinsockExamples/WSshowip.c

Client

A simple client application that receives a short text string from a server.
https://www.tallyhawk.net/WinsockExamples/client_README.txt
https://www.tallyhawk.net/WinsockExamples/WSclient.c

Server

A simple server application that sends a short text string to a client.
https://www.tallyhawk.net/WinsockExamples/server_README.txt
https://www.tallyhawk.net/WinsockExamples/WSserver.c

Poll

Poll a socket. The original poll.c polls stdin which is a file descriptor. This works in Unix/Linux because file descriptors and socket descriptors are essentially the same and even share some functions (e.g., close). This is not true in Windows. The following links provide examples of polling a socket in both Unix/Linux and Windows, respectively. Note that the function in Windows is "WSAPoll", not "poll".
Unix/Linux: https://www.tallyhawk.net/WinsockExamples/poll.c
Windows: https://www.tallyhawk.net/WinsockExamples/WSpoll.c

PollServer

Implement a crude, multi-person chat server to demonstrate using WSAPoll on multiple servers. Note the name of the function in Windows is "WSAPoll", not "poll".
https://www.tallyhawk.net/WinsockExamples/pollserver_README.txt
https://www.tallyhawk.net/WinsockExamples/WSpollserver.c

Another thing to note is that telnet in Windows works differently than in Unix/Linux. Unix/Linux telnet is line buffered. This means that nothing is sent until the user presses the <Enter> key. Windows telnet is not line buffered. It sends each character as it is typed. To send an entire line, return to the telnet prompt (usually <Ctrl-]>) and type "send", a space, and the message followed by the <Enter> key.

A final note is that telnet is not available by default in Windows. It must be installed and/or activated before it can be used. Use your favorite internet search engine to look for "Windows telnet" to find instructions.

Select

Demonstrate use of the select function. The original select.c uses stdin which is a file descriptor. This works in Unix/Linux because file descriptors and socket descriptors are essentially the same and even share some functions (e.g., close). This is not true in Windows. The following links provide examples of using select on a socket in both Unix/Linux and Windows, respectively.
Unix/Linux: https://www.tallyhawk.net/WinsockExamples/select.c
Windows: https://www.tallyhawk.net/WinsockExamples/WSselect.c

SelectServer

Implement a crude, multi-person chat server to demonstrate using select on multiple servers.
https://www.tallyhawk.net/WinsockExamples/selectserver_README.txt
https://www.tallyhawk.net/WinsockExamples/WSselectserver.c

See the notes in the Poll Server section.

Pack

Quick and dirty method of packing floating point numbers for transmission. While small, simple, and fast, it uses space inefficiently and has a very restricted range. Refer to other packing methods below for more robust methods.
No changes required.
https://www.tallyhawk.net/WinsockExamples/WSpack.c

Note that Windows has functions called "htonf" and "ntohf" defined in winsock2.h.

IEEE754

Pack floating point and double precission floating point number by converting them to the format defined by IEEE 754.
No changes required.
https://www.tallyhawk.net/WinsockExamples/WSieee754.c


Beej describes some socket functions in his publication but does not actually provide examples. In this section, I provide examples of these functions in both Unix/Linux and Windows.

Get Host Name

The function gethostname returns the name of the computer on which the program is running.
Unix/Linux: https://www.tallyhawk.net/WinsockExamples/gethostname.c
Windows: https://www.tallyhawk.net/WinsockExamples/WSgethostname.c

Get Peer Name

The function getpeername returns the name of the server to which the client is connected.
Unix/Linux: https://www.tallyhawk.net/WinsockExamples/getpeername.c
Windows: https://www.tallyhawk.net/WinsockExamples/WSgetpeername.c


Reference

Hall, Brian (Beej Jorgenson). Beej's Guide to Network Programming Using Internet Sockets. Version 3.1.2. Copyright © November 13, 2019. https://beej.us/guide/bgnet

Copyright

This web site is Copyright © 2022 Steven Mitchell. The content is based on Beej's copyrighted work (see Reference) where he places the C source code examples in the public domain. I claim no rights to the original (Unix/Linux) versions of the example source code nor to the Windows versions I have created. Please note that I do not guarantee how well the Windows version of the code will work for you.