Solved

CSocket question

Posted on 2004-09-28
4
256 Views
Last Modified: 2013-11-20
Hello,
I need to develop an application that uses CSocket and reads data through a certain port. The application will only read data through that port and will not send any. It should accept a connection from anywhere and save everything received  in a file. Saving in a file is not a problem, but I am facing a problem in putting the sockect to work. Could someone please provide me with the code of a socket that reads data through a certain port, please don't post any links, I have alot of links but I need something fast to work for today and I'll modify it later.

      Thanks,

          Mazen
0
Comment
Question by:Mazen
  • 2
4 Comments
 
LVL 1

Author Comment

by:Mazen
ID: 12168994
One more thing, in VC++ 6 , we added the timer function by opening the class wizard and choosing WM_TIMER and then add function, but do we do so in VC++ .net?
0
 
LVL 6

Accepted Solution

by:
Amritpal Singh earned 150 total points
ID: 12169048
sorry to post a link ,but plz c it for a moment
a full-fledged code example

http://www.funducode.com/freevc/Socket%20Prog.htm

BOL
0
 
LVL 2

Assisted Solution

by:carribus
carribus earned 100 total points
ID: 12169256
The first thing you should do is derive your own class from CAsyncSocket, lets call it CYourAsyncSocket.

In this class, override the following functions:

OnAccept(), OnClose(), OnConnect(), OnReceive()

In your app, you set this class up as the listener socket (to receive connections):

CYourAsyncSocket listener;
int nListenPort = 12345; // the port you want to listen on for incoming connections

listener.Create(nListenPort);
listener.Listen();

Now, in your overridden CYourAsyncSocket::OnAccept() function, you call the Accept() function, passing in a new CAsyncSocket object. The function could look something like this:

void CYourAsyncSocket::OnAccept(int nErrorCode)
{
  CYourAsyncSocket* pNewSocket = new CYourAsyncSocket;

  Accept( *pNetSocket );
  // you now have the connection... maybe you should say hello :)
  pNewSocket->Send("Hello", 5);

  // you should actually store the new socket in some kind of an array...
  m_arraySockets.Add(pNewSocket);
}

As you can see in the above example, you can send with the Send() function if you need to. However, to receive data, you now need to hook the OnReceive() function as mentioned above to save the data coming down to a file... the function could look similar to:

void CYourAsyncSocket::OnReceive(int nErrorCode)
{
  TCHAR buffer[4096];
  int nBytesRead = 0;

  nBytesRead = Receive(buffer, sizeof(buffer));

  switch (nBytesRead)
  {
    case 0: // socket was closed
      Close();
      break;

    case SOCKET_ERROR:
      AfxMessageBox("Error on receive!");
      Close();
      break;

    default:
      {
        // This is where you need to save the data to a file. The data is in the buffer array.
      }
      break;
  }
}

That should sort you out :)
Cheers
0
 
LVL 1

Author Comment

by:Mazen
ID: 12187309
HI carribus ,

   I know that you posted the code for me and amrit_82 posted a link, but I've used the class that is already written in the link he provided because as he said it is really a full-fledged code example and it took me five minutes to develop the application I need. Thank you both for solving my problem quickly.

         Mazen
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

830 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