|
-
August 14th, 1999, 04:44 AM
#1
CAsyncSocket????????
Hello,
This (probably simple) AsyncSocket problem that ive been having is driving me outta my mind.....
Can anybody tell me what im doing wrong here.
All i want my program to do i to make a simple socket connection to another computer via ip# and eventually transfer say a file.
But before i can do all that, im trying to just get the socket connection going.
So far, i have both server and client functions..
Server:
CListenerSocket sockSrvr ;
sockSrvr.Create(700);
sockSrvr.Listen();
Client:
CAsyncSocket sockClient;
if(sockClient.Connect(varthatismyip, 700))
AfxMessageBox("Success");
else
AfxMessageBox("Failure");
Also, the CListenerSocket is a class i derived from CAsyncSocket and overided the OnAccept function (i read that i had to do that)
So far, all its been giving me are "failures". For now i just want it to successfully connect. What am i doing incorrectly or missing?
Thanks for your time
P.S Also, if you have any bits of knowledge about send data, such as a file, over after the successful connection id greatly appeciate it
-
August 14th, 1999, 07:24 AM
#2
Re: CAsyncSocket????????
Try add Create() in client before connect
Dmitriy, MCSE
-
August 14th, 1999, 01:25 PM
#3
Re: CAsyncSocket????????
OK, i added the create() function in the client before connect() and still it gives me a failure. Anything else?
Thanks for your time
-
August 14th, 1999, 01:59 PM
#4
Re: CAsyncSocket????????
Any chance of sending some more code?
-
August 14th, 1999, 04:38 PM
#5
Re: CAsyncSocket????????
Yes, I agree, it is better to have some code.
In any way, where do you have error? Did server receive some data (did you have OnAccept event)?
Dmitriy, MCSE
-
August 14th, 1999, 05:39 PM
#6
Re: CAsyncSocket????????
Hello,
I think the error occurs in the client portion of the program. When I execute the following code:
CAsyncSocket sockClient;
sockClient.Create()
if(sockClient.Connect(varthatismyip, 700))
AfxMessageBox("Success");
else
AfxMessageBox("Failure");
the connect function returns 0. I dont know why . I pass to it a legitimate IP and port#, so what else can be wrong or missing?
-
August 14th, 1999, 06:02 PM
#7
Re: CAsyncSocket????????
Yes, I see. Did you receive event OnAccept() on the server side? Did you create new socket on the server side in case you've received OnAccept()?
Dmitriy, MCSE
-
August 14th, 1999, 06:06 PM
#8
Re: CAsyncSocket????????
No, OnAccept doesnt even get called at all. In my OnAccept i did not create a new socket yet. i just created created a messagebox to notify my if it got called. the messagebox doesnt popup.
-
August 14th, 1999, 06:33 PM
#9
Re: CAsyncSocket????????
It seems to me, the message does not reach your server. Try ping your ip-address on the server.
Another question - why did you select port 700?
Try 21, for example.
Dmitriy, MCSE
-
August 14th, 1999, 10:08 PM
#10
Re: CAsyncSocket????????
Hello, i really appreciate you helping me out.
I pinged my ip and it pinged back, and i selected port 21 and it still didnt work.
I want to add that i already tried doing this with CSocket instead of CAsyncSocket:
Client Code:
CSocket sockClient;
sockClient.Create( );
sockClient.Connect(myip, 700);
CSocketFile file(&sockClient);
CArchive arIn(&file,
CArchive::load);
CArchive arOut(&file,
CArchive::store);
CString strTemp="Send to the server";
arOut << i;
Server Code:
CSocket sockSrvr;
sockSrvr.Create(700);
sockSrvr.Listen();
CSocket sockRecv;
sockSrvr.Accept( sockRecv );
CSocketFile file(&sockRecv);
CArchive arOut(&file,
CArchive::store);
CArchive arIn(&file,
CArchive::load);
CString strTemp;
arIn>>strTemp;
AfxMessageBox(strTemp);
This code works and my server pops a message box with the string "Send to the server". But the problem with this is that the server stops everything until the client sends its message. (I read that this is called blocking i think) So, i want the server to still be free to do its business and i heard i could do that with CAsyncSocket. But i try to do that with the code in my previous messages but Asyncsocket does not work right and i dont know what im doing wrong?
-
August 15th, 1999, 12:12 AM
#11
Re: CAsyncSocket????????
Hi,
use GetLastError( ) and find the reason why the connect failed. Listen can only be used for TCP Stream connections, so please show us the Create function you used and possibly the AsyncSelect functions too. Your server also needs to Bind..."Before it can accept connection requests, a listening server socket must select a port number and make it known to Windows Sockets by calling Bind. Bind establishes the local association (host address/port number) of the socket by assigning a local name to an unnamed socket." I don't see that call in your code.
Vicken, Paul...
Regards,
Paul Belikian
-
August 15th, 1999, 02:47 AM
#12
Re: CAsyncSocket????????
Hello,
I did what you said and now i have the following:
Client:
CAsyncSocket sockClient;
if(!sockClient.Create(700,SOCK_STREAM))
AfxMessageBox("Create Failure");
sockClient.Connect(varofmycurrentip,700);
DWORD a=GetLastError();
Server:
CListenerSocket sockSrvr ;
sockSrvr.Create(700,SOCK_STREAM,FD_ACCEPT|FD_CONNECT);
sockSrvr.Bind(700,varofmycurrentip);
sockSrvr.Listen();
Now, it still doesnt work. The GetLastError() function returns the value of 10035. Any idea what this is? Any other ideas?
Thanks for your time
-
August 15th, 1999, 07:14 AM
#13
Re: CAsyncSocket????????
I'm not sure, but try connectionless UDP-
in server socket.Create(700,SOCK_DGRAM)
Dmitriy, MCSE
-
August 15th, 1999, 04:59 PM
#14
Re: CAsyncSocket????????
I don't bother with the windows socket stuff, opting instead for using a more direct approach. A good example can be found in http://www.codeguru.com/network/ChessServer.shtml.
I did notice a couple possible problems. You don't do a call to WSAStartup(), Bind(), or Accept() on the server side. (Note, that the WSAStartup might be done in the CASyncSocket. Don't know, haven't used them.) The following snippet should give you an idea. (This example uses blocking sockets.) Hope it helps.
//SERVER STARTUP FUNCTION
//DON'T FORGET TO TEST FOR ERRORS
#define BACKLOG 5 //5 other client connections can pool up before getting an error
LPCSOCKADDR psa; //pointer to socket address you created before you called this
WSADATA wsd;
HSOCKET hsockListen; //your server socket
HSOCKET hsockNewConnection; //someone connecting with client program
WSAStartup(0x0101,&wsd);
hsockListen = socket(AF_INET, SOCK_STREAM, 0);
bind(hsockListen, psa, sizeof(SOCKADDR));
listen(hsockListen, BACKLOG);
hsockNew = accept(hsockListen, psa, &nLengthAddr);
//server is now waiting for a connection after the call to accept
-
August 15th, 1999, 05:40 PM
#15
Re: CAsyncSocket????????
MSDN does not tell you this but...the way that you should do this to to inherit the CAsyncSocket.
On the Client side, call the Create and Connect of the CAsyncSocket from your class. Make use of the Notification Functions (eg OnConnect) to determine if a successful connection has been established. The OnReceive Notification Function can be used to handle data received by the socket.
The server side is more tricky. You will need 2 classes...both which inherits the CAsyncSocket. One is used to handle the connection (eg. Create, Listen, Accept). Once the connection is about to be established, (detected using the OnAccept Notification Function), dynamically allocate your second class and call Accept from this class to accept the connection. When connection is established, handle the socket receive/transimit in this second class. Don't forget to use the notification function to receive and send your data through the socket.
kktoh
Software Engineer(BE Hons)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|