Thread: Socket Problem

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Socket Problem

    I have attached 2 files of my socket program. Whenever i run it, i got the messagebox "Socket failed". I checked everything, and they are fine as far as i am concerned. Please point out my mistakes. Thank you.
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could try reading the manual
    socket Function (Windows)

    Specifically
    Return Value
    If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

    So call WSAGetLastError and find out WHY it didn't work, as opposed to just saying "It didn't work" and bombing out.

    Do this for ALL your socket related functions - make it a class member function you can easily call.


    Code:
        char* socketRecv() //pass buffer to str.  
        { 
            char *buffer; 
            int result2; 
            int len = 1024; //maximum bytes received 
            if ((result2 = recv(clientsockfd, buffer, len, 0)) < 0)  //flags are left to 0 
            {                                                        //returns 0 means client closed connection 
                System::Windows::Forms::MessageBox::Show("Recv failed"); 
                return buffer; 
            } 
        }
    1. You don't allocate any space for 1024 characters (buffer points to what?)
    2. You don't make use of the return length, to indicate how much VALID data there is. It could be anything from 1 byte to len bytes
    3. What do you return if this fails? At the moment, it's just a junk pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thank you.
    I have run the code and i get this error.
    WSAEACCES
    10013
    "Permission Denied"
    I am running on windows 7 with admin privileged. I also tried to run the ".exe" with "run as Administrator" but still getting the same error.
    I tried this on console, therefore i got the error with WSAGetLastError().

    However, how can i generate this error in a messagebox. I tried replacing this
    System::Windows::Forms::MessageBox::Show("Socket failed");
    with
    System::Windows::Forms::MessageBox::Show(WSAGetLas tError());
    But i later found that WSAGetLastError() returns an int. I tried using itoa() but could not convert it to ascii. Please guide me how to cast from "int" to "system string". Thank you.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by valthyx View Post
    But i later found that WSAGetLastError() returns an int. I tried using itoa() but could not convert it to ascii. Please guide me how to cast from "int" to "system string". Thank you.
    Code:
    WSAGetLastError().ToString()

  5. #5
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thanks alot Mike, I didn't know it is so simple. Does this work for converting "char*" to "system string"? And is there any similar way of converting "system string" to "char*" string?

    By the way, i still could not figure out how to go about the "permission denied" error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket problem
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-17-2010, 01:32 AM
  2. Problem with Socket.h in C
    By Yuushi in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-10-2007, 05:33 AM
  3. Problem with socket
    By salcom in forum Linux Programming
    Replies: 2
    Last Post: 05-30-2003, 02:12 AM
  4. Socket problem
    By k_w_s_t_a_s in forum Linux Programming
    Replies: 2
    Last Post: 05-23-2003, 08:36 AM
  5. C socket problem
    By TeMpEsT-9 in forum C Programming
    Replies: 6
    Last Post: 07-27-2002, 12:53 PM