18,363
社区成员




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
}
}