Tôi đã theo dõi Beej Networking guide và trong phần máy chủ có một phần mã mà nó đã gọi là hàm fork().Thay thế forking trong cửa sổ
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);
Tôi đang ở trên máy tính và không thể làm việc đó. Tôi có thể làm gì để giải quyết vấn đề này ?. Mã của tôi như sau.
/* 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;
}
Khi bạn mã cho Windows, bạn thường sử dụng một loại suy nghĩ khác. Tôi nghĩ bạn nên nhìn vào hướng dẫn Winsock thay vì hướng dẫn này bạn đang sử dụng. –
câu hỏi liên quan: [cửa sổ điều gần nhất có gì để ngã ba()?] (Http://stackoverflow.com/questions/985281/), [CreateThread vs fork()] (http://stackoverflow.com/questions/619019 /), [fork/chroot tương đương với ứng dụng máy chủ Windows] (http://stackoverflow.com/questions/1686578/), [Microsoft Windows 7 POSIX hiện đang ở đâu?] (Http://stackoverflow.com/questions/4746043 /) –