🔒 Locked
[MFC] Using CAsyncSocket
Advertisement
Original Post
This class makes me sick. I've just created a member in my MFC Dialog class: CAsyncSocket m_Socket; After that, I'm creating the socket: if( !m_Socket.Create() ) MessageBox(L"Could not create Socket", L"Error"); Next, I'm connecting to my server: m_Socket.Connect(L"127.0.0.1", 80); And this function ALWAYS returns 0. Why? My firewall informs me that this program wants to connect to the specified adress, and i ALLOW it. I can also disable the firewall, it just doesn't matter. I thouhgt it may be an error with the return code, so I'm just sending a message to my apache server: const char *msg = "GET /index.html HTTP/1.0/ Host: localhost"; int iSnd = m_Socket.Send(msg, (int)strlen(msg)); But this one never gets noticed in the log. What is going wrong here? I don't get it...
Thanks for your answer :)
In my case, the error is always 10035. From winerror.h I find the defined name:
WSAEWOULDBLOCK
alogn with the explaination:
'A non-blocking socket operation could not be completed immediately'
What does this mean? I don't understand that error. How did I mark my Socket as blocking, and what does block mean here anyway?
In my case, the error is always 10035. From winerror.h I find the defined name:
WSAEWOULDBLOCK
alogn with the explaination:
'A non-blocking socket operation could not be completed immediately'
What does this mean? I don't understand that error. How did I mark my Socket as blocking, and what does block mean here anyway?
By using CASyncSocket you're requesting a socket that doesn't block (i.e. it doesn't hang up until operations on it have completed, instead it returns immediately).
What's happening here is you're calling Connect on a non-blocking socket, it returns immediately and the actual connect happens some (imperceptible) time later. If you're using CAsyncSocket the actual completion information comes through a call to the CAsyncSocket::OnConnect function, which you probably have to override to handle (I've no idea, I've never used it).
As a side-note MFC's CSocket is also a non-blocking socket, but it runs things inside a polling loop to make things look synchronous...
Jans.
What's happening here is you're calling Connect on a non-blocking socket, it returns immediately and the actual connect happens some (imperceptible) time later. If you're using CAsyncSocket the actual completion information comes through a call to the CAsyncSocket::OnConnect function, which you probably have to override to handle (I've no idea, I've never used it).
As a side-note MFC's CSocket is also a non-blocking socket, but it runs things inside a polling loop to make things look synchronous...
Jans.
I'm still using the Aynchronus class. The connection can now be established, and I'm also able to send a message to my server, but somehow, this message isn't noticed in the log of my server. I've also made a breakpoint at OnRecieve of my CAsynSocket class to see when the Server responds, but that doesn't seem to happen.
CAsyncSocket::Send(...) returns exactly the length of the string I want it to send ( "GET /ownscripts/ HTTP/1.1 Host: localhost" ) and this should work. A year ago I was able to send some messages to my apache server with this technique and I also recieved messages, but now, theres something wrong.
Does anyone know what this could be?
*edit*
There is something seriously wrong. Was there big change with CAysncSocket from vc6.0 to msvc2005? A year ago, I wrote a little "server" that accepts incomming sockets and then prints every message it recieves. I tried that again, but the only thing that my Server does is accepting connections, it doesn't recieve ANYTHING.
Am i using this class in the wrong way?
[Edited by - SiS-Shadowman on March 14, 2007 8:44:06 AM]
CAsyncSocket::Send(...) returns exactly the length of the string I want it to send ( "GET /ownscripts/ HTTP/1.1 Host: localhost" ) and this should work. A year ago I was able to send some messages to my apache server with this technique and I also recieved messages, but now, theres something wrong.
Does anyone know what this could be?
*edit*
There is something seriously wrong. Was there big change with CAysncSocket from vc6.0 to msvc2005? A year ago, I wrote a little "server" that accepts incomming sockets and then prints every message it recieves. I tried that again, but the only thing that my Server does is accepting connections, it doesn't recieve ANYTHING.
// StartupCMySocket m_Socket;CMyConSocket m_ConnectionSocket;if(!m_Socket.Create(1234)) MessageBox(L"Can't create Socket", L"Error");if(!m_Socket.Listen()) MessageBox(L"Can't start Server on Port 1234", L"Error");m_Socket.SetParent(this);m_ConnectionSocket.SetParent(this);// CMySocket overrides the OnAccept function of CAsyncSocket// When CMySocketOnAccept(int nErrorCode) gets called, this function// calls CServerDlg::OnAccept()// It actually gets called and the connection will be establishedvoid CServerDlg::OnAccept(){ m_Socket.Accept(m_ConnectionSocket);}// CMyConSocket overrides the OnRecieve function of CAsyncSocket// etc...// This function NEVER gets called, although a connection gets established// ( through opera, firefox, etc.. just typing in localhost:1234 )void CServerDlg::OnRecieve(){ char *pBuf = new char[1025]; int iBufSize = 1024; int iRcvd; CString strRecvd; if( (iRcvd = m_Socket.Receive(pBuf, iBufSize)) != SOCKET_ERROR) { // Ende der Nachricht abschneiden pBuf[iRcvd] = NULL; // Nachricht in einen CString kopieren strRecvd = pBuf; }}Am i using this class in the wrong way?
[Edited by - SiS-Shadowman on March 14, 2007 8:44:06 AM]
IIRC, when requesting something over HTTP you have to end your header with two endlines. In other words, you need to end your string with "\r\n\r\n" before the server will respond. Otherwise, it'll wait for you to send more HTTP headers.
In regard to your server socket code, you're trying to read data from your server socket rather than your connection socket. In your CServerDlg::OnRecieve, I believe you should be using m_ConnectionSocket instead of m_Socket.
In regard to your server socket code, you're trying to read data from your server socket rather than your connection socket. In your CServerDlg::OnRecieve, I believe you should be using m_ConnectionSocket instead of m_Socket.
I've changed my header. I'm now sending "GET / HTTP/1.1/ Host: localhost \r\n\r\n"
But apache doesn't respond to that either, nor is this request mentioned in the logs...
And yes, you're right, I used the wrong socket, however it doesn't change anything when I'm trying to connect to the server, OnRecieve() gets never called.
But apache doesn't respond to that either, nor is this request mentioned in the logs...
And yes, you're right, I used the wrong socket, however it doesn't change anything when I'm trying to connect to the server, OnRecieve() gets never called.
Again, my HTTP knowledge might be fuzzy, but I think what you need to be sending is this: "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n". I just tried it in a telnet window with google, and it worked.
If it still doesn't work, try running it on a different computer perhaps, or turning your firewall off completely.
If it still doesn't work, try running it on a different computer perhaps, or turning your firewall off completely.
Okay, the messages now appear in the log, but I don't get an answer from the server *lol*
127.0.0.1 - - [14/Mar/2007:19:00:41 +0100] "GET /ownscripts/ HTTP/1.0" 200 2782
( The 1.0 was intention, just to be shure it's my programm accessing the server ^^ )
If I'm right, the server should have answered, since the size of the response message is standing right after the return code 200.
*edit*
Well, I just misspelled receive *g. Now everything works perfect. Thanks alot :D
[Edited by - SiS-Shadowman on March 14, 2007 1:00:35 PM]
127.0.0.1 - - [14/Mar/2007:19:00:41 +0100] "GET /ownscripts/ HTTP/1.0" 200 2782
( The 1.0 was intention, just to be shure it's my programm accessing the server ^^ )
If I'm right, the server should have answered, since the size of the response message is standing right after the return code 200.
*edit*
Well, I just misspelled receive *g. Now everything works perfect. Thanks alot :D
[Edited by - SiS-Shadowman on March 14, 2007 1:00:35 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement