替代 Windows 中的分叉
我一直在关注 Beej Networking 指南 和服务器部分有部分代码调用了函数 fork()。
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
我在一台 Windows 机器上,无法让该部分工作。我能做什么来解决这个问题? 我的代码如下。
/* Server */
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <sys/types.h>
using namespace std;
const int winsockVersion = 2;
#define BACKLOG 10
#define PORT "3000"
int main(void){
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){
cout<<"-WSAStartup initialized..." << endl;
int status;
int sockfd, new_fd;
const char yes = '1';
struct addrinfo hints, *res,*loop_find;
struct sockaddr_storage their_addr;
socklen_t addr_size;
memset(&hints,0,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ( (status = getaddrinfo(NULL,PORT,&hints,&res)) == 0 ){
cout<<"-Call to get addrinfo successful!." << endl;
}
for (loop_find = res; loop_find!=NULL; loop_find = loop_find->ai_next){
if ( (sockfd = socket(loop_find->ai_family,loop_find->ai_socktype,loop_find->ai_protocol) ) == -1 ){
cout<<"-Could not create socket." << endl;
continue;
}else{
cout<<"-Socket Created." << endl;
}
//clearing in use ports.
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
cout<<"-Couldnt clear blocked port." << endl;
perror("setsockopt");
exit(1);
}
if( bind(sockfd,loop_find->ai_addr,loop_find->ai_addrlen) == -1 ){
closesocket(sockfd);
perror("server: bind");
continue;
}
break;
}
if (listen(sockfd,BACKLOG) != -1){
cout<<"-Listening for incoming connections.";
}
//accept loop.
while(true){
socklen_t addr_size = sizeof their_addr;
new_fd = accept(sockfd,(sockaddr*)&their_addr,&addr_size);
if ( new_fd == -1 ){
perror("accept");
continue;
}
struct sockaddr new_addr;
int len = sizeof new_addr;
getpeername(new_fd,&new_addr,&len);
cout<<"-Connected to " << new_addr.sa_data << endl;
if(!fork()){ //this is a child process
closesocket(sockfd);
if (send(new_fd,"hello world!!",13,0) == -1){
perror("send");
closesocket(new_fd);
exit(0);
}
}
closesocket(new_fd);
}
}
//clear stuff
if( WSACleanup() != 0){
cout<<"-WSACleanup unsuccessful" << endl;
}else{
cout<<"-WSACleanup successful" << endl;
}
return 0;
}
I've been following Beej Networking guide and in the server section there is portion of code where it has called a function fork().
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!", 13, 0) == -1)
perror("send");
close(new_fd);
exit(0);
I'm on a windows machine and cant get that part working. What can I do to solve this?.
My code is as follows.
/* Server */
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <sys/types.h>
using namespace std;
const int winsockVersion = 2;
#define BACKLOG 10
#define PORT "3000"
int main(void){
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){
cout<<"-WSAStartup initialized..." << endl;
int status;
int sockfd, new_fd;
const char yes = '1';
struct addrinfo hints, *res,*loop_find;
struct sockaddr_storage their_addr;
socklen_t addr_size;
memset(&hints,0,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ( (status = getaddrinfo(NULL,PORT,&hints,&res)) == 0 ){
cout<<"-Call to get addrinfo successful!." << endl;
}
for (loop_find = res; loop_find!=NULL; loop_find = loop_find->ai_next){
if ( (sockfd = socket(loop_find->ai_family,loop_find->ai_socktype,loop_find->ai_protocol) ) == -1 ){
cout<<"-Could not create socket." << endl;
continue;
}else{
cout<<"-Socket Created." << endl;
}
//clearing in use ports.
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
cout<<"-Couldnt clear blocked port." << endl;
perror("setsockopt");
exit(1);
}
if( bind(sockfd,loop_find->ai_addr,loop_find->ai_addrlen) == -1 ){
closesocket(sockfd);
perror("server: bind");
continue;
}
break;
}
if (listen(sockfd,BACKLOG) != -1){
cout<<"-Listening for incoming connections.";
}
//accept loop.
while(true){
socklen_t addr_size = sizeof their_addr;
new_fd = accept(sockfd,(sockaddr*)&their_addr,&addr_size);
if ( new_fd == -1 ){
perror("accept");
continue;
}
struct sockaddr new_addr;
int len = sizeof new_addr;
getpeername(new_fd,&new_addr,&len);
cout<<"-Connected to " << new_addr.sa_data << endl;
if(!fork()){ //this is a child process
closesocket(sockfd);
if (send(new_fd,"hello world!!",13,0) == -1){
perror("send");
closesocket(new_fd);
exit(0);
}
}
closesocket(new_fd);
}
}
//clear stuff
if( WSACleanup() != 0){
cout<<"-WSACleanup unsuccessful" << endl;
}else{
cout<<"-WSACleanup successful" << endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与现有的答案(来自 OJ 和 Vincent Robert)相反,fork() 确实存在于 Windows 的高端版本中。它是基于 UNIX 的应用程序子系统 (SUA) 的一部分,之前称为Microsoft Windows Services for UNIX (SFU),以前称为Interix。
引用 http://en.wikipedia.org/wiki/Interix,SUA 可在
企业版) - 版本 6.0
版本 6.0
Windows 7 - 版本 6.1
要使用 fork(),您只需安装免费的 SUA SDK。根据您的目标系统,您需要以下之一:
子系统套件 (SDK)
基于 UNIX 的应用程序
Microsoft 中基于 UNIX 的应用程序
Windows Vista RTM/Windows Vista SP1
和 Windows Server 2008 RTM
Microsoft 中基于 UNIX 的应用程序
Windows 7 和 Windows Server 2008
R2
您还可以查看Interix实现fork()吗?
Contrary to both existing answers (from OJ and Vincent Robert) fork() does exist on high-end versions of Windows. It's part of Subsystem for UNIX-based Applications (SUA) earlier called Microsoft Windows Services for UNIX (SFU), earlier called Interix.
Citing http://en.wikipedia.org/wiki/Interix, SUA is available on
Enterprise editions) - version 6.0
version 6.0
Windows 7 - version 6.1
All you have to do to use fork() is to install free SUA SDK. Depending on your target system you need one of the following:
Kit (SDK) for Subsystem for
UNIX-based Applications
UNIX-based Applications in Microsoft
Windows Vista RTM/Windows Vista SP1
and Windows Server 2008 RTM
UNIX-based Applications in Microsoft
Windows 7 and Windows Server 2008
R2
You can also take a look at Does Interix implement fork()?
fork() 显然在 Windows 上不存在。相反,您需要创建一个新线程 ,或全新流程。
fork() obviously doesn't exist on Windows. Instead you'll need to create a new thread, or a whole new process.
Windows 上不存在
fork
。您必须使用名为CreateProcess 的特定于 Windows 的 API
。与
fork
相反,CreateProcess
需要EXE的路径。您可以通过调用GetModuleFileName
带有 NULL 参数。fork
does not exist on Windows. You have to use a Window specific API calledCreateProcess
.Contrary to
fork
,CreateProcess
needs the path to the EXE. You can retrieve the path of the current EXE by callingGetModuleFileName
with a NULL parameter.