Solved

How to use WinSock ?

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 144
Compile GLUT with Visual Studio 2015 1 230
Error creating a new C++ project in ,net 20 57
Can Live bindings change TGrid Cell Colour ? 1 61
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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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