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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章