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.
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.
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.
1. You don't allocate any space for 1024 characters (buffer points to what?)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; } }
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.
Thank you.
I have run the code and i get this error.
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.WSAEACCES
10013
"Permission Denied"
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
withSystem::Windows::Forms::MessageBox::Show("Socket failed");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.System::Windows::Forms::MessageBox::Show(WSAGetLas tError());
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.