Solved

How to use WinSock ?

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 199
Getting IP address 8 79
Dialogbox API leak? 18 96
Path to  STL Map header file 1 58
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

809 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