Sockets Question

This is a discussion on Sockets Question within the Windows Programming forums, part of the Platform Specific Boards category; Here's some code: #include <iostream.h> #include <conio.c> #include <winsock.h> #define PORT 23 int main() { char msg[1000]; SOCKET sck; int ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Sockets Question

    Here's some code:


    #include <iostream.h>
    #include <conio.c>
    #include <winsock.h>

    #define PORT 23

    int main()
    {
    char msg[1000];
    SOCKET sck;
    int sin_size;
    struct sockaddr_in my_addr;
    WORD version=MAKEWORD(1, 1);
    WSADATA data;
    int key;
    int connection;
    int incoming;
    char inMsg[1000];

    WSAStartup(version, &data);

    sck = socket(AF_INET, SOCK_STREAM, 0);
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = inet_addr("207.65.112.12");

    connection = connect(sck, (struct sockaddr *) &my_addr, sizeof(my_addr));

    cout << "Type a message: ";
    cin.getline(msg, 1000);

    cout << "\n\n";

    send(sck, msg, strlen(msg), 0);

    getch();

    incoming = listen(sck, 2);

    cout << inMsg;

    cout << "\n\n";

    WSACleanup();

    getch();
    return 0;
    }


    When I press a key at getch(); it just receives EL32.DLL. What is wrong?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    >>
    incoming = listen(sck, 2);

    cout << inMsg;
    <<

    Umm...where do you get inMsg from? it has nothing in it!

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    And also, are you sure that there's a server at the other end of the connection sending you the right data? or any data at all? Sure your client is designed to recieve from it correctly?

    just a thought. I haven't done C Winsock in a long while.

  4. #4
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Well, I agree with -KEN- in saying that inMsg is not touched by any socket function, and has no reason to have any valid data in it.

    Also, you might want to check the flow of the functions--did you mean to use recv() instead of listen() in that particular instance? Setting up a listening socket requires an extra struct and a call to bind(), neither of which appear in your code.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  5. #5
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Question

    Would I also do:

    incoming = bind( ... ) or just do bind by itself? And yes I have recv now.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  6. #6
    Registered User DutchStud's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Arrow kind of a side question...

    To get info from a server, that file has to be running on a website, not just the file be located there right? Can you do that through a free homepage provider?

  7. #7
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question What do I need for(...)?

    What do I need for something like a chat. Asynchronous sockets? If so, can I have a link?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about forking with sockets listening
    By Phoenix_Rebirth in forum C Programming
    Replies: 2
    Last Post: 11-10-2008, 04:05 AM
  2. Sockets: IPv4 vs IPv6
    By ninboy in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-15-2008, 02:02 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Hi all!, Another question about socket's & NetBios
    By Pandora in forum Windows Programming
    Replies: 9
    Last Post: 03-19-2003, 08:10 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21