1)socket的組成,server 和 client
2)server部分的程序包括:
- Create a socket with the
socket() system call
- Bind the socket to an address using the
bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
- Listen for connections with the
listen() system call
- Accept a connection with the
accept() system call. This call typically blocks until a client connects with the server.
- Send and receive data
client部分的程序包括:
- Create a socket with the
socket() system call
- Connect the socket to the address of the server using the
connect() system call
- Send and receive data. There are a number of ways to do this, but the simplest is to use theread() andwrite() system calls.
address domain: Unix domain----系統的入口(字符串)和端口號(可以選擇2000到65535的任何一個數字)
Internet domain----IP地址和端口號
socket type:stream sockets 用 TCP協議,是一種可靠的,基於stream的協議
datagram sockets 用UDP協議,是一種不可靠的,基於message的協議
4)例程,例程是Internet domain基於stream type的協議的c語言的源程序
server.c http://www.linuxhowtos.org/data/6/server.c
client.c http://www.linuxhowtos.org/data/6/client.c
關於例程的逐行解釋,請移步源網址~
----------文章轉自http://www.linuxhowtos.org/C_C++/socket.htm