Lab 1 - One way chat

CS Majors

Due: Class time Sept 17


The objective of this lab is to familiarize the student to the development environment on Linux for writing networking code using sockets. This will be done by implementing and testing the code in the later part of the text, Chapter 1.


First, check if your Linux System has g++. Simple type "g++" into a terminal window.

If not there, then execute the following command:

  • sudo apt-get install g++
You will need to enter the login password ("mvnucs" on lab systems).

Create two files, server.cpp and client.cpp. Enter the code as seen in the text. As the code is written it will not compile, as several includes are missing for Linux. I found the following includes to all be needed for client.cpp:

#include <stdio.h>
#include <strings.h>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

I found the following includes needed for server.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

Also, the len variable in server.cpp needs to be of type socklen_t, eg:

socklen_t len;

In addition, you need a single Makefile to to build both of these files. Working Code is found below:

Sample code:

Test out the code between two different systems in the lab, and between the systems in the lab, and the server (you should have login credentials).

Part 1 - Build and run from Command line


  1. Go into the "User Accounts" section of Edubuntu, and create a user for yourself. Use this account from now on.
  2. Get the code above running on your lab computer. Add the "g++" application as needed.
  3. By default the client sends messages to a server on the same system, using the address "". What is the significance of this address? Do some research and explain it in your lab report.
  4. Open up "VMWare"* and run the Ubuntu that is in there. Create an account for yourself on that system also. Get the system to work between the outer, and inner operating systems. Do it BOTH ways, e.g. with the server on either side. What steps did have to take? What IP addresses did you use? Can you explain the significance of these addresses?
    1. Note: VMWare virtual network adapaters may be in "bridge" mode or "NAT" mode (and some others modes). In order to create a connection from the outer operating system to the inner operating system you MUST be in bridge mode.
    2. Exaplain in your lab report why it must me in bridge mode.
    3. *If VMware, and the Linux guest, has not been installed, follow the instructions: VmWare Instructions.
    4. Go to VM->settings and select "Network Adapter". If the system in in NAT mode, change to bridge mode.
  5. Get your code to talk to someone elses computer in the lab, in both directions. What steps did you have to take?
  6. Get your code to work between your computer and the server. You should all have accounts on this system. Again, explain what you had to do.
  7. Note what happens if the server NOT running when you run the Client, nothing happens, no error. Why is this?
  8. Go through the program, and put comments before every line of code, explaining the purpose of that line.

Part 2 - Build and run from NetBeans IDE


  1. Click on the "K" in the lower left. Type "NetBeans". Click on NetBeans and start.
  2. Create a new project with "File->New Project".
  3. Pick "C/C++" from Categories, and "C++ Application" from Projects to create a new project.
  4. Create two projects, server and client.
  5. You will need to paste the code provided into main.cpp.
  6. Build and test the programs.
Note: I had to add "#include <string.h>" to get Netbeans to compile the code.

Turn in

  1. (20%) A capture of the working operation.
  2. (30%) The answers to the questions in 3-7.
  3. (30%) The commented code from step 8.
  4. (20%) A paragraph describing your experience, including any difficulties encountered and suggestions for improvement.
Topic revision: r4 - 2015-09-10 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback