VC++ 用Socket怎样编持续接收数据的程序
目前只是编好了接受一次的程序#include"afxsock.h"#include"SetPort.h"CWinThread*pThread=NULL;boolm_exi...
目前只是编好了接受一次的程序
#include "afxsock.h"
#include "SetPort.h"
CWinThread *pThread = NULL;
bool m_exit = false;
unsigned int StartServer(LPVOID lParam)
{
if (!AfxSocketInit())
{
if(g_pEdit) g_pEdit->wShow("\r\n Socket初始化失败! \r\n");
return 1;
}
m_exit = false;
//socket------------------------------------------------
CSocket aSocket, serverSocket;
if (!aSocket.Socket())
{
char szError[256] = {0};
sprintf(szError, "Create Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
aSocket.SetSockOpt(SO_REUSEADDR, (void *)&bOptVal, bOptLen, SOL_SOCKET);
if (!aSocket.Bind(1500))
{
char szError[256] = {0};
sprintf(szError, "Bind Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
if(!aSocket.Listen(10))
{
char szError[256] = {0};
sprintf(szError, "Listen Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
CString strText;
if(g_pEdit) g_pEdit->wShow("\r\n GPRS连接已打开! \r\n");
strText += "Server Start! \r\n";
while(!m_exit)
{
if(!aSocket.Accept(serverSocket))
{
continue;
}
else
{
char szRecvMsg[256] = {0};
char szOutMsg[256] = {0};
serverSocket.Receive(szRecvMsg, 256);
sprintf(szOutMsg, "Receive Msg: %s \r\n", szRecvMsg);
strText += szOutMsg;
serverSocket.Send("Have Receive The Msg", 50);
serverSocket.Close();
}
}
aSocket.Close();
serverSocket.Close();
strText += "Have Close!";
return 0;
}
只是可以接受一次数据,怎样改下可以持续监听,只要有数据连接就接受,直到不再需要就收数据。拜托各位大神了!希望给出详细的程序,谢谢 展开
#include "afxsock.h"
#include "SetPort.h"
CWinThread *pThread = NULL;
bool m_exit = false;
unsigned int StartServer(LPVOID lParam)
{
if (!AfxSocketInit())
{
if(g_pEdit) g_pEdit->wShow("\r\n Socket初始化失败! \r\n");
return 1;
}
m_exit = false;
//socket------------------------------------------------
CSocket aSocket, serverSocket;
if (!aSocket.Socket())
{
char szError[256] = {0};
sprintf(szError, "Create Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
aSocket.SetSockOpt(SO_REUSEADDR, (void *)&bOptVal, bOptLen, SOL_SOCKET);
if (!aSocket.Bind(1500))
{
char szError[256] = {0};
sprintf(szError, "Bind Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
if(!aSocket.Listen(10))
{
char szError[256] = {0};
sprintf(szError, "Listen Faild: %d", GetLastError());
AfxMessageBox(szError);
if(g_pEdit) g_pEdit->wShow(szError);
return 1;
}
CString strText;
if(g_pEdit) g_pEdit->wShow("\r\n GPRS连接已打开! \r\n");
strText += "Server Start! \r\n";
while(!m_exit)
{
if(!aSocket.Accept(serverSocket))
{
continue;
}
else
{
char szRecvMsg[256] = {0};
char szOutMsg[256] = {0};
serverSocket.Receive(szRecvMsg, 256);
sprintf(szOutMsg, "Receive Msg: %s \r\n", szRecvMsg);
strText += szOutMsg;
serverSocket.Send("Have Receive The Msg", 50);
serverSocket.Close();
}
}
aSocket.Close();
serverSocket.Close();
strText += "Have Close!";
return 0;
}
只是可以接受一次数据,怎样改下可以持续监听,只要有数据连接就接受,直到不再需要就收数据。拜托各位大神了!希望给出详细的程序,谢谢 展开
2个回答
展开全部
你这种是用的阻塞模式,就是 Accept和Receive 都会卡住不动,直到有新连接或者有新数据。
实际使用场景中,阻塞模式,一个线程专门Accept 有新的连接之后,为每一个连接再创建一个线程来处理 Receive,也就是对于服务器来说,假设当前有10个工作的连接,那么至少需要11个线程。
你只需要开几个专门的线程来负责接受连接和接收数据就可以了。
这种阻塞模式不适合大并发量的网络程序,测试小程序没问题,大并发量时需要使用非阻塞模式,比如一般常用的select模式
百度 “select模型” 就可以搜到。
实际使用场景中,阻塞模式,一个线程专门Accept 有新的连接之后,为每一个连接再创建一个线程来处理 Receive,也就是对于服务器来说,假设当前有10个工作的连接,那么至少需要11个线程。
你只需要开几个专门的线程来负责接受连接和接收数据就可以了。
这种阻塞模式不适合大并发量的网络程序,测试小程序没问题,大并发量时需要使用非阻塞模式,比如一般常用的select模式
百度 “select模型” 就可以搜到。
追问
可以有具体实例发给我吗?
2016-01-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
VC++ 用Socket持续接收数据的程序举例如下:
while(1) { //client receiving code
if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
perror("recv");
}
buf[numbytes] = '\0';
printf("numbytes is %d\n", numbytes);
printf("client: received '%s'\n", buf);
}
以下是输出:
numbytes is 0
client: received '12345'
numbytes is 0
client: received '6'
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
while(1) { //client receiving code
if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
perror("recv");
}
buf[numbytes] = '\0';
printf("numbytes is %d\n", numbytes);
printf("client: received '%s'\n", buf);
}
以下是输出:
numbytes is 0
client: received '12345'
numbytes is 0
client: received '6'
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
numbytes is 0
client: received ''
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询