void CServerDlg::OnBnClickedButton1()
{
/////////////////////////////////////////////////
int m_iServerSocket = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN m_rcvaddr;
memset((void *)&m_rcvaddr, 0, sizeof(SOCKADDR_IN));
m_rcvaddr.sin_family = AF_INET;
m_rcvaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
m_rcvaddr.sin_port = htons(1000);
//绑定
bind(m_iServerSocket, (struct sockaddr *)&m_rcvaddr, sizeof(SOCKADDR_IN));
//监听
listen(m_iServerSocket, 5);
//客户端
SOCKET cmdsrv = (int)accept(m_iServerSocket,NULL,NULL);
if(cmdsrv == INVALID_SOCKET)
{
int err=GetLastError();
return;
}
char buf[1024];
//接收数据
int rxn = recv(cmdsrv, buf, 1024,0);
buf[rxn] = '\0';
AfxMessageBox(buf);
sprintf(buf, "你也好,再见");
send(cmdsrv, buf, strlen(buf), 0);
closesocket(m_iServerSocket);
}
此段代码其他机器都好使,但在我电脑上不行,socket返回值总是-1,环境都是用的VS2010,初学者,遇到这种莫名其妙的问题,求指教~
富国沪深
尚方宝剑之说