Winsock:在全局套接字上接受,启动新线程并复制到那里,释放以前的全局套接字

发布于 2024-09-06 19:04:34 字数 1351 浏览 5 评论 0原文

这是纯粹的winsock服务器问题。只是为了说明我已经知道线程是如何工作的。 我有名为 Global 和 Main_Socket 的全局套接字。

long __stdcall newthreadfunction(); //prototype of new thread function

SOCKET Global; // To be shared and copied by thread
SOCKET Main_Socket; //main listening socket

WSADATA wsaData;
sockaddr_in service;


int main() {

........ DO STARTUP THINGS HERE ........

   Main_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 service.sin_family = AF_INET;
 service.sin_addr.s_addr = inet_addr(IP);//"127.0.0.1"
 service.sin_port = htons(PORT);
   bind(Main_Socket, (SOCKADDR*)&service, sizeof(service));

   while(1) {
     Global = SOCKET_ERROR;
     while (Global == SOCKET_ERROR)  {
      Global = accept(Main_Socket, NULL, NULL); 
      CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, 0, 0, 0);  
     }
   }
...........CLEANUP HERE ..............
}

long __stdcall newthreadfunction() {
 SOCKET Local = ?????????????????????; 

 What to do here so that the accepted connection (on Global) gets attached to 
        SOCKET Local and it starts sending and receiving independently from Global and 
        Main_Socket ??????>>>>>>>>>>>>>>>>
}

我想做的是,每当我在套接字 m_socket 上收到新连接时,我都会在套接字全局上接受它,然后启动新线程,然后我希望线程内的本地套接字开始使用全局上接受的套接字,仅此而已。

快速回答或修复将比链接更受欢迎。

再见

It is pure winsock server question. Just to clear that I already know how threads work.
I have global sockets called Global and Main_Socket.

long __stdcall newthreadfunction(); //prototype of new thread function

SOCKET Global; // To be shared and copied by thread
SOCKET Main_Socket; //main listening socket

WSADATA wsaData;
sockaddr_in service;


int main() {

........ DO STARTUP THINGS HERE ........

   Main_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 service.sin_family = AF_INET;
 service.sin_addr.s_addr = inet_addr(IP);//"127.0.0.1"
 service.sin_port = htons(PORT);
   bind(Main_Socket, (SOCKADDR*)&service, sizeof(service));

   while(1) {
     Global = SOCKET_ERROR;
     while (Global == SOCKET_ERROR)  {
      Global = accept(Main_Socket, NULL, NULL); 
      CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)newthreadfunction, 0, 0, 0);  
     }
   }
...........CLEANUP HERE ..............
}

long __stdcall newthreadfunction() {
 SOCKET Local = ?????????????????????; 

 What to do here so that the accepted connection (on Global) gets attached to 
        SOCKET Local and it starts sending and receiving independently from Global and 
        Main_Socket ??????>>>>>>>>>>>>>>>>
}

What I am trying to do is whenever I receive new connection on socket m_socket I accept it on socket Global, then start new thread and then I want a Local socket within thread to start using accepted socket on Global, and that's all.

A quick answer or fix will be appreciated rather than links.

Bye

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-09-13 19:04:34

不能将新套接字(GLOBAL)作为参数传递给CreateThread吗?

Can't you pass the new socket (GLOBAL) as a parameter to CreateThread?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文