本篇主要在紀錄 TCP/IP 的程式範例。為什麼要紀錄呢?因為年紀大了,很多程式都想撿現成的來改就好了。要用的時候還要找就太麻煩了,所以乾脆找一個自己想要的版本紀錄起來,以後要用就有了。這次的紀錄重點,包含了: Linux Socket Programming. Select Usage. 範例程式主要來自下面這本書: TCP/IP Sockets in C: Practical Guide for Programmers, Second Edition (ISBN: 978-0-12-374540-8) by Kenneth L. Calvert and Michael J. Donahoo 其實我根本沒買書啦,反正網路上有 範例程式 可以下載,我就直接拿來改囉。 下面就是改寫後的程式碼。改寫的部份主要是風格的問題,以及不想讓 client 傳完一個句子就關閉 socket。 Server: # include < stdio.h > // for printf() and fprintf() # include < sys/socket.h > // for socket(), bind(), and connect() # include < arpa/inet.h > // for sockaddr_in and inet_ntoa() # include < stdlib.h > // for atoi() and exit() # include < string.h > // for memset() # include < unistd.h > // for close() # include < sys/time.h > // for struct timeval {} # include < fcntl.h > // for fcntl() # define MAXPENDING 5 // Maximum outstanding conn...