Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CSocket question

Posted on 2004-09-28
4
Medium Priority
?
289 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 600 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

730 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