How to use socket to trigger another program

Hi Experts,

I am using vc++6.

There are 2 programs on 2 pcs in network.
One(client) will trigger another one(server) by socket connection.

There will be multi trigger at the same time, so both side need to run in multi-thread.

I am new in socket and multi-thread.
May you give an simple example?

Thanks and Regards,

Yu Ming
turbot_yuAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
You can use MFC's CAsyncSocket, if you just want to trigger a process, you can use UDP frames.
Derive a socket from CAsyncSocket, with OnReceive event:
class CYourSocket : public CAsyncSocket
{
       virtual void OnReceive(int nErrorCode);
};

then, in implementation file,
void CYourSocket::OnReceive(int nErrorCode)
{
        char buffer[2000];   // enough space for an UDP frame

         if ( !Receive(buffer,sizeof(buffer))
      return;  // no data
         else
                DoSomething(buffer);   // Your processing function
}

Declare a data member on your app object, let's say m_socket;
to use socket, you will need to create it, typically in InitInstance() function of your app:

BOOL CYourApp::InitInstance()
{
      if (!AfxSocketInit()) {
            AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
            return FALSE;
      }
      
// All your initialization code here

      BOOL _true = TRUE;
      m_socket.Create(1500, SOCK_DGRAM, FD_READ);   // port 1500 for this example
      m_socket.SetSockOpt(SO_BROADCAST, &_true, sizeof(_true));
      
      return TRUE;
}

Now you are ready to receive data from udp port 1500....
0
 
turbot_yuAuthor Commented:
May I use sample of TCP/IP
0
 
turbot_yuAuthor Commented:
And I also want to transfer some simple data, thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
grg99Commented:
UDP is a poor choice for this.   The packet may arrive zero, one, or multiple times.   Not a good choice for spinning off processes.  Better use a TCP connection.   Full examples for this are found on most "man socket" pages.



0
 
Jaime OlivaresSoftware ArchitectCommented:
>The packet may arrive zero, one, or multiple times.
Multiple times just occurs outside a LAN, when an UDP frame is routed in multiple paths, if you work just inside a LAN, this will not occur.
Zero bytes? Have not seen never inside a LAN, unique situation I have seen is when a data doesn't arrive, but this is very unprobable in a mid-traffic LAN.
0
 
grg99Commented:
>Multiple times just occurs outside a LAN, when an UDP frame is routed in multiple paths, if you work just inside a LAN, this will not occur.

Theoretically yes, but in actuality quite often these days.  Having put network snoopers onto various networks, I'm always amazed at the kind of junk flowing through networks that the users think are mostly okay.  

I'd estimate 22% of LANS have some sort of firewall or router misconfiguration  that results in duplicate packets.  This often goes unnoticed for years.


>Zero bytes? Have not seen never inside a LAN, unique situation I have seen is when a data doesn't arrive, but this is very unprobable in a mid-traffic LAN.

Not ever?   You never have packet collisions?  Must be a very very idle network.  On our little LAN with only a handful of nodes, we get about 3% packet loss.  Some due to collisions, some due to older network cards that are slow and have small buffers.   Also some of the longer wire runs have a higher error rate.

UDP is unreliable.  It gets less unreliable on carefully configured and scrupulously maintained networks.  But it's never reliable and can't be made so.





0
 
turbot_yuAuthor Commented:
Hi

May you give a sample of tcp?

Thanks,

Turbot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.