慶應義塾大学
2007年度 秋学期

ネットワーク・プログラミング(C言語)
Network Programming in C

2007年度秋学期 火曜日2時限
科目コード: 13070 / 2単位
カテゴリ:
開講場所:SFC
授業形態:講義
担当: Rodney Van Meter
E-mail: rdv@sfc.keio.ac.jp

第10回 12月18日
Lecture 10, December 18: Getting Caught Up 3

Outline of This Lecture

make

I've noticed that some of you are still compiling each program by hand. You should be using a simple Makefile and the command make if you are working on a Unix-like system (including cygwin).

Below is the Makefile that I am currently using for the programs for this class. You should make one like it, replacing the program names with your own program names for the four programs.

# this flag says to compile for the debugger
CFLAGS=-g

# edit this list to match your set of programs
TARGETS=client-rdv server-wiki-rdv server-udp-rdv client-udp-rdv \
			client-udp-wiki

all:			${TARGETS}

# edit the lines below this to match your set of programs

server-wiki-rdv:	server-wiki-rdv.c

client-rdv:		client-rdv.c

server-udp-rdv:		server-udp-rdv.c

client-udp-rdv:		client-udp-rdv.c

client-udp-wiki:	client-udp-wiki.c

clean:
	rm ${TARGETS}

A Better Makefile

On BSD systems, you will need to use gmake instead of make to use this Makefile.

# this flag says to compile for the debugger
CFLAGS=-g

# edit this list to match your set of programs
TARGETSOURCES=client-rdv.c server-wiki-rdv.c server-udp-rdv.c \
	client-udp-rdv.c client-udp-wiki.c

LIBRARIES=mylib.a

# edit this list to match your set of library source files
LIBSOURCES=mylib.c

#edit this list to match your set of test programs
TESTSOURCES=mylibtest.c gai.c

# header files
HEADERFILES=prototypes.h

LIBOBJS=$(subst .c,.o,$(LIBSOURCES))

ALLSOURCES=$(LIBSOURCES) $(TESTSOURCES) $(TARGETSOURCES)

TESTBINARIES=$(subst .c,,$(TESTSOURCES))
TARGETBINARIES=$(subst .c,,$(TARGETSOURCES))
ALLBINARIES=$(TARGETBINARIES) $(TESTBINARIES)


all:			$(ALLBINARIES)

# the four main programs we have discussed
server-wiki-rdv:	server-wiki-rdv.c $(LIBRARIES)
	$(CC) $(CFLAGS) -o server-wiki-rdv server-wiki-rdv.c $(LIBRARIES)

client-rdv:		client-rdv.c $(LIBRARIES)
	$(CC) $(CFLAGS) -o client-rdv client-rdv.c $(LIBRARIES)

server-udp-rdv:		server-udp-rdv.c $(LIBRARIES)
	$(CC) $(CFLAGS) -o server-udp-rdv server-udp-rdv.c $(LIBRARIES)

client-udp-wiki:	client-udp-wiki.c $(LIBRARIES)
	$(CC) $(CFLAGS) -o client-udp-wiki client-udp-wiki.c $(LIBRARIES)

# this was a short test program I wrote; you probably don't need it
gai:	gai.c $(LIBRARIES)
	$(CC) $(CFLAGS) -o gai gai.c $(LIBRARIES)

# here is the library
mylib.a: $(LIBOBJS)
	ar r mylib.a $(LIBOBJS)

clean:
	rm -f *.o *.a *.d $(ALLBINARIES) TAGS

# automatically generated dependency files
include $(subst .c,.d,$(ALLSOURCES))

# rules for generating dependency files
%.d: %.c
	$(CC) -M $(CFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

Structure of Your System

By now, you should have a complete system fairly well under way. It should consist of the following:

宿題 12/18
Homework for Lecture 10

Note: this homework has been somewhat modified from the originals from Lectures 3 and 4.

This week's homework (submit via email):

  1. Take the program at the top of Lecture 3 and fix it. It does not currently compile. Please copy that to your computer, and debug it. (You may also want to use this as an opportunity to try a source code control system, such as RCS, and my preference is that you send me the solution to this problem as a diff or rcsdiff from the existing program.)
    Your output should look something like this:
    [rdv@localhost network-programming-in-c]$ ./a.out 
    elementNumber: 10
    elementNumber: 9
    elementNumber: 8
    elementNumber: 7
    elementNumber: 6
    elementNumber: 5
    elementNumber: 4
    elementNumber: 3
    elementNumber: 2
    elementNumber: 1
    elementNumber: 0
    
  2. Look for the word Report in the comments in the UDP server code. There is a chunk of code using getnameinfo(), getpeerinfo(), and gai_strerror(). Take that chunk of code and put into the library file mylib.c. I called my function report_mysocket.
  3. Take the TCP server program in Lecture 4, and edit where the comments are to print out information about the connections, and make it use that same function.
  4. Test for errors from the system calls in the above code. Use perror() where appropriate. (We will come back to this.)
  5. You were supposed to create two source files and link them together for your program. This week, we are going to add unit test. We need one more source code file:
    • mylibtest.c
    That file should include a main() function that calls each function in the corresponding source file to test that it works. Now you must compile five separate programs: your TCP client and server, your UDP client and server, and mylibtest. The initial code for mylibtest.c will probably be similar to the TCP server with the error printing.

Additional Information

その他