Need help with sockets

[b][red]This message was edited by the KerMatus at 2002-3-31 14:6:56[/red][/b][hr]
OK, this is the first program I've written to use sockets, so I'm not sure what I'm doing wrong. Can anyone tell me what's wrong with it?

Basically, it's just supposed to get a random local address and start listening on port 3490 and tries to receive a message.
Also, I'm using VC++ 6.0 on win98 and I have made the program to send the message and it works.

//--------------------------------------------
#include
#include
#include
#include
#include
// wsock32.lib

#define MYPORT = 3490
#define BACKLOG = 5

int main()
{
WSADATA wsaData;

if(WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed
");
exit(1);
}

int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char tmp;

cout.sync_with_stdio();
cout.rdbuf()->setbuf(0,0);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // error #C2143,C2660,C2059
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '', 8);

bind(sockfd, (sockaddr *)&my_addr, sizeof(sockaddr));

listen(sockfd, BACKLOG); // error C2059

sin_size = sizeof(sockaddr_in);
new_fd = accept(sockfd, (sockaddr *)&their_addr, &sin_size);


char buff[30];
if(recv(sockfd, buff, sizeof(buff), 0) == -1)
{
"ERROR: Receiving
";
}
else
cout << buff << endl;

while(!_kbhit());
tmp = getch();

return 0;
}
//--------------------

Thanks,
Alex


Comments

  • : this is the first program I've written to use sockets, so I'm not sure what I'm doing wrong.

    You error has nothing to do with sockets:
    [code=ffffff]
    [color=a020f0]#define MYPORT = [/color][color=bb0000]3490[/color]
    [color=a020f0]#define BACKLOG = [/color][color=bb0000]5[/color]
    [/code]
    You've define MYPORT as "= 3490". So, later, you try to compile the line:
    [code=ffffff]
    my_addr.sin_port = htons(= [color=bb0000]3490[/color]); [color=80a0b0][italic]// error #C2143,C2660,C2059[/italic][/color]
    [/code]
    Can you see why the compiler might compain about that?

    Cheers,
    Eric

  • DUH!!
    Sorry, I don't know how I missed that. When I re-wrote the code before reading your post I didn't make that mistake.

    Here's the code for anyone who is interested, as rough as it is:

    //---------------------
    #include
    #include
    #include
    // wsock32.lib

    #define MYPORT 3490
    #define BACKLOG 10

    int main()
    {
    WSADATA wsaData;

    if(WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup failed
    ");
    exit(1);
    }

    cout.sync_with_stdio();
    cout.rdbuf()->setbuf(0,0);

    int sockfd, new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    int sin_size;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    cout << "connect: error
    ";
    else
    cout << "connect: ok
    ";

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(MYPORT);
    my_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(my_addr.sin_zero), '', 8);

    cout << "bind: ";
    if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
    cout << "error
    ";
    else
    cout << "ok
    ";

    cout << "listen: ";
    if(listen(sockfd, BACKLOG)==-1)
    cout << "error
    ";
    else
    cout << "ok
    ";

    cout << "
    Waiting for user to connect

    ";

    sin_size = sizeof(struct sockaddr_in);
    new_fd =accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);

    char buff[50] = "";
    int rcv = recv(new_fd, buff, 50, 0);
    if(rcv == -1)
    cout << "recv: error
    ";
    else if(rcv == 0)
    cout << "recv: connection dropped by user
    ";
    else {
    cout << "recv: ok

    ";

    char *a1;
    a1 = inet_ntoa(their_addr.sin_addr);
    cout << "buff= "" << buff << ""
    ";
    cout << "received from: " << a1 << '
    ';
    }

    cout << "
    Press (enter) to continue
    ";
    cin.ignore(80, '
    ');

    closesocket(sockfd);
    WSACleanup();

    return 0;
    }
    //----------------------------------

    Thanks again Eric, sorry for the sheer blindness.
    Alex

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories