socket的编程过程(Internet Domain, stream type)

本文深入解析socket编程中的server与client部分,详细阐述了从创建socket到接受连接,直至数据发送与接收的全过程。同时,介绍了socket建立时需要声明的address domain和socket type,以及具体的实现步骤,并提供了基于Internetdomain和streamtype的C语言源程序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值