kbhao7788
kbhao7788
采纳率19.8%
2017-02-25 10:23 阅读 1.1k

c++ socket网络编程问题怎么解决啊

#include
#include
#pragma comment(lib,"ws2_32.lib")
#include
using namespace std;
const short POSO = 2058;
void main2(void pl)
{
char hnp[100];
SOCKET *son = (SOCKET
)pl;
SOCKET son2 = *son;
SOCKET soka= recv(son2, hnp, sizeof(hnp) - 1, 0);
if (soka == SOCKET_ERROR)
{
cout << "错误码是:" << WSAGetLastError() << endl;
return ;
}
cout << hnp << endl;
}
int main()
{
WSADATA wd;
WSAStartup(0x0202, &wd);

SOCKET so = socket(AF_INET, SOCK_STREAM, 0);
if (so == INVALID_SOCKET)
{
    cout << "错误码是:" << WSAGetLastError() << endl;
}
sockaddr_in sa = { AF_INET, htons(POSO) };

int n=bind(so, (sockaddr*)&sa, sizeof(sa));
if (n==SOCKET_ERROR)
{
    cout <<"错误码是:"<< WSAGetLastError() << endl;
}
n=listen(so, 5);
int hn = sizeof(sa);
if (n==SOCKET_ERROR)
{
    cout << "错误码是:" << WSAGetLastError() << endl;
}

char hnp[70];
while (true)
{

    SOCKET son= accept(so, NULL, NULL);
    if (n == SOCKET_ERROR)
    {
        cout << "错误码是:" << WSAGetLastError() << endl;
    }
    _beginthread(main2, 0, &son);


}

}
为什么第一次调试遇到_beginthread(main2, 0, &son); 不进mian2的里面,而是while循环的第二次开始就进main2里面

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

相关推荐