Solved

How to use WinSock ?

Posted on 1999-01-05
4
284 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Within a class, declare a function/method as static or not 16 132
Header of docx file 17 97
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 16
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 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

18 Experts available now in Live!

Get 1:1 Help Now