當前位置:編程學習大全網 - 編程語言 - sClient=socket(AF_INET,SOCK_STREAM,0)意思是什麽

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即可。

  • 上一篇:web前端培訓課程都學習什麽內容_學web前端培訓學校
  • 下一篇:日本“美女”機器人,憑什麽這麽火?
  • copyright 2024編程學習大全網