Solved

How to use WinSock ?

Posted on 1999-01-05
4
281 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
  • 3
4 Comments
 
LVL 4

Accepted Solution

by:
nil_dib earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now