コード例 #1
0
ファイル: recho.c プロジェクト: gene9/uplink-source-code
int main (int argc, char *argv[])
{
int Rc;
LPUDPSOCK lpSock=NULL;
char szVer [128];
int  nTries=0;
unsigned short nPort;

  if (argc<2)  Usage();
  if (argc==3  &&  strcmp (argv[1], "-v")==0)  Tcp4uEnableLog (LOG4U_DUMP);

  Tcp4uVer (szVer, sizeof szVer);   printf ("Using %s\n", szVer);
  Tcp4uInit();

  nPort = Udp4uServiceToPort ("echo");
  printf ("echo port is %u\n", nPort);

  Rc = UdpInit (& lpSock, argv[argc-1], nPort, 0);
  if (Rc>=TCP4U_SUCCESS) 
     Rc = UdpBind (lpSock, TRUE, UDP4U_CLIENT);
  
  if (Rc<TCP4U_SUCCESS) 
  {
    printf ("Error %d/%d: <%s>\n", Rc, errno, Tcp4uErrorString (Rc));
    return 0;
  }

  /* send 3 datagrams */
     nTries++;
     do
     {
        Rc = UdpSend (lpSock, STRING, sizeof STRING, 0, HLOG);
        if (Rc>=TCP4U_SUCCESS) 
           Rc= UdpRecv (lpSock, buf, sizeof buf, 5, HLOG);
        nTries++;
     }
     while (nTries <= 3 && Rc==TCP4U_TIMEOUT);

  if (Rc>=TCP4U_SUCCESS) 
       printf ("Server sends %d bytes. String <%s>\n", Rc, buf);
  else printf ("Error %d/%d: <%s>\n", Rc, errno, Tcp4uErrorString (Rc));

  if (lpSock!=NULL)   UdpCleanup (lpSock);
  Tcp4uCleanup();
return 0;
}
コード例 #2
0
/**
 * Function called after the CActivePanConn completes and the IAP has started.
 */
void CPanConnections::IapStarted()
	{		
	TInt rerr;
	if (!iIapLoading)
		{

		}
	
	iIapLoading = EFalse;
	iIapStarted = ETrue;
	// Open TCP socket with interface to existing RConnection

	if (iUseTcpTransport)
		{
		rerr = iSocket.Open(iSockSvr, KAfInet, KSockStream, KProtocolInetTcp, iConnection);
		}
	else
		{
		// User wants a UDP socket
		rerr = iSocket.Open(iSockSvr, KAfInet, KSockDatagram, KProtocolInetUdp, iConnection);
		}
		
	if (rerr != KErrNone)
		{
		return;
		}
	// Start TCP connection sequence			
	if (iListening && iUseTcpTransport)
		{
		TcpIpBindAndListen();
		AcceptNewSocket();
		}
	// If we are a UDP socket then bind, a wait is required	
	if (!iUseTcpTransport)
		{
		TInt duration;
		if (iLocalRole == KPanGnUUID)
			{
			duration = 4000000;
			}
		else 
			{
			duration = 6000000;
			}
			
		RTimer timer;
		TRequestStatus status;
		timer.CreateLocal();
		timer.After(status, duration);
		User::WaitForRequest(status);
		timer.Close();
		rerr = UdpBind();
		if (iLocalRole == KPanGnUUID)
			{
			ReceiveRemoteName();
			}
		else
			{
		
			TPckgBuf<TInt> buff = 1;
			TRequestStatus status;
			TInetAddr addr(KDealerIpAddr,KGenericPort);
			iSocket.SendTo(buff, addr, 0, status);
			User::WaitForRequest(status); 
			iConsole.Printf(_L("\nJoined the PAN, press ENTER to continue..."));
			}
			
		
		}
		

	}