Hello all. Can anyone here help me create a separate function for the socket() API in socket programming? I successfully created one in Linux before but so far I can't do the same in Windows. The Windows version can't successfully connect using the separate socket() function. My primary suspect is because the socket file descriptor in Linux is of type INT while in Windows's Winsock, the data type is SOCKET. Here are the two versions of the socket() function I created:
By the way, I'm using C programming.
Linux version (working fine):
(as you can see, sockfd is of type int)
int init_socket(int *sockfd) {
*sockfd = socket(PF_INET,SOCK_STREAM,0);
if(sockfd<0) {
perror("socket: ");
return -EIO;
}
}
Windows version (not yet working fine):
(as you can see, ConnectSocket is of type SOCKET)
int init_socket(SOCKET ConnectSocket) {
ConnectSocket = socket(PF_INET, SOCK_STREAM, 0);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
system("PAUSE");
return 1;
}
}
Can anyone give me advice on how to fix the windows version of socket()? Answers will be greatly appreciated. Thank you.