socket的編程過程(Internet Domain, stream type)

1)socket的組成,server 和 client

2)server部分的程序包括:

  1. Create a socket with the socket() system call
  2. 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.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Send and receive data

     client部分的程序包括:

  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use theread() andwrite() system calls.
3)socket建立的時候需要聲明 address domain 和 socket type

     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


發佈了37 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章