Solved

CSocket question

Posted on 2004-09-28
4
229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 calculate times for developing software? 8 65
Error on moodle after upgrade 3 123
unable to delete all specified values regedit 38 277
NotAlone Challenge 20 79
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

910 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

20 Experts available now in Live!

Get 1:1 Help Now