First of all, allow me to say this is by no means an expert problem, so if this is not the place to post it forgive me, but it seemed much more relevant here than in the beginners section. What I am trying to do is simple enough, I'm trying to get the hang of socket programming in C, with the end goal is being able to send a single bit of information from one computer to another. I've read through Beej's guide to sockets about half a dozen times (trying to make sure I understand as much of it as I can, the rest time will do) up to the point where the definitions are put aside and the actual functions begin, and that's where my problems begin aswell. Now with the introduction out of the day, here's my problem. Whenever I call socket, it returns an error (-1), however errno and perror state there is no error. I am thinking it's one of those head-wall cases where a single misplaced character puts you off for hours, but that's just my non-expert analysis. Either way, here is the code (not much, but getting there):
#include <winsock.h>
#include <dos.h>
int main( int argc, char* argv[])
{
struct sockaddr_in scAdr;
int sockfd, sError;
scAdr.sin_family = AF_INET;
scAdr.sin_port = htons(5432);
scAdr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(scAdr.sin_zero), '\0', 8);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket Result: %d\n",sockfd);
printf("Errno: %d\n",errno);
perror("socket");
sError = bind(sockfd, (struct sockaddr*)&scAdr, sizeof(struct sockaddr));
printf("Bind Result: %d\n",sError);
printf("Errno: %d\n",errno);
perror("socket");
system("PAUSE");
return 0;
}
For which the output would be:
Socket Result: -1
Errno: 0
socket: No error
Bind Result: -1
Errno: 0
socket: No error
Obviously something is wrong here, most likely due to my error. Either that or I just descovered the mythical -1 port. Anyhow, thanks in advance :)