Solved

How to use WinSock ?

Posted on 1999-01-05
4
300 Views
Last Modified: 2010-04-02
I am a learner Winsock,got some questions in CAsyncSocket,
1) I derive my own class from CAsyncClass, I send data from ther server side, but the OnSend are never invoked, Why?
2) Could anybody show me the sequence when using CAsyncSocket? Say, in the serve side, If I want to send sth after receive sth, how to call?
Thanks so much.
0
Comment
Question by:zouying
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 4

Accepted Solution

by:
nil_dib earned 50 total points
ID: 1181520
First of all, if you are a beginner with sockets you should use
the CSocket class which is much more easyer to use (its derived from CAsyncSocket).
Anyway if u want to use CAsyncSocket the sequence of operations
on the server side should be:
1. create a socket object:
   CAsyncSocket myServerSock;
   myServerSock.Create();
2. on the server side you should call CAsyncSocket::Bind to associate a local address with the socket
   myServerSock.Bind(1048,"mypc.domain.xx");
   recognize the port must be greater then 1024!
3. call CAsyncSocket::Listen
4. now your server is waiting for new connections. If a client connets to your server, your server has to call
   CAsyncSocket::Accept
   now construct a new socket object witout calling create!
   CAsyncSocket mySockNewClient;
   myServerSock.Accept( mySockNewClient);
5. the mySockNewClient is the socket object to your client.
   you can now override the member functions of CAsyncSocket:

CMyServer::OnReceive()
{
   char  szBuf[2048];
   CAsyncSocket::OnReceive(nErrorCode);
   Receive( szBuf, sizeof(szBuf) );
   ....
   // if( CString(szBuf) == "sth" )
   // Send( "sth", 3 );
   ....
}

But one more time CAsyncSocket is more difficult to use than CSocket.
You have to handle same things by our own such as byteordering, blocking ....
You should also read the articles Windows Sockets: Using Class CAsyncSocket, Windows Sockets: Background,Windows Sockets: Blocking, Windows Sockets: Byte Ordering, Windows Sockets: Sequence of Operations ....


 
0
 

Author Comment

by:zouying
ID: 1181521
Nil_dib, thank you very much. But I still don't fully understand. In fact, I tried to send some data from Client to Server, but can't. So I asked why OnSend is never invoked, and what is the seqence when calling send or receive? So, could you pls show me how to send data from client to Server? Again, thanks.
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181522
mhhh,
I think you are on the wrong way with the "OnSend" method ...
the appropiate method is OnReceive which is called when data receives.
Ok, step by step (client):
1. create a socket object
   CSocket mySock;
   mySock.Create();
2. connect to server:
   mySock.Connect("myserver.domain.com", 1048);
   perhaps you should call mySock.AsyncSelect(FD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE|FD_OOB|FD_ACCEPT)
3. now you can send data from client to server:
   mySock.Send("hello world", 11);
   (on the server side "OnReceive" is called to signal that data is received)

hpoe this makes it more clear.
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1181523
You can also look at http://www.winsite.com/info/pc/win95/programr/socktst3.zip/downl.html
this is a complete socket client (with MFC source!).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question