socket函数(socket函数的三个参数)

http://www.itjxue.com  2023-02-11 14:05  来源:未知  点击次数: 

sClient=socket(AF_INET,SOCK_STREAM,0)意思是什么

生成一个TCP的socket

Function: int socket (int namespace, int style, int protocol)

This function creates a socket and specifies communication style style, which should be one of the socket styles listed in 16.2 Communication Styles. The namespace argument specifies the namespace; it must be PF_LOCAL (see section 16.5 The Local Namespace) or PF_INET (see section 16.6 The Internet Namespace). protocol designates the specific protocol (see section 16.1 Socket Concepts); zero is usually right for protocol.

The return value from socket is the file descriptor for the new socket, or -1 in case of error. The following errno error conditions are defined for this function:

EPROTONOSUPPORT

The protocol or style is not supported by the namespace specified.

EMFILE

The process already has too many file descriptors open.

ENFILE

The system already has too many file descriptors open.

EACCES

The process does not have the privilege to create a socket of the specified style or protocol.

ENOBUFS

The system ran out of internal buffer space.

The file descriptor returned by the socket function supports both read and write operations. However, like pipes, sockets do not support file positioning operations.

SOCK_STREAM提供面向连接的稳定数据传输,即TCP协议。SOCK_STREAM应用在C语言socket编程中,在进行网络连接前,需要用socket函数向系统申请一个通信端口。socket函数的使用方法如下:

int socket(int domain, int type, int protocol);

在参数表中,domain指定使用何种的地址类型,比较常用的有:

PF_INET, AF_INET: Ipv4网络协议;

PF_INET6, AF_INET6: Ipv6网络协议。

type参数的作用是设置通信的协议类型,可能的取值如下所示:

SOCK_STREAM: 提供面向连接的稳定数据传输,即TCP协议。

OOB: 在所有数据传送前必须使用connect()来建立连接状态。

SOCK_DGRAM: 使用不连续不可靠的数据包连接。

SOCK_SEQPACKET: 提供连续可靠的数据包连接。

SOCK_RAW: 提供原始网络协议存取。

SOCK_RDM: 提供可靠的数据包连接。

SOCK_PACKET: 与网络驱动程序直接通信。

参数protocol用来指定socket所使用的传输协议编号。这一参数通常不具体设置,一般设置为0即可。

C++的socket方法

C++中Socket方法主要用于网络通信,常用到的函数如下:

CSocket::Socket初始化

CSocket::SetSockOpt?设置socket选项

CSocket::Bind?绑定地址端口

CSocket::Connect?连接

CSocket::Listen??监听

CSocket::Accept?接收外部连接的socket

CSocket::Send?发送内容

CSocket::Receive?接收内容

CSocket::Close?关闭(不等于delete)

下面是C++编程下用Socket方法做一个客服端和服务端通信的案例:

1、服务端代码

?//初始化Winscok

?if?(!AfxSocketInit())

?{

??AfxMessageBox(IDP_SOCKETS_INIT_FAILED);

??return?1;

?}

?m_exit?=?false;

?CServerDlg?*aDlg?=?(CServerDlg?*)lParam;

?//获取端口

?CString?strPort;

?aDlg-GetDlgItemText(IDC_EDIT_PORT,?strPort);

?UINT?nPort?=?atoi(strPort);

?

?//socket----创建2个socket--------------------------------------------

?CSocket?aSocket,?serverSocket;

?//最好不要使用aSocket.Create创建,因为容易会出现10048错误

?if?(!aSocket.Socket())

?{

??char?szError[256]?=?{0};

??sprintf(szError,?"Create?Faild:?%d",?GetLastError());

??AfxMessageBox(szError);

??return?1;?

?}

?BOOL?bOptVal?=?TRUE;

?int?bOptLen?=?sizeof(BOOL);

?//设置Socket的选项,?解决10048错误必须的步骤

?aSocket.SetSockOpt(SO_REUSEADDR,?(void?*)bOptVal,?bOptLen,?SOL_SOCKET);

?

??//绑定

?if?(!aSocket.Bind(nPort))

?{

??char?szError[256]?=?{0};

??sprintf(szError,?"Bind?Faild:?%d",?GetLastError());

??AfxMessageBox(szError);?

??return?1;?

?}

?//监听

?if(!aSocket.Listen(10))

?{?

??char?szError[256]?=?{0};

??sprintf(szError,?"Listen?Faild:?%d",?GetLastError());

??AfxMessageBox(szError);

??return?1;

?}

?

?CString?strText;

?aDlg-GetDlgItemText(IDC_EDIT_LOG,?strText);

?strText?+=?"服务已经开启!?\r\n";

?aDlg-SetDlgItemText(IDC_EDIT_LOG,?strText);

?while(!m_exit)

?{

??//接收外部连接

??if(!aSocket.Accept(serverSocket))

??{

???continue;

??}

??else

??{

???char?szRecvMsg[256]?=?{0};

???char?szOutMsg[256]?=?{0};?

???serverSocket.Receive(szRecvMsg,?256);?//接收客户端内容:阻塞

???sprintf(szOutMsg,?"接受到的参数是:?%s?\r\n",?szRecvMsg);

???aDlg-GetDlgItemText(IDC_EDIT_LOG,?strText);

???strText?+=?szOutMsg;

???aDlg-SetDlgItemText(IDC_EDIT_LOG,?strText);

???serverSocket.Send("服务器已经收到,已经做出操作!",?50);//发送内容给客户端

???serverSocket.Close();//关闭

??}

??

?}

?

?aSocket.Close();

?serverSocket.Close();

?aDlg-GetDlgItemText(IDC_EDIT_LOG,?strText);

?strText?+=?"Have?Close!";

?aDlg-SetDlgItemText(IDC_EDIT_LOG,?strText);

?return?0;

2、客户端代码

//初始化?CSocket?对象,客户端可以是用Create?因为客户端不需要绑定任何端口和地址,?所以用默认参数即可

//连接指定的地址和端口是用函数Connect函数

//发送内容给服务器是用send函数

//接收服务端发送的内容使用Receive函数

?AfxSocketInit();

?CSocket?aSocket;

?CString?strIP;

?CString?strPort;

?CString?strText;

?this-GetDlgItem(IDC_EDIT_IP)-GetWindowText(strIP);

?this-GetDlgItem(IDC_EDIT_PORT)-GetWindowText(strPort);

?this-GetDlgItem(IDC_EDIT_TEXT)-GetWindowText(strText);

??//初始化?CSocket?对象,?因为客户端不需要绑定任何端口和地址,?所以用默认参数即可

?if(!aSocket.Create())

?{

??char?szMsg[1024]?=?{0};

??sprintf(szMsg,?"create?faild:?%d",?aSocket.GetLastError());

??AfxMessageBox(szMsg);

??return;

?}

?//转换需要连接的端口内容类型

?int?nPort?=?atoi(strPort);

?//连接指定的地址和端口

?if(aSocket.Connect(strIP,?nPort))

?{

??char?szRecValue[1024]?=?{0};

??aSocket.Send(strText,?strText.GetLength());?//发送内容给服务器

??aSocket.Receive((void?*)szRecValue,?1024);?//接收服务器发送回来的内容(该方法会阻塞,?在此等待有内容接收到才继续向下执行)

??AfxMessageBox(szRecValue);

?}

?else

?{

??char?szMsg[1024]?=?{0};

??sprintf(szMsg,?"create?faild:?%d",?aSocket.GetLastError());

??AfxMessageBox(szMsg);

?}

?aSocket.Close();

Socket之bind、listen实现

bind()函数的使用方法很简单,但是它是怎么实现的呢?

本文将从应用层出发,沿着网络协议栈,分析了bind()、 listen()的系统调用、Socket层实现,以及它的TCP层实现。

应用层

int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);

bind() gives the socket sockfd the local address my_addr.

给socket描述符绑定IP和端口,一般服务器才需要。端口号的范围为0 ~ 65535。调用bind()时,一般不要把端口号置为小于1024的值,因为1到1023是保留端口号。

系统调用

bind()是由glibc提供的,声明位于include/sys/socket.h中,实现位于sysdeps/mach/hurd/bind.c中,主要是用来从用户空间进入名为sys_socketcall的系统调用,并传递参数。sys_scoketcall()实际上是所有socket函数进入内核空间的共同入口。

在sys_socketcall()中会调用sys_bind()。

经过了socket层的总入口sys_socketcall(),现在进入sys_bind()。

经过了socket层的总入口sys_socketcall(),现在进入sys_bind()。

通过文件描述符,找到对应的file结构。

通过file结构,找到对应的socket结构。

把用户空间的socket地址复制到内核空间,同时检查是否合法,成功返回0。

socket层

SOCK_STREAM套接口的socket层操作函数集实例为inet_stream_ops,其中绑定函数为inet_bind()。

socket层做的主要事情为合法性检查、绑定IP地址,而真正的端口绑定是在TCP层进行的。

应用层

int listen(int sockfd, int backlog);

Accept incoming connections and a queue limit for incoming connections.

backlog的定义

Now it specifies the queue length for completely established sockets waiting to be accepted,

instead of the number of incomplete connection requests. The maximum length of the queue

for incomplete sockets can be set using the tcp_max_syn_backlog sysctl. When syncookies

are enabled there is no logical maximum length and this sysctl setting is ignored.

全连接队列的最大长度:

backlog保存的是完成三次握手、等待accept的全连接,而不是半连接。

负载不高时,backlog不用太大。(For complete connections)

系统最大的、未处理的全连接数量为:min(backlog, somaxconn),net.core.somaxconn默认为128。

半连接队列的最大长度:

tcp_max_syn_backlog默认值为256。(For incomplete connections)

当使用SYN Cookie时,这个参数变为无效。

半连接队列的最大长度为backlog、somaxconn、tcp_max_syn_backlog的最小值。

listen()是由glibc提供的,声明位于include/sys/socket.h中,实现位于sysdeps/mach/hurd/listen.c中,主要是用来从用户空间进入名为sys_socketcall的系统调用,并传递参数。sys_socketcall()实际上是所有socket函数进入内核空间的共同入口。

在sys_socketcall()中会调用sys_listen()。

经过了socket层的总入口sys_socketcall(),现在进入sys_listen()。

SOCK_STREAM套接口的socket层操作函数集实例为inet_stream_ops,其中监听函数为inet_listen()。

检查套接口的状态、当前连接的状态是否合法,然后调用inet_csk_listen_start()启动监听。

启动监听时,做的工作主要包括:

listen_sock结构用于保存SYN_RECV状态的连接请求块,所以也叫半连接队列。

(1)创建

queue是连接请求控制块,nr_table_entries是半连接的最大个数,即backlog。

(2)销毁

销毁连接请求块中的listen_sock实例,释放半连接队列。

python socket中函数socket.socket( family, type)和socket(family, type)的区别

没有区别。首先都是用socket类中的成员函数socket创建一个socket对象,这个对象有一个句柄(socket.socket的返回值,也可以理解为套接字文件指针),在socket类中有数据AF_INET成员和SOCK_STREAM成员,所以第一种形式实际上是将socket对象自身的这两个数据成员传递给socket类的构造函数(即__init__()方法),而第二种形式的参数则是socket类包中定义好的变量(其实相当于C++的符号常量),跟第一种形式是等同的,因为默认值一样。

AF_INET:表示使用TCP/IPv4版本的协议,(另外有一种是v6的,通常是v4)。

SOCK_STREAM:表示创建的socket是基于TCP传输方式的。

请教socket函数的参数问题

楼上,九级的人,怎么能乱说。。

兄弟,我帮你摘的

/*

* Protocols

*/

#define IPPROTO_IP 0 /* dummy for IP */

#define IPPROTO_HOPOPTS 0 /* IPv6 hop-by-hop options */

#define IPPROTO_ICMP 1 /* control message protocol */

#define IPPROTO_IGMP 2 /* internet group management protocol */

#define IPPROTO_GGP 3 /* gateway^2 (deprecated) */

#define IPPROTO_IPV4 4 /* IPv4 */

#define IPPROTO_TCP 6 /* tcp */

#define IPPROTO_PUP 12 /* pup */

他用0,即代表用的是 IPPROTO_IP 遵守这个规范

(责任编辑:IT教学网)

更多

推荐通讯数据软件文章