int run_tests() { int i, j; int success = 0, total = 0; for (i = 0; i < (sizeof(test_cases) / sizeof(test_cases[0])); i++) { uint8_t nxt_hdr; uint8_t result[512]; uint8_t *rv; struct udp_hdr *udp = (struct udp_hdr *)result; total++; rv = unpack_udp(result, &nxt_hdr, test_cases[i].pack); if (test_cases[i].pack_len != rv - test_cases[i].pack) { printf("ERROR: wrong unpack length: %li %p %p\n", (rv - test_cases[i].pack), test_cases[i].pack, rv); continue; } if (test_cases[i].result.srcport != udp->srcport) { printf("ERROR: wrong srcport\n"); continue; } if (test_cases[i].result.dstport != udp->dstport) { printf("ERROR: wrong dstport\n"); continue; } if (test_cases[i].result.len != udp->len) { printf("ERROR: wrong length\n"); continue; } if (test_cases[i].result.chksum != udp->chksum) { printf("ERROR: wrong chksum: 0x%x 0x%x\n", test_cases[i].result.chksum, udp->chksum); continue; } success++; } printf("%s: %i/%i tests succeeded\n", __FILE__, success, total); if (success == total) return 0; return 1; }
int _tmain(int argc, _TCHAR* argv[]) { struct sockaddr_in si_me, si_other; int s, slen=sizeof(si_other); char buf[BUFLEN]; // int num; // int i; g_btmid = 0; g_cur_init_pulse = 0; g_cur_report_pulse = 0; //版本协商 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return -1; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return -1; } if ((s=socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); return -1; } bzero((char *) &si_me, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(SERVER_PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (sockaddr *)&si_me, sizeof(si_me))==-1) { perror("bind"); return -1; } printf("program start...\n"); //for (i=0; i<NPACK; i++) while(1) { if (recvfrom(s, (char *)udp_recv_buf, sizeof(udp_recv_buf), 0, (sockaddr *)&si_other, &slen)==-1) { perror("recvfrom()"); return -1; } printf("Received packet from %s:%d\nDatanum: %d\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); unpack_udp(); show_recv(); load_udp_data(); pack_udp(); if (sendto(s, (char *)udp_send_buf, sizeof(udp_send_buf), 0, (sockaddr *)&si_other, slen) == -1) { perror("sendto()"); return -1; } } closesocket(s); WSACleanup(); return 0; }