SOCKET SendConnectSocket= INVALID_SOCKET, // Window socket
SendDataSocket = INVALID_SOCKET; // Socket for communicating
SOCKADDR_IN local_sinSend, // Local socket address 监听端口
accept_sinSend; // Receives the address of the 接收端口地址 send
// connecting entity
int accept_sinSend_len; // Length of accept_sinPic
BOOL InitServerSend(void)
{
//////////////////////////////////////////////////////////////////
if ((SendConnectSocket = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
// MessageBox (NULL, NULL, TEXT("Send socket() Error"), MB_OK);
return FALSE;
}
//////////////////////////////////////////////////////////////////
// 读配置文件
//--------------------------------------------------------------
if (bind (SendConnectSocket,(struct sockaddr *) &local_sinSend,
sizeof (local_sinSend)) == SOCKET_ERROR)
{
// MessageBox (NULL, NULL, TEXT("Send Socket bind() Error"), MB_OK);
closesocket (SendConnectSocket);
return FALSE;
}
//---------------------------------------------------------------------------
// Establish a socket to listen for incoming connections.
int iRet=listen(SendConnectSocket, SOMAXCONN);
// Stop listening for connections from clients.
accept_sinSend_len = sizeof (accept_sinSend);
SendDataSocket = accept (SendConnectSocket, (struct sockaddr *) &accept_sinSend, (int *) &accept_sinSend_len); // Accept an incoming connection attempt on WinSocket.
closesocket (SendConnectSocket); // Stop listening for connections from clients.
int sendBufLen=0;
int len=sizeof(sendBufLen);
getsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,&len);
sendBufLen=32;
setsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,len);
return TRUE;
}void SendData() // 线程函数
{ unsigned char* bDataRec=(unsigned char*)malloc(1024);
int iReadLen=0; //wzm 2014-12-6
InitServerSend();
while(1)
{
iReadLen = recv (SendDataSocket, (char*)bDataRec, 1024, 0);
if(iReadLen>0)
{
}
else
{
closesocket(SendDataSocket);
InitServerSend();
}
Sleep(1);//60000 WZM 2014-12-3
}
}
AfxBeginThread(AFX_THREADPROC(&SendHandleData), NULL,0);//网口线程void SendHandleData() // 线程函数
{
while(1)
{
if(g_SoftwareState==0)
{
return;
}
if(g_pTeleControlerDlg->m_b_IsSending)
{
send(SendDataSocket,(const char*)sendBuf,32,0);
}
Sleep(4);//60000 WZM 2014-12-7
}
}
SendDataSocket = INVALID_SOCKET; // Socket for communicating
SOCKADDR_IN local_sinSend, // Local socket address 监听端口
accept_sinSend; // Receives the address of the 接收端口地址 send
// connecting entity
int accept_sinSend_len; // Length of accept_sinPic
BOOL InitServerSend(void)
{
//////////////////////////////////////////////////////////////////
if ((SendConnectSocket = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
// MessageBox (NULL, NULL, TEXT("Send socket() Error"), MB_OK);
return FALSE;
}
//////////////////////////////////////////////////////////////////
// 读配置文件
//--------------------------------------------------------------
if (bind (SendConnectSocket,(struct sockaddr *) &local_sinSend,
sizeof (local_sinSend)) == SOCKET_ERROR)
{
// MessageBox (NULL, NULL, TEXT("Send Socket bind() Error"), MB_OK);
closesocket (SendConnectSocket);
return FALSE;
}
//---------------------------------------------------------------------------
// Establish a socket to listen for incoming connections.
int iRet=listen(SendConnectSocket, SOMAXCONN);
// Stop listening for connections from clients.
accept_sinSend_len = sizeof (accept_sinSend);
SendDataSocket = accept (SendConnectSocket, (struct sockaddr *) &accept_sinSend, (int *) &accept_sinSend_len); // Accept an incoming connection attempt on WinSocket.
closesocket (SendConnectSocket); // Stop listening for connections from clients.
int sendBufLen=0;
int len=sizeof(sendBufLen);
getsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,&len);
sendBufLen=32;
setsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,len);
return TRUE;
}void SendData() // 线程函数
{ unsigned char* bDataRec=(unsigned char*)malloc(1024);
int iReadLen=0; //wzm 2014-12-6
InitServerSend();
while(1)
{
iReadLen = recv (SendDataSocket, (char*)bDataRec, 1024, 0);
if(iReadLen>0)
{
}
else
{
closesocket(SendDataSocket);
InitServerSend();
}
Sleep(1);//60000 WZM 2014-12-3
}
}
AfxBeginThread(AFX_THREADPROC(&SendHandleData), NULL,0);//网口线程void SendHandleData() // 线程函数
{
while(1)
{
if(g_SoftwareState==0)
{
return;
}
if(g_pTeleControlerDlg->m_b_IsSending)
{
send(SendDataSocket,(const char*)sendBuf,32,0);
}
Sleep(4);//60000 WZM 2014-12-7
}
}
int sendBufLen=0;
int len=sizeof(sendBufLen);
getsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,&len);
sendBufLen=32;
setsockopt(SendDataSocket,SOL_SOCKET,SO_SNDBUF,(char*)&sendBufLen,len);
2. 不懂得解析协议的另一种说法。tcp概念里没有包,只有流。设置缓冲变小,当然会导致数据通量变小。
对于发送端,先传数据体长度,然后再串数据体buffer;
对于接收端,先接收数据体长度,再根据数据体长度循环接收数据体buffer;
由于TCP底层可能会根据当前网络拥塞情况,进行切包,所以上层接收时需要多次循环接收,直到接收到指定长度为止。
CxUdpSocket
http://www.cnblogs.com/EdmundDwyane/p/3218838.html
CxClientSocket
http://www.cnblogs.com/EdmundDwyane/p/3248297.html
CxServerSocket
http://www.cnblogs.com/EdmundDwyane/p/3278641.html
不要缩小socket缓冲大小,就可以增加。
send(人多)send(病少)send(财富)
recv(人多病)recv(少财富)
陷阱里面啊!
http://bbs.csdn.net/topics/380167545