Solved

CSocket question

Posted on 2004-09-28
4
214 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand to include initial dialog with two choices. 9 59
format the code in java 6 72
has77  challenge 9 66
Fibonacci challenge 11 81
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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