对于TCP,服务器先创建TCP套接字sl_Socket,并将其绑定在TCP服务器地址sl_Bind,然后指定端口监听sl_Listen,并接受TCP连接sl_Accept,一单建立起TCP连接,就能进行数据接受sl_Recv和数据传送sl_Send,最后关闭TCP套接字sl_Close。
对于UDP,不需要监听和接受连接,创建并绑定好套接字后就可以通信,所以这是一种不可靠的连接。不过udp套接字的应用简单很多。
套接字的应用程序可以使用操作系统运行,也可以不使用操作系统运行。不使用操作系统时,通常调用sl_SetSocketOpt()设置为非阻塞模式,而使用操作系统,由于多线程可使用阻塞模式。
下面在程序中体现
int BsdTcpServer(unsigned short usPort)
{
SlSockAddrIn_t sAddr;
SlSockAddrIn_t sLocalAddr;
int iCounter;
int iAddrSize;
int iSockID;
int iStatus;
int iNewSockID;
long lLoopCount = 0;
long lNonBlocking = 1;
int iTestBufLen;
for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
{
g_cBsdBuf[iCounter] = (char)(iCounter % 10);
}
iTestBufLen = BUF_SIZE;
sLocalAddr.sin_family = SL_AF_INET;
sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
sLocalAddr.sin_addr.s_addr = 0;
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( iSockID < 0 )
{
ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
}
iAddrSize = sizeof(SlSockAddrIn_t);
iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
if( iStatus < 0 )
{
sl_Close(iSockID);
ASSERT_ON_ERROR(BIND_ERROR);
}
iStatus = sl_Listen(iSockID, 0);
if( iStatus < 0 )
{
sl_Close(iSockID);
ASSERT_ON_ERROR(LISTEN_ERROR);
}
iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
&lNonBlocking, sizeof(lNonBlocking));
if( iStatus < 0 )
{
sl_Close(iSockID);
ASSERT_ON_ERROR(SOCKET_OPT_ERROR);
}
iNewSockID = SL_EAGAIN;
while( iNewSockID < 0 )
{
iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
(SlSocklen_t*)&iAddrSize);
if( iNewSockID == SL_EAGAIN )
{
MAP_UtilsDelay(10000);
}
else if( iNewSockID < 0 )
{
sl_Close(iNewSockID);
sl_Close(iSockID);
ASSERT_ON_ERROR(ACCEPT_ERROR);
}
}
while (lLoopCount < g_ulPacketCount)
{
iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);
if( iStatus <= 0 )
{
sl_Close(iNewSockID);
sl_Close(iSockID);
ASSERT_ON_ERROR(RECV_ERROR);
}
lLoopCount++;
}
Report("Recieved %u packets successfully\n\r",g_ulPacketCount);
iStatus = sl_Close(iNewSockID);
ASSERT_ON_ERROR(iStatus);
iStatus = sl_Close(iSockID);
ASSERT_ON_ERROR(iStatus);
return SUCCESS;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
分析:
创建TCP套接字 sl_Socket()
绑定TCP套接字
监听TCP套接字
设置非阻塞模式
接受TCP连接
接受TCP数据包
关闭TCP套接字
iStatus = sl_Close(iSockID),关闭的iSockID包括sl_Accept() AND sl_Socket()的返回值。
BsdTcpClient()分析
int BsdTcpClient(unsigned short usPort)
{
int iCounter;
short sTestBufLen;
SlSockAddrIn_t sAddr;
int iAddrSize;
int iSockID;
int iStatus;
long lLoopCount = 0;
for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
{
g_cBsdBuf[iCounter] = (char)(iCounter % 10);
}
sTestBufLen = BUF_SIZE;
sAddr.sin_family = SL_AF_INET;
sAddr.sin_port = sl_Htons((unsigned short)usPort);
sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);
iAddrSize = sizeof(SlSockAddrIn_t);
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( iSockID < 0 )
{
ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
}
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
if( iStatus < 0 )
{
sl_Close(iSockID);
ASSERT_ON_ERROR(CONNECT_ERROR);
}
while (lLoopCount < g_ulPacketCount)
{
iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 );
if( iStatus < 0 )
{
sl_Close(iSockID);
ASSERT_ON_ERROR(SEND_ERROR);
}
lLoopCount++;
}
Report("Sent %u packets successfully\n\r",g_ulPacketCount);
iStatus = sl_Close(iSockID);
ASSERT_ON_ERROR(iStatus);
return SUCCESS;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
分析:
创建TCP套接字 sl_Socket()
连接TCP服务器
发送TCP数据包
关闭TCP套接字
应用:无线UART程序设计 基于TCP
主要分析用TCP套接传送的字符
参考:ARM CORTEX-M4+WIFI MCU应用指南 CC3200 IAR 基础篇 郭书军