码迷,mamicode.com
首页 > 其他好文 > 详细

SIp Socket

时间:2014-08-03 08:04:55      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:网络连接   服务器   socket   server   大循环   

UINT AMILinkThread(LPARAM lparam)

{

CCallSchedulingSystemDlg *Dlg = (CCallSchedulingSystemDlg *)lparam;


//大循环,出现网络连接断开或点击设置中的应用后,会重新连接服务器,并生成新的socket

while(Dlg->AMIlinking)

{

closesocket(Dlg->AMIsock);


//连接server

Dlg->AMIsock=socket(AF_INET,SOCK_STREAM,0);


SOCKADDR_IN addrSrv;

addrSrv.sin_addr.S_un.S_addr=inet_addr(Dlg->m_strServer.GetString());

addrSrv.sin_family=AF_INET;

addrSrv.sin_port=htons(5038);


int error=-1, len;

len = sizeof(int);

timeval tm;

fd_set set;

unsigned long ul = 1;

ioctlsocket(Dlg->AMIsock, FIONBIO, &ul);  //设置为非阻塞模式

bool ret = false;

int iRet = connect(Dlg->AMIsock, (struct sockaddr *)&addrSrv, sizeof(addrSrv));

if( iRet == -1)

{

tm.tv_sec  = 8;  //超时时间8秒

tm.tv_usec = 0;

FD_ZERO(&set);

FD_SET(Dlg->AMIsock, &set);

if( select(Dlg->AMIsock+1, NULL, &set, NULL, &tm) > 0)

{

char bb[128] = {0};

int iRet = getsockopt(Dlg->AMIsock, SOL_SOCKET, SO_ERROR, bb, (socklen_t *)&len);

if(iRet == 0)

ret = true;

else 

ret = false;

else 

{

ret = false;

}

}

else

{

ret = true;

}

//ul = 0;

//ioctlsocket(Dlg->AMIsock, FIONBIO, &ul); //设置为阻塞模式

if(!ret) 

{

//连接失败

//closesocket(Dlg->AMIsock);

continue; //重新连接

}


memset(Dlg->lastbuff, 0 , sizeof(Dlg->lastbuff)); 


mysql_close(&(Dlg->mysql));

if(!mysql_real_connect(&(Dlg->mysql), Dlg->m_strServer.GetString(),"operator","123456",NULL,3306,NULL,0)) //mydb为你所创建的数据库,3306为端口号,可自行设定 

{

//AfxMessageBox("数据库连接失败");

Dlg->isConnectd = FALSE;

continue;

}

else

{

Dlg->isConnectd = TRUE;

if(!mysql_real_connect(&(Dlg->myAlarmSql),Dlg->m_strServer,"operator","123456",NULL,3306,NULL,0))

{

Dlg->isConnectd = FALSE;

continue;

}

}

Dlg->canUsersInit = 1;


//socket缓存

char lastbuff[BUFFERSIZE] = {0};

char buff[BUFFERSIZE] = {0};

len = 0;


//接收数据

fd_set rset;

int nReady;

struct timeval timeout;

while(Dlg->AMIlinking)

{

FD_ZERO(&rset);

timeout.tv_sec = 0;

timeout.tv_usec = 200;


//listen socket 加入fdset

FD_SET(Dlg->AMIsock, &rset); 

nReady = select(Dlg->AMIsock+1, &rset, NULL, NULL, &timeout);

if(nReady == 0) //超时

{

continue;

}

else if(nReady == -1)  //出错

{

//closesocket(Dlg->AMIsock);

Dlg->isConnectd = FALSE;

break;

}

else if( FD_ISSET(Dlg->AMIsock,&rset))

{

Dlg->canGetMessagedata = 1;

//Dlg->GetMessagedata();


/*int result = recv(Dlg->AMIsock,buff+len,BUFFERSIZE-len-1,0);

//如果网络连接断开

if(SOCKET_ERROR == result)

{

//AfxMessageBox("SOCKET连接出错");

break;

}

else if(result > 0)

{

CString str;

if(strstr(buff,"Asterisk Call Manager"))

{

str = "Action: login\r\nUsername: phpagi\r\nSecret: phpagi\r\n\r\n";

send(Dlg->AMIsock,str.GetBuffer(0),str.GetLength(),0);


continue;

}

else if(strstr(buff, "Authentication accepted"))

{

//str = "Action: sippeers\r\n\r\naction: DAHDIShowChannels\r\n\r\naction: CoreShowChannels\r\n\r\nAction: MeetmeList\r\nConference: "+m_strUser+"\r\n\r\n";

str = "Action: sippeers\r\n\r\naction: DAHDIShowChannels\r\n\r\naction: CoreShowChannels\r\n\r\nAction: MeetmeList\r\n\r\n";

send(Dlg->AMIsock,str.GetBuffer(0),str.GetLength(),0);


continue;

}


//处理

char *head = buff;

char *pStr = NULL;


//处理head指向的内存的过程

while((pStr = strstr(head, "\r\n\r\n")))

{

*(pStr+2) = ‘\0‘;


Dlg->StrToEvent(head);

Dlg->EventProcess();


head = pStr + 4;

if(*head == ‘\0‘)

break;

}


memset(lastbuff,0,BUFFERSIZE);

len = strlen(head);

if(*head != ‘\0‘)

{

memcpy(lastbuff, head, len);

}

}*/

}

}


//连接断开

//closesocket(Dlg->AMIsock);

Dlg->isConnectd = FALSE;

}


return 0;

}


本文出自 “日知其所无” 博客,谢绝转载!

SIp Socket,布布扣,bubuko.com

SIp Socket

标签:网络连接   服务器   socket   server   大循环   

原文地址:http://shenyantao.blog.51cto.com/1957956/1534116

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!