Unix to Windows Porting Dictionary for HPC

Links

Function List

bind


Unix

header file: socket.h, types.h

int bind(int s, const struct sockaddr *addr, int addrlen);

Windows

header file: winsock2.h

int bind(SOCKET s, const struct sockaddr *name, int namelen);

Purpose

The bind function assigns a socket address to an unnamed socket.

Discussion

The bind() function call is very similar between Unix and Windows. A major difference is the socket 's' provided to the function has a different type. With Unix the socket is an integer type to represent a file handle. On Windows the socket is a SOCKET type representing a socket handle. In practical terms this means that, unlike on Unix, the socket can only be passed to other socket functions.

Example of Use in Windows

#include <winsock2.h>
#include <windows.h>

SOCKET s;
sockaddr_in name;

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
        /* error creating socket */
        return;
}

name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr("127.0.0.1"):
name.sin_port = htons(12345);

ret = bind(s, &name, sizeof(name));
if (ret == 0) {
        /* successful bind */
}
else {
        /* there was a problem, use WSAGetLastError to get the error */
        return;
}

  
blog comments powered by Disqus