004
20.05.2003, 10:48 Uhr
virtual
Sexiest Bit alive (Operator)
|
Dieses Programm zeigt die Empfangsrichtung für Windows und Linux, TCP und UDP. Ist zwar nicht genau das was Du willst, weil es recv an Stelle von recvfrom verwendet, aber es zeigt ungefähr wie es läuft (Die Namensauflösun des hostnames ist komplizierter als erforderlich):
C++: |
#include <stdlib.h> #include <stdio.h> #if _WIN32 # include <windows.h> #else # include <errno.h> # include <sys/socket.h> #endif
#ifdef _WIN32 #define errno WSAGetLastError #endif
#define USAGE fprintf(stderr, "usage: %s [-u|--udp] [-t|--tcp] port\n", szProgramName)
int main( int nArgC, char** pszArgV) { #ifdef _WIN32 WSADATA oWSA; #endif char szHostname[64]; const char* szProgramName; int nPort; int bUseTCP = 0; int bUseUDP = 0; int nUDPSocket; int nTCPSocket; struct hostent* poHostEntry; #ifdef _WIN32 struct sockaddr_in oSockAddr; #else struct sockaddr oSockAddr; #endif int nLen; int nSocket; char szBuffer[100]; static long nonblock = 1;
/* * Get invokation name */ szProgramName = *pszArgV + strlen(*pszArgV); while (szProgramName>*pszArgV && szProgramName[-1]!='\\' && szProgramName[-1]!='/' ) { szProgramName--; }
/* * Check command line */ pszArgV++; nArgC--; while (nArgC) { if ('-' != pszArgV[0][0]) { break; }else if (0==strcmp("--", pszArgV[0])) { pszArgV++; nArgC--; break; }else if (0==strcmp("-u", pszArgV[0]) || 0==strcmp("--udp", pszArgV[0])) { bUseUDP = 1; pszArgV++; nArgC--; }else if (0==strcmp("-t", pszArgV[0]) || 0==strcmp("--tcp", pszArgV[0])) { bUseTCP = 1; pszArgV++; nArgC--; }else { fprintf(stderr, "%s: unknown option %s\n", szProgramName, pszArgV[0]); USAGE; exit (EXIT_FAILURE); } }
if (1 != nArgC) { fprintf(stderr, "%s: illegal command line\n", szProgramName); USAGE; exit (EXIT_FAILURE); }
if (1 != sscanf(pszArgV[0], "%d", &nPort) && nPort<0 || nPort>65535) { fprintf(stderr, "%s: illegal port number (allowed values: 0...65535)\n", szProgramName); exit (EXIT_FAILURE); } if (!bUseTCP && !bUseUDP) { bUseUDP = 1; bUseTCP = 1; }
/* * Initialize Socket library and other variables (without deeper version checking) */ #ifdef _WIN32 if (0 != WSAStartup(MAKEWORD(2,0), &oWSA)) { fprintf(stderr, "%s: WSAStartup() failed: %d\n", szProgramName, errno); exit (EXIT_FAILURE); } #endif if (-1 == gethostname(szHostname, sizeof(szHostname))) { fprintf(stderr, "%s: gethostname() failed: %d\n", szProgramName, errno); exit (EXIT_FAILURE); } if (NULL == (poHostEntry = gethostbyname(szHostname))) { fprintf(stderr, "%s: gethostbyname() failed: %d\n", szProgramName, errno); exit (EXIT_FAILURE); }
memset(&oSockAddr, 0, sizeof(oSockAddr)); oSockAddr.sin_family = poHostEntry->h_addrtype; memcpy((char *)&oSockAddr.sin_addr, poHostEntry->h_addr, poHostEntry->h_length); oSockAddr.sin_port = htons((short)nPort);
/* * Setup UDP Socket, if requested */ if (bUseUDP) { /* * Create and bind socket to port */ nUDPSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (INVALID_SOCKET == nUDPSocket) { fprintf(stderr, "%s: socket() for UDP failed: %d", szProgramName, errno); exit(EXIT_FAILURE); } ioctlsocket(nUDPSocket, FIONBIO, &nonblock); if (SOCKET_ERROR == bind(nUDPSocket, (struct sockaddr*)&oSockAddr, sizeof(oSockAddr))) { fprintf(stderr, "%s: bind() for UDP failed: %d", szProgramName, errno); exit(EXIT_FAILURE); }
/* * Write some information */ printf("Opened UDP socket %d on %s:%d\n", nUDPSocket, szHostname, nPort); }
/* * Setup TCP Socket, if requested */ if (bUseTCP) { /* * Create and bind socket to port */ nTCPSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == nTCPSocket) { fprintf(stderr, "%s: socket() for TCP failed: %d", szProgramName, errno); exit(EXIT_FAILURE); } ioctlsocket(nTCPSocket, FIONBIO, &nonblock); if (SOCKET_ERROR == bind(nTCPSocket, (struct sockaddr*)&oSockAddr, sizeof(oSockAddr))) { fprintf(stderr, "%s: bind() for TCP failed: %d", szProgramName, errno); exit(EXIT_FAILURE); }
/* * Write some information */ printf("Opened TCP socket %d on %s:%d\n", nTCPSocket, szHostname, nPort); }
/* * Loop */ for(;;) { fd_set oReadSet; struct timeval oTimeout = {5, 0}; /* * Wait for timeout or one of the sockets */
FD_ZERO(&oReadSet); if (bUseUDP) FD_SET(nUDPSocket, &oReadSet); if (bUseTCP) FD_SET(nTCPSocket, &oReadSet); printf("Waiting for data on any of the sockets...\n"); switch (select(0, &oReadSet, NULL, NULL, &oTimeout)) { case 0: printf("No bytes received yet...\n"); break; case SOCKET_ERROR: fprintf(stderr, "%s: select() failed: %d\n", szProgramName, errno); exit (EXIT_FAILURE); break; default: nLen = 0; if (bUseUDP && FD_ISSET(&oReadSet, nUDPSocket)) { nSocket = nUDPSocket; }else if (bUseTCP && FD_ISSET(&oReadSet, nTCPSocket)) { nSocket = nUDPSocket; }else { fprintf(stderr, "%s: whoops - no socket has data: %d\n", szProgramName, errno); exit (EXIT_FAILURE); break; } do { nLen = recv(nSocket, szBuffer, sizeof(szBuffer), 0); if (nLen>=0) { printf("Received %d bytes from the %s socket\n", nLen, nSocket==nUDPSocket? "UDP":"TCP"); }else { fprintf(stderr, "%s: recv() failed: %d\n", szProgramName, errno); exit (EXIT_FAILURE); } } while (nLen>0); break; } }
return EXIT_SUCCESS; }
|
-- Gruß, virtual Quote of the Month Ich eß' nur was ein Gesicht hat (Creme 21) |