Sorry for maybe silly question, but I am not familiar with winsock 2.0 and especially in C++, so could anyone help me with this:
I've wrote c++ console application and it uses winsock 2.0. Console application connects to server and gets response with 14 bytes(as soons as it connects), then I am trying to send login data and then I should get a response from server but I always get 0 length which is not suppost to be like that. I am not using any threads. Not sure how socket works in C++.
So there might be few possible issues: I did not received an answer and I get disconected prior this or server did not understood my login (which is less likely but pissible).
Unfortunatelly I have no code for newcamd server, just the code for client in linux which I translated.
In very short code would be something like this:
Connect()
Receive_bytes (getting 14 bytes)
Send_Login && Received_bytes!=0 then is bad login (always fails here on receive)
if answer_byte = "\E1" then login suceeded
else answer_byte= "\E2" then login failed
In .NET method MySocket.BeginReceive() will return 0 length only when server disconnects or will wait on this method for any other answer. Is this same for c++?
My receive code (code taken from some web as an example as I am quite new in C++):
int cNetSocket::Read (unsigned char *data, int len, int to)
{
string bytes = mso->ReceiveBytes();
int mlen = bytes.length();
if (mlen>0 && mlen<=len) memcpy(data, bytes.c_str(), mlen);
if (mlen>0 && mlen>len) memcpy(data, bytes.c_str(), len);
return mlen;
}
After new testing looks like server accepts login, so I believe that there is a problem in receiving as bytes from socket. The first byte should be zero and this what might be an issue for string. My current Socket code looks like this:
std::string Socket::ReceiveBytes() {
std::string ret;
char buf[1024];
while (1) {
u_long arg = 0;
if (ioctlsocket(s_, FIONREAD, &arg) != 0)
break;
if (arg == 0)
break;
if (arg > 1024) arg = 1024;
int rv = recv (s_, buf, arg, 0);
if (rv <= 0) break;
std::string t;
t.assign (buf, rv);
ret += t;
}
return ret;
}
Could anyone help me to turn this to unsgined char array as an output?
Original code is taken from
this Socket example site.