Results 1 to 2 of 2

Thread: Winsock, Pain in the...

  1. #1

    Thread Starter
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169

    Winsock, Pain in the...

    ok, i'm trying to make a chat server to get into winsocks programming in C++ coming over from VB6. but i think i 'm missing some key knowlegde when it comes to strings.

    I can get

    • get the data from the socket one charater at a time
    • i can catch a new line
    • i can catch a backspace


    but what i can't do put it into a string without FOUR 2222 at the end of it all.

    image below.

    WHYYYYYYYY????

    VB Code:
    1. send (Client, "Server V. 0.1\n",13, 0);
    2.     char buffer;
    3.     char flip[255];
    4.     char *flop;
    5.     int pos = 0;
    6.  
    7.    
    8.     while(Alive == 1) {
    9.            
    10.             rCode = recv(Client,&buffer,1,0);
    11.             if(rCode == SOCKET_ERROR) {
    12.                 rCode = WSAGetLastError();
    13.                 cout << "\nERROR:recv()" << rCode;
    14.                 WSACleanup();
    15.                 return 1;
    16.             }else if(rCode == 0) {
    17.                 cout << "Disconnected";
    18.                 Alive = 0;
    19.             }else{
    20.                 if (buffer == char(13)) {
    21.                     flop = new char[pos];
    22.                     for (int i = 0; i < pos; i++) {
    23.                         flop[i] = flip[i];
    24.                     }
    25.                     pos = 0;
    26.                     cout << flop;
    27.                 } else {
    28.                     flip[pos] = buffer;
    29.                     pos++;
    30.                 }
    31.             }
    32.     }

    Digitalmyth

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Winsock, Pain in the...

    You receive the string fine. The problem is when you display it. The display routines assume that the string is NUL-terminated, but the one you received isn't, because you haven't made it so.
    Code:
    flop = new char[pos];
    for (int i = 0; i < pos; i++) {
    	flop[i] = flip[i];
    }
    flop[pos] = '\0';
    pos = 0;
    cout << flop;
    Mind you, you have a huge memory leak and a buffer overflow in that code, not to mention quite some unnecessary copy overhead. (The flop buffer is completely unnecessary.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width