Dear programmers,
i'm trying to make a internet connection in a C program using Winsock. I found a great tut (Programming Windows TCP Sockets in C++ for the Beginner - CodeProject) but everything did not work as aspected. I have a winsock error n.o. 10049. When I search for this i see this : "The requested address is not valid in its context."
(source:
when I try to set up a loopback connection it gives no errors, but doesn't work either: he always think that there is a connectioncall (other words: the program doesn't "hang"). What am I doing wrong?
here is the server code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock.h>
int ListenOnPort(int portno);
int main()
{
int errorcode=0;
printf("Hello world!\n");
// while(1)
errorcode=ListenOnPort(6221);
printf("errorcode is %d", errorcode);
return 0;
}
//LISTENONPORT – Listens on a specified port for incoming connections
//or data
int ListenOnPort(int portno)
{
SOCKET s;
WSADATA w;
int error = WSAStartup (0x0202, &w); // Fill in WSA info
if (error)
{
return 50; //For some reason we couldn't start Winsock
}
if (w.wVersion != 0x0202) //Wrong Winsock version?
{
WSACleanup ();
return 51;
}
SOCKADDR_IN addr; // The address structure for a TCP socket
addr.sin_family = AF_INET; // Address family
addr.sin_port = htons (portno); // Assign port to this socket
//Accept a connection from any IP using INADDR_ANY
//You could pass inet_addr("0.0.0.0") instead to accomplish the
//same thing. If you want only to watch for a connection from a
//specific IP, specify that //instead.
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
if (s == INVALID_SOCKET)
{
return 52; //Don't continue if we couldn't create a //socket!!
}
if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
wprintf(L"bind failed with error %u\n", WSAGetLastError());
closesocket(s);
WSACleanup();
//We couldn't bind (this will happen if you try to bind to the same
//socket more than once)
return 53;
}
//Now we can start listening (allowing as many connections as possible to
//be made at the same time using SOMAXCONN). You could specify any
//integer value equal to or lesser than SOMAXCONN instead for custom
//purposes). The function will not //return until a connection request is
//made
listen(s, SOMAXCONN);
printf("Done\n");
//Don't forget to clean up with CloseConnection()!
}
kind regards,
Libpgeak
EDIT: when I change the IPaddres to 192.168.0.28 I get this error.