Winsock:在全局套接字上接受,启动新线程并复制到那里,释放以前的全局套接字
这是纯粹的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不能将新套接字(GLOBAL)作为参数传递给CreateThread吗?
Can't you pass the new socket (GLOBAL) as a parameter to CreateThread?