Introduction
这是一个两周的超级大作业,用Linux Network Programming的知识写一个聊天软件。
内容涉及方方面面:套接字编程,异常处理,信号处理,多进程编程,多线程编程,数据库,命令行,UI等等。
In this assignment you will be creating a chat service much like Google’s
Hangouts, called Wolfie Chat. The goal is to learn about network programming
in C. You will implement the service so that it adheres to the Wolfie
Protocol.
The concept of a protocol is an important one to understand. When implementing
the protocol you need to follow the requirements and description TO THE
LETTER. The idea is to create a standard so that anyone implementing the
Wolfie Protocol will be able to connect and operate with any other program
implementing the same protocol. Any client and chat should work with any
server if the protocol is used correctly (HINT HINT think grading).
Certain public networks like wolfienet secure or starbucks will not allow you
to connect your client and server together over the network. You may also want
to test your client by connecting to another group’s server, and your server
by having other group’s clients connect to it. This is a good way to ensure
that the protocol is implemented correctly in your client/server programs.
While we encourage testing this way, THIS IS NOT an open invitation to share
code with other groups. You should not be showing or sharing code with anyone
else. To perform these tests you should simply be telling your client programs
to connect to the IP address and port number at which these servers are
running at. You have been warned!
In certain situations your implementation will result in deadlocks, race
conditions, etc. This is acceptable. We will be addressing these concerns in
HW6.
We will be using this assignment as the basis for HW6. in order to do so,
certain aspects of the project must be implemented with particular
properties/structure. Make sure to pay attention to these details. We will
mark these criteria with symbol.
Groups
In this assignment you are allowed groups of two members. Unless stated
otherwise, we will assume that you are in the same group as the previous
assignment. If you wish to divorce your partner from the previous assignment
and assemble a new group please follow the directions in hw4.
Remember all changes to groups must be emailed within the first 2 days of
the assignment posting. Otherwise, you are stuck with your partner!
Since we will be using this assignment as the basis for HW6. Any groups who
dissolve after HW5 will not be allowed to partner up for HW6.
Getting started
Make the directory hw5 in the root of your git repository. All files which you
create for this assignment should be placed in this directory. You should also
create a Makefile, which generates the server program called server, a client
program called client, and your chat program called chat. When we navigate to
the hw5 directory and type make these three programs should be generated.
$ cd hw5
$ make
gcc -Wa11 -Werror server.c -o server
gcc -Wal1 -Werror client.c -o client
gcc -Wa11 -Werror chat.c -o chat
$ ls
chat chat.c client client.c Makefile README.md server server.c
You are encouraged to separate your programs into as many files as you want.
Just make sure that when we type make, it generates the client, chat, and
server binaries.
A good strategy for splitting up the work in this assignment should be to have
one partner create the client/chat program and the other partner create the
server. Test out each part as you implement it to ensure that it works (don’t
just build the entire thing separately and hope it all works in the end.) It’s
also okay to test both the server and client locally on your own machine, but
you really should test it out over a real network as well.
Pro Tip: PLAN AHEAD! if you take the time to layout the organization of your
control flow and logic using the diagrams in this assignment as a resource
you will be much better off in the long run.
Helpful Resources
You should make sure you understand chapter 11, 12.2, and 12.3 in your
textbook. Also if you didn’t understand files, fork, execute, and pipes from
the previous homework, make sure you review chapter 10.1-10.4 and 10.6-10.12.
An AMAZING reference on sockets which summarizes the man pages can be found
here (Don’t worry that the site is called python). Additionally you may also
want to read Bee’s Guide to Network Programming. It has a good mix of
tutorials, explanations, and descriptions of the functions you need to use for
this assignment. You may also want to consider using wireshark or tcpdump to
monitor the network packets.
Things to be aware of
Aside from the issues which occur in the protocol (such as a user with
duplicate names), you also need to handle other external errors such as
connections getting lost, client program getting killed, server program
getting killed, etc. You should look to handle:
- EINTR
- EPIPE
- ECONNREFUSED
- ECHILD
- EJNVAL
- SIGCHILD
A This is not an exhaustive list. You are responsible for handling all types
of errors which can occur. Read the man pages for the types of errors that
may occur.
If the client gets disconnected for any error reason, the server should “clean
up” the user. This means that at the minimum the user name should be removed
from the current user list.
Use valgrind to make sure that you program does not exit with allocated
memory or open file descriptors.
Naming threads
To aid in debugging and grading you should name your threads. To do so, you
need to add the _GNU_SOURCE directive to your header file.
define _GNU_SOURCE
Now when you create a thread you can name it. For example:
pthread_create(&tid, NULL, &myname_thread, cfd);
pthread_setname_np(tid, “MYNAME”);
Now you can open up another terminal and run the following command to look at
all the threads running in your program
> ps -e -T | grep ‘server|COMM|LOGIN’
PID SPID TTY TIME CMD
7667 7667 pts/4 00:00:00 server # Server program you ran; ./server 1234 “Super Cool motd”
7667 7933 pts/4 00:00:00 LOGIN # This is a login thread
7667 7936 pts/4 00:00:00 LOGIN # This is a login thread
7667 7952 pts/4 00:07:74 COMM # This is the main communication thread
# If you had any other threads you didn’t name they would also show up as server
If you would like to watch your threads in real-time you can use a program
called htop.
Install htop with the following command:
sudo apt-get install htop
To configure htop to show threads, press F2 -> and go to Display Options and
make sure you turn on the following options.
Set the options by pressing spacebar. After selecting the options you want
press escape a few times to go back to the main view. Now press F3 and search
for ./server
You should now see your process and all of its threads created in real time.