Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Socket Function in Linux and Windows Help

Status
Not open for further replies.

shinsengumi

Newbie level 4
Joined
Dec 11, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
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)
Code:
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)
Code:
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();
    return 1;
  }  
}
 

- Your functions do not return anything when there was no error...
- for linux, you fill in the address where sockfd is with the new created socket (int* sockfd)
- for windows, you pass ConnectionSocket by value, why not using (SOCKET* ConnectSocket)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top