005
16.12.2007, 17:16 Uhr
Tron
|
Moin,
hatte gerade nichts anderes vor.
Dein Code mit leichter Änderung.
C++: |
#include <winsock2.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <iphlpapi.h> #include <errno.h>
#pragma argsused int main(void) { WSADATA wsaData; SOCKET upnp_sock; int i=0; int j=0; int bytes=0; int remo_addr_len; struct sockaddr_in remo_addr; struct sockaddr_in listen_addr; char buff[500]; WSAStartup(MAKEWORD(2, 2), &wsaData) ;
upnp_sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); printf("\nSOCKET ERROR:%i\n",WSAGetLastError());
listen_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); listen_addr.sin_port = htons(1905); listen_addr.sin_family = AF_INET;
bind(upnp_sock, (struct sockaddr*) &listen_addr, sizeof(listen_addr)); printf("\nBIND ERROR:%i\n",WSAGetLastError());
printf("Versuche irgendetwas zu empfangen...\n"); remo_addr_len = sizeof(remo_addr); i=1; while (j<10) { bytes=recvfrom(upnp_sock, buff, sizeof(buff), 0, (struct sockaddr *) &remo_addr, &remo_addr_len); if (bytes == -1) { printf("\nRECVFROMERROR:%i\n",WSAGetLastError()); } printf("Bytes:%d\n", bytes); buff[bytes]='\0'; bytes=0; // Ausgabe des Durchlauf und Inhalt von buff. printf("%d . %s",i, buff); /*while (buff[i] != '\0' ) { printf("%s", buff); }*/
printf("\n\n#############\n\n");
// Bestätigung an Client senden. sendto( upnp_sock, "OKAY", 5, 0, (SOCKADDR*)&remo_addr, remo_addr_len);
memset(&buff, '\0', sizeof(buff)-1); j++; i++; } printf("Feddish\n"); closesocket (upnp_sock); getchar();
return 0; }
|
Test mit UDP-Client
C++: |
#include <stdio.h> #include <winsock2.h>
#define SERVER_PORT 1905 #define BUF_MAXLEN 256
long WinsockStartup();
int main() { SOCKET sock; SOCKADDR_IN addr; long rc; char buffer[BUF_MAXLEN]; char hostAdress[] = "127.0.0.1"; rc = WinsockStartup(); if (rc == SOCKET_ERROR) { printf("Error: Winsock Startup failed: %d\n", WSAGetLastError() ); return 0; }
// socket anlegen sock=socket(AF_INET,SOCK_DGRAM,NULL); if (sock == INVALID_SOCKET) { printf("Error: Cannot create Socket: %d\n", WSAGetLastError() ); return 0; }
memset(&addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten addr.sin_family = AF_INET; // Adress Familie addr.sin_port = htons(SERVER_PORT); // Port-Nummer addr.sin_addr.s_addr = inet_addr(hostAdress); // host for( ;; ) { sendto( sock, "test", 5, 0, (SOCKADDR*)&addr, sizeof (SOCKADDR)); rc = recvfrom( sock, buffer, sizeof buffer, 0, 0, 0); if( rc > 0 ) printf("%s\n", buffer); //else printf("%d %d .",rc, WSAGetLastError () - WSABASEERR); else printf("."); Sleep( 500 ); } return 0; }
/* ******************************************************************** */ long WinsockStartup() { long rc; WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 1); rc = WSAStartup( wVersionRequested, &wsaData ); return rc; }
|
Sollte jetzt auch bei Dir funktionieren. |