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.

I'm not sure what your trying to accomplish here...Are you try to create a unique socket API for both Windows and Linux?

Oh by the way, your Linux function is lacking..What does it return if its successful?

I'm not sure what your trying to accomplish here...Are you try to create a unique socket API for both Windows and Linux?

Oh by the way, your Linux function is lacking..What does it return if its successful?

I'm just creating separate functions for the different steps in socket programming. I already created separate functions for connect(), etc. It's because many functions will consecutively connect to some device through socket programming so I will just call these functions I created. I initially did it on Linux but for some reason I have to transfer them to Windows and those two are some of the versions I've done.

To show you the functions:
Linux version:

int init_socket(int *sockfd) {
  *sockfd = socket(PF_INET,SOCK_STREAM,0);
  if(sockfd<0) {
	perror("socket: ");
	return -EIO;
  }
}

int init_connect(int *sockfd) {
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(80);
  inet_pton(AF_INET,"some IP address",&servaddr.sin_addr);

  if(connect(*sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0){
	perror("connect: ");
	close(sockfd);
	return -EIO;
  }
}

int some_function() {
  init_socket(&sockfd);
  init_connect(&sockfd);
}

The code I'm trying to implement on Windows is similar to the one above, but as I mentioned, the init_socket part is not yet working well and that's what I'm seeking help for.

Regarding the Linux function, it's returning sockfd. I declared it as a pointer to the address of the file descriptor, so that when its value is changed, the value in that address will be changed (pass by reference). Well it's not really returning anything per se, but it's changing some value in a specific address so that when sockfd needs to be used in another function, the value in that address will just be called.

You missed the point. Your function is expected to return an integer

int init_socket(int *sockfd);

But the logic in the body of the function doesn't return anything when socket creation is successful.

If you had this situation below where the socket creation was successful

int ans = init_socket(&sock);

What would the value of ans be?

You missed the point. Your function is expected to return an integer

int init_socket(int *sockfd);

But the logic in the body of the function doesn't return anything when socket creation is successful.

If you had this situation below where the socket creation was successful

int ans = init_socket(&sock);

What would the value of ans be?

The value of ans will be an integer file descriptor, the exact value of which I don't know. Come on man, I have no problem with the Linux version coz it already worked fine for me, I'm asking about the Windows version. To be honest I already tried doing what you're saying: returning sockfd's value to the calling function. But when I tried to print sockfd's value in the init_socket function and in the function that called init_socket, the values were different (meaning sockfd's value wasn't 'returned' properly, that's why I instead passed it by reference, so that the address itself will be used to contain sockfd's value, and no 'returning' is needed.)

I think in the Windows version I need to return the value of ConnectSocket but I don't know how to do it. It's because ConnectSocket is of type SOCKET and how would I name a function that way? SOCKET init_socket? In Linux sockfd is of type int so the function is int init_socket, but in this Windows version I really don't know. Maybe that's where you could suggest to me some ideas/techniques in properly returning its value to the calling function, coz so far I can't do it well.

you should try initializing winsock before performing any winsock functions

WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws);
//do something like
//SOCKET sock;
//sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//...
WSACleanup();

you should try initializing winsock before performing any winsock functions

WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws);
//do something like
//SOCKET sock;
//sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//...
WSACleanup();

Thanks for the reply. I was already able to make the program work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.