Jump to content
  • Advertisement
Sign in to follow this  
Daedalus AI

Winsock

This topic is 4825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I've done socket programming before in Linux and I'm now trying to learn how to do it in windows using winsock. But I'm absolutely stumped on it now, as it just wont work.
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

#define NET_OK	0
#define NET_ER	-1

#define PORT 1985

void GreetUser(void)
{
    puts("Welcome to Andy's Network\n");
    printf("You are running a Server on Port %d\n", PORT);
}

int main(int argc, char* argv[])
{
    int FromLen;

    SOCKET Server, Client;
    WSADATA WinSockData;

    struct sockaddr_in MySockAddr, From;

    if ( (WSAStartup(0x101, &WinSockData)) != 0 )
    {
	puts("WSA has failed");

	return NET_ER;
    }

    FromLen = sizeof(From);
    MySockAddr.sin_family = AF_INET;
    MySockAddr.sin_addr.s_addr = INADDR_ANY;
    MySockAddr.sin_port = htons(PORT);

    if ( (socket(AF_INET, SOCK_STREAM, 0)) == -1 )
    {
	puts("Socket has failed");

	return NET_ER;
    }

    if ( (bind(Server, (sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )
    {
	puts("Bind has failed");

        return NET_ER;
    }

    if ( (listen(Server, 10)) != 0 )
    {
	puts("Listen has failed");

        return NET_ER;
    }

    while (1)
    {
        Client = accept(Server, (sockaddr*) &From, &FromLen);

        printf("Your IP is %s\n", (inet_ntoa(From.sin_addr)) );

        send(Client, "Hello", strlen("Hello"), 0);
        printf("Connection from %s\n", (inet_ntoa(From.sin_addr)) );
		
        closesocket(Client);

    }

    closesocket(Server);

    WSACleanup();

    return NET_OK;
}

It kicks up the problem about using sockaddr on line 44 (for some reason not also line 60), about not declaring it (if i declare, I get aload of syntax errors). Any help would greatly be appreciated, Thanx Daed.

Share this post


Link to post
Share on other sites
Advertisement
Enigma addresses it below.

if ( (bind(Server, (sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )

should change to

if ( (bind(Server, (struct sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )

Client = accept(Server, (sockaddr*) &From, &FromLen);

should be

Client = accept(Server, (struct sockaddr*) &From, &FromLen);

[Edited by - nprz on August 29, 2005 11:53:01 AM]

Share this post


Link to post
Share on other sites
Welcome to the world of nasty little differences between linux and windows sockets :)

I added this define to solve the problem you describe:

#if defined(WIN32)
#define s_addr S_un.S_addr
#endif

As you can see, windows uses a different name for s_addr. More info in the MSDN (so you can create a solution more elegant than mine :).

Tom

EDIT: Ok, enigma obviously counts alot better than I do, but as the MSDN entry shows, I'm not making this up...

[Edited by - dimebolt on August 29, 2005 9:03:15 AM]

Share this post


Link to post
Share on other sites
Assuming I can count, your error is on this line:
if ( (bind(Server, (sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )
and occurs because you omitted the struct keyword before sockaddr in your cast. You will also need to fix it on line 60. The reason your compiler only gave one error is because compilers are typically configured to only raise one error per undefined token.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
Enigma addresses it below.

if ( (bind(Server, (sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )

should change to

if ( (bind(Server, (struct sockaddr*) &MySockAddr, sizeof(struct sockaddr))) != 0 )

Client = accept(Server, (sockaddr*) &From, &FromLen);

should be

Client = accept(Server, (struct sockaddr*) &From, &FromLen);


Yeah, I did that initially. But it keeps failing at Bind, so I assumed I was wrong. Any ideas as to why bind should fail?

As far as I can see, I'm not trying anything weird.

Thanx for the help so far, Daed.

Share this post


Link to post
Share on other sites
This line:
if ( (socket(AF_INET, SOCK_STREAM, 0)) == -1 )

Should be:
if ( (Server = socket(AF_INET, SOCK_STREAM, 0)) == -1 )

I recommend getting a little familiar with int WSAGetLastError(). Maybe use this page for lists of errors. The one bind returned was 10038: Socket operation on non-socket.
This is because you forgot to put the socket returned into Server.

(I had to do some comparisons with my code and your code as well to see the difference)

Share this post


Link to post
Share on other sites
Alright BUD [grin]

The problem here dude, when i compiled your code on .NET 2003 was that you were using the SOCKET variables without them being defined.

So initialise both your SOCKETs to 0 before you use them,

Other than that there was nothing wrong with your code at all. It just runs an drops out.

ace

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
This line:
if ( (socket(AF_INET, SOCK_STREAM, 0)) == -1 )

Should be:
if ( (Server = socket(AF_INET, SOCK_STREAM, 0)) == -1 )

I recommend getting a little familiar with int WSAGetLastError(). Maybe use this page for lists of errors. The one bind returned was 10038: Socket operation on non-socket.
This is because you forgot to put the socket returned into Server.

(I had to do some comparisons with my code and your code as well to see the difference)


Ah you legend, I knew it would be something simple. Can't believe I've missed it out.

Thanx Daed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!