016
21.05.2003, 20:20 Uhr
virtual
Sexiest Bit alive (Operator)
|
Tut mir Leid, daß ich gestern keine Zeit mehr hatte. Ich habe mich mal eben dran gemacht, ein Beispielprogram zu schreiben. Arbeitet mit UDP und macht das, was Dein Server/Client so tun sollte. Den Server startet man mit
Den Client analog dazu mit
Der client sollte dann 'b' zurückbekommen. Hier der Source:
C++: |
/*udp_send.c*/ /* Define nur wegen inet_aton */ #define _BSD_SOURCE
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
#define USAGE fprintf(stderr, "usage: %s server-port character\n", szProgram);
int main( int p_nArgC, char** p_pszArgV) { const char* szProgram = *p_pszArgV+strlen(*p_pszArgV)-1; unsigned short nPort; int hSocket; struct sockaddr_in oAddress; socklen_t nLen; char cChar;
/* * Get invokation name */ while (szProgram != *p_pszArgV && (szProgram[-1]!='/' || szProgram[-1]!='\\') ) { szProgram--; }
/* * Check command line */ if (3 != p_nArgC) { fprintf(stderr, "error: %s: illegal command line.\n", szProgram); USAGE; exit (EXIT_FAILURE); } if (1!=sscanf(p_pszArgV[1], "%hu", &nPort)) { fprintf(stderr, "error: %s: illegal port number.\n", szProgram); USAGE; exit (EXIT_FAILURE); }
cChar = p_pszArgV[2][0];
/* * Setup server address */ memset(&oAddress, 0, sizeof(oAddress)); oAddress.sin_family = AF_INET; inet_aton("127.0.0.1", &oAddress.sin_addr); oAddress.sin_port = htons(nPort);
/* * Create the socket */ hSocket = socket(AF_INET, SOCK_DGRAM, 0); if (-1 == hSocket) { fprintf(stderr, "error: %s: socket() failed: %s.\n", szProgram, strerror(errno)); exit(EXIT_FAILURE); }
/* * Send the message */ if (1 != sendto(hSocket, &cChar, 1, 0, (struct sockaddr*)&oAddress, sizeof(oAddress))) { fprintf(stderr, "error: %s: sendto() failed: %s.\n", szProgram, strerror(errno)); close(hSocket); exit(EXIT_FAILURE); }
/* * Wait for response */ nLen = sizeof(oAddress); if (1 != recvfrom(hSocket, &cChar, 1, 0, (struct sockaddr*)&oAddress, &nLen)) { fprintf(stderr, "error: %s: recvfrom() failed: %s.\n", szProgram, strerror(errno)); close(hSocket); exit(EXIT_FAILURE); }
/* * Give information about response */ printf("%s:%hu answers: \'%c\'\n", inet_ntoa(oAddress.sin_addr), ntohs(oAddress.sin_port), cChar);
/* * Cleanup */ close(hSocket); return EXIT_SUCCESS; }
|
C++: |
/* udp_recv.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
#define USAGE fprintf(stderr, "usage: %s port\n", szProgram);
int main( int p_nArgC, char** p_pszArgV) { const char* szProgram = *p_pszArgV+strlen(*p_pszArgV); unsigned short nPort; int hListen; struct sockaddr_in oAddress;
/* * Get invokation name */ while (szProgram != *p_pszArgV && (szProgram[-1]!='/' || szProgram[-1]!='\\') ) { szProgram--; }
/* * Check command line */ if (2 != p_nArgC) { fprintf(stderr, "error: %s: illegal command line.\n", szProgram); USAGE; exit (EXIT_FAILURE); } if (1!=sscanf(p_pszArgV[1], "%hu", &nPort)) { fprintf(stderr, "error: %s: illegal port number.\n", szProgram); USAGE; exit (EXIT_FAILURE); }
/* * Create the socket */ hListen = socket(AF_INET, SOCK_DGRAM, 0); if (-1 == hListen) { fprintf(stderr, "error: %s: socket() failed: %s.\n", szProgram, strerror(errno)); exit(EXIT_FAILURE); }
/* * Bind the socket */ memset(&oAddress, 0, sizeof(oAddress)); oAddress.sin_family = AF_INET; oAddress.sin_port = htons(nPort); oAddress.sin_addr.s_addr = htonl(INADDR_ANY); if (0 != bind(hListen,
(struct sockaddr*)&oAddress, sizeof(oAddress))) { fprintf(stderr, "error: %s: bind() failed: %s.\n", szProgram, strerror(errno)); close(hListen); exit(EXIT_FAILURE); }
/* * Loop to process data */ printf("Waiting for incoming requests...\n");
for(;;) { struct sockaddr_in oClientAddress; socklen_t nClientLen; char cChar;
/* * Receive data */ nClientLen = sizeof(oClientAddress); if (1 != recvfrom(hListen, &cChar, 1, 0, (struct sockaddr*)&oClientAddress, &nClientLen)) { fprintf(stderr, "error: %s: recvfrom() failed: %s.\n", szProgram, strerror(errno)); close(hListen); exit(EXIT_FAILURE); }
/* * Give information about source */ printf("Processing request from %s:%hu, character is \'%c\'.\n", inet_ntoa(oClientAddress.sin_addr), ntohs(oClientAddress.sin_port), cChar);
/* * Increase character */ ++cChar;
/* * Send response */ if (1 != sendto(hListen, &cChar, 1, 0, (struct sockaddr*)&oClientAddress, nClientLen)) { fprintf(stderr, "error: %s: sendto() failed: %s.\n", szProgram, strerror(errno)); close(hListen); exit(EXIT_FAILURE); } } }
|
-- Gruß, virtual Quote of the Month Ich eß' nur was ein Gesicht hat (Creme 21) Dieser Post wurde am 21.05.2003 um 21:05 Uhr von virtual editiert. |