Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
turbot_yu
Asked:
turbot_yu
  • 3
  • 2
  • 2
4 Solutions
 
Jaime OlivaresCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 OlivaresCommented:
>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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now