|
Building Socket Programs
|
Sample code: the sample tarball contains a Makefile
and code for a simple server and its corresponding client. The Makefile works as
is for Linux and Mac OS X. The server keeps count of how many times it has been
contacted by the client. It waits for connection and when a client connects, it
sends back a message with the contact count. The client contacts the server, which
has been hardcoded to be running on the same host as the client ("localhost"), receives
the server's contact count and prints it out. Both server and client have run
on Linux, Mac OS X, and Windows platforms. In the following, we show only how to
build a server using the file server.c
. You can follow an analogous
set of steps to build a client using the file client.c
. (But don't
include both source files into a single project!)
To find out how to specify command line options, add to header file
search path, and link with 3rd-party libraries, see the course note on these topics.
Please let me know if you have any
correction or addition. Thanks.
Linux:
version: Ubuntu 12.10
gcc: 4.7.2
Eclipse 3.8.0 (Juno)
version: Red Hat Enterprise Linux Server release 6.4 (Santiago)
gcc: 4.4.7
Eclipse 3.6.1 (Helios)
Sockets come standard with Linux.
You should be able to just type "make" with the support code of this course
and everything should work "out of the box."
Eclipse 3.6.1 project (graphical step-by-step):
- Start up Eclipse and choose your workspace, click "OK" (Fig. 1)
- Open the C/C++ perspective: on the main menu
select "Window→Open Perspective→C/C++"
(Fig. 2)
- Create a new project:
- Select "File→New→C++ Project" (Fig. 3)
- Give the project a name, e.g., "server". In the panels below, specify project type, "Executable→Empty Project", and select the appropriate toolchain (
make
, gcc
, ld
, etc.). Since we're not cross compiling, I choose the "Linux GCC" toolchain. Click "Finish" (Fig. 4)
- Add source files:
- Right click on "server" on the first line of the "Project Explorer" tab on the left and select "Import" (Fig. 5)
- On the "Select" page, choose "General→File System" and click "Next >" (Fig. 6)
- On the "File system" page, either type in the directory where your source file resides, or click the "Browse" button next to "From directory:" to choose the directory (Fig. 7). In browsing for the directory, remember that you're telling eclipse the directory where your source files are, not the source files themselves.
- Once the directory is identified, click on all the source files on the right pane you want imported, for example,
the provided
server.c
(or click the directory name on the left pane to import
all files in the directory), and click "Finish" (Fig. 8)
- Build and run the program:
- On the main menu, select "Project→Build All" (Fig. 9)
- Under the play button on the second menu bar
( ), click on the drop-down menu and select "Run Configurations..."" (Fig. 10)
- On the "Create, manage, and run configurations" page,
double click on "C/C++ Application" (Fig. 11). It will create a "server Debug" application.
- On the "server Debug" configuration page, check "Enable auto build" and click "Run" (Fig. 12)
- Click on the "Console" tab on the lower part of the central pane to view the console output of the program. Click on the red square to stop the program. (Fig. 13).
Mac OS X:
version: Mavericks 10.9.1
kernel: Darwin 13.0.0
Xcode: 5.0.2
gcc: 4.2.1
Sockets come standard with Mac OS X.
You should be able to just type "make" with the support code of this course
and everything should work "out of the box."
Xcode 5.0.2 project (graphical step-by-step):
Windows:
version: 10 Enterprise; 8.1 Enterprise; 7 Enterprise
Visual Studio Enterprise 2015 v. 14.0.24720.00 Update 1; Visual Studio 2013 v. 12.0.31101.00 Update 4; 2012 v. 11.0.51106.01 Update 1
.NET Framework v. 4.6.01055; 4.5.51650; 4.5.50709
- Installation/verification:
Winsock has been part of standard Windows release since Windows NT 3.5 (late 1994).
You can find Winsock files at the following locations on your system:
- runtime libraries:
C:\Windows\System32\ws2_32.dll
on 64-bit Windows, additionally:
C:\Windows\SysWOW64\ws2_32.dll
- header files:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include\WinSock2.h
["Program Files
" for 32-bit Windows]
- linker library:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\WS2_32.Lib
["Program Files
" for 32-bit Windows]
- Later in your source files, include the following line:
#include <winsock2.h>
#include <ws2tcpip.h>
See sample code for an example of how to set up and use
the Windows Sockets API (WSA) within your code.
- Visual Studio 2015 project (graphical step-by-step):
- Create a new project:
- Select "File→New→Project" (Fig. 24)
- Choose a template for your new project:
"Visual C++→Win32→Win32 Console Application",
give the project a name, e.g., "server", along with location and solution name.
Then click "OK" (Fig. 25)
- On the "Welcome to the Win32 Application Wizard" page,
click "Next >" (Fig. 26)
- On the "Application Setting" dialog box, under "Additional options"
tick "Empty project", then click "Finish" (Fig. 27)
- Add source files:
Right click on your project, for example, "server", on the third
line of the "Solution Explorer" pane on the right to
"Add→Existing Item" (Fig. 28),
select your source and header file(s), for example, the provided
server.c
, and click "Add"
- Add libraries:
- Right click on "server" again and select "Properties" to tell
the linker which libraries need to be added (Fig. 29)
- At the very top of the "Property Pages", next to "Configuration:"
choose "All Configurations" (Fig. 30)
- Select "Configuration Properties→Linker→Input" on the left pane.
Then in the "Additional Dependencies" field at the top of the right pane enter:
ws2_32.lib;
Double check that there are no spaces before or after the
semicolon.
Hit RETURN and then click "Apply" (Fig. 31).
- To prevent the compiler from complaining of this or that API being
deprecated, set the following compiler flags:
- Close the "Property Pages" pane by clicking "Ok", then click on the play button on the second menu bar
( )
to build and run the program. If you print out messages to the
console, run the program using Ctl-F5 instead,
to keep the cmd window from exiting after the program exits.
- OpenSSL for Windows: if your project needs to use OpenSSL functions, such as SHA1(),
on Windows, you'll need to install OpenSSL. Download the
developer, not the "Light," version of
OpenSSL binaries
for Windows (v1.0.2e as of this writing).
The 32-bit and 64-bit choices here and below refer to the
architecture you want to build for.
Even if you're running 64-bit Windows,
you can build for 32-bit architecture. (Since Visual Studio defaults to
32-bit (x86) configuration, it's simpler to just choose 32-bit everywhere and
build for 32-bit.) For EECS 489, we only use the SHA1() function,
so when prompted by the installer, you can install OpenSSL in your Desktop,
for example. When prompted whether you want to copy OpenSSL DLLs to
Windows system directory or the OpenSSL binaries directory, choose the latter.
Once installed, you can distribute the OpenSSL files as follows:
- 32-bit:
- runtime libraries/dlls from
OpenSSL-Win32\
(top folder) to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\bin\
We only need libeay32.dll, so you can choose to copy only that file.
- linker libraries from
OpenSSL-Win32\lib\
folder to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\lib\
Again, we only need libeay32.lib, so you can choose to copy only that file.
- header files: copy the whole folder
OpenSSL-Win32\include\openssl
to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\include\
- 64-bit:
- runtime libraries/dlls from
OpenSSL-Win64\
(top folder) to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\bin\amd64\
We only need libeay32.dll, so you can choose to copy only that file.
- linker libraries from
OpenSSL-Win64\lib\
folder to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\lib\amd64\
Again, we only need libeay32.lib, so you can choose to copy only that file.
- header files: copy the whole folder
OpenSSL-Win64\include\openssl
to:
C:\Program Files (x86)\Microsoft Visual Studio *\VC\include\
Only the header file opensslconf.h differs between the 32-bit and 64-bit versions.
- You can then uninstall OpenSSL to remove the OpenSSL folder from your Desktop and Windows registry.
To use SHA1() you must #include <openssl/sha.h> in
your source file. Then in VS, append "libeay32.lib;
" to your
"Additional Dependencies" under
"Configuration Properties→Linker→Input" for "All Configurations".
Be sure not to include the quotes nor any spaces before or after
libeay32.lib;
.
If you're using Cygwin, use Cygwin's setup program to install the "Devel→openssl-devel" package, then just type make to make the support code provided for this course.
References not cited:
Last updated: Jan. 26, 2016 by Sugih Jamin