Skip to content

Instantly share code, notes, and snippets.

@fortheday
Last active January 23, 2018 03:16
Show Gist options
  • Save fortheday/ce96a0e30f9c6b0e2571ad92096de7ab to your computer and use it in GitHub Desktop.
Save fortheday/ce96a0e30f9c6b0e2571ad92096de7ab to your computer and use it in GitHub Desktop.
// C++Builder code
struct sockaddr_in addr;
SOCKET s;
int i;
memset(&addr, NULL, sizeof(struct sockaddr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
i = bind(s, (sockaddr *)&addr, sizeof(addr));
if (i == 0) {
i = sizeof(struct sockaddr);
getsockname(s, (sockaddr *)&addr, &i);
ShowMessage(AnsiString(ntohs(addr.sin_port)));
}
closesocket(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment