Solved

CSocket question

Posted on 2004-09-28
4
271 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
[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
  • 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

Industry Leaders: 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
How to create frequencies of a variable from SAS dataset? 10 146
List out all word 7 358
sum28 challenge 31 117
Use of condition with 'serial' in ansible 2 102
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

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