#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;
}
🔒 Locked
Winsock
Advertisement
Original Post
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. 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.
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]
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]
Welcome to the world of nasty little differences between linux and windows sockets :)
I added this define to solve the problem you describe:
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]
I added this define to solve the problem you describe:
#if defined(WIN32)#define s_addr S_un.S_addr#endifAs 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]
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
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
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.
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)
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)
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
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
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.
im not a winsock superhero, but arent you supposed to use the SOCKET_ERROR constant instead of -1?
yes actually he should use SOCKET_ERROR but this is not the problem since socket_error is declared as (-1)
in my library i did typedefs and cross platform declaration of all used types in conjuction with sockets
why? because i personally think using the provided socket routines all over again is a mess and leads to loss of productivity
in my library i did typedefs and cross platform declaration of all used types in conjuction with sockets
why? because i personally think using the provided socket routines all over again is a mess and leads to loss of productivity
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement