Thread: Help, debugger, Winsock :D

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    Help, debugger, Winsock :D

    hehe, i need help, this code compiles on Dev-C++ but when i am running it i get an error, when i recieve the pass attempt thing


    Code:
    #include <iostream>
    #include <fstream>
    #include <winsock2.h>
    #define PORT 23
    #define BACKLOG 5
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    ofstream iplogs("iplogs.txt", ios::app);
    int clientlen, i, bytes;
    char *idnumber = NULL, *loggered = "We have logged your IP as: ", *ask = "\nID: ", *pass = "chikka";
    WSADATA wsaData;
    WSAStartup(MAKEWORD(1, 1), &wsaData);
    SOCKADDR_IN ourinfo;
    ourinfo.sin_family = AF_INET;
    ourinfo.sin_port = htons(PORT);
    ourinfo.sin_addr.s_addr = htonl(INADDR_ANY);
    
    SOCKET sock;
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    bind(sock, (SOCKADDR *)&ourinfo, sizeof(ourinfo));
    listen(sock, BACKLOG);
    SOCKET newconn;
    SOCKADDR_IN client;
    clientlen = sizeof(client);
    newconn = accept(sock, (SOCKADDR *)&client, &clientlen);
    cout << "Got connection from: " << inet_ntoa(client.sin_addr) << endl;
    send(newconn, loggered, strlen(loggered), 0);
    
    send(newconn, inet_ntoa(client.sin_addr), 13, 0);
    
    iplogs << inet_ntoa(client.sin_addr) << endl;
    send(newconn, ask, strlen(ask), 0);
    
    recv(newconn, idnumber, strlen(idnumber), 0);
    if(stricmp(idnumber, pass) == 0)
    {
    send(newconn, "Welcome to the Super Computer\n", 31,0);
    }
    else
    {
    send(newconn, "hehe, idiot\n", 13, 0);
    }
    cout << endl << idnumber;
    closesocket(sock);
    closesocket(newconn);
    WSACleanup();
    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but when i am running it i get an error
    Posting details of the error always helps
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    i don't get an error, sorry, it just crashes and asks if i want to send an error report to Micosoft
    haha

    This is what the debugger thnig said

    'An Access Violation (Segmentation Fault) raised in your program'

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *idnumber = NULL,
    >>recv(newconn, idnumber, strlen(idnumber), 0);
    Here's one error. idnumber doesn't point anywhere, so you can't dereference it. Try defining it as an array
    >>char idnumber[SOME_SIZE];

    There might be other errors, I haven't read all of your code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    when i use
    char idnumber[500];

    my program recieves one char then closes connection, so that won't work


    Arggggghhhhhhhhhhh

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>recv(newconn, idnumber, strlen(idnumber), 0);
    As Salem shows, don't use strlen(), use sizeof().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    I did everything you said and it doesn't work

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    and i can't compare idnumber[500] with a '\n'

    so that means i would ave to get 1 char then put that char into idnumber then use like i++ thing
    then compare it


  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by the_jugl3r
    I did everything you said and it doesn't work
    What did recv() return then?

    There's plenty of examples and helpful links in the networking forum.

    >>and i can't compare idnumber[500] with a '\n'
    Why do you want to do that. Anyway:
    >>if (idnumber[0] == '\n')
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    I have to recieve one char at a time, and then when i get a newline compar what i got with the password so how could i use idnumber[0] Hmm

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    I can't get this working arrgggggggghhhhhhhhhhhhhhhhhhh

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about something like
    Code:
    char idnumber[500], *ptr = idnumber;
    int bytesReceived = 0;
    
    while (recv(newconn, ptr, 1, 0) == 1)
    {
      bytesReceived++;
      if (*ptr == '\n' || bytesReceived > sizeof(idnumber))
        break;
      ptr++;
    }
    
    *ptr = '\0';
    
    cout <<idnumber <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    thanks, its sort of working now, thanks A LOT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM
  5. Complete Port I/O and Heap Problems :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2002, 12:45 AM