Hey,
I'm trying to make a simple UDP server, which sends current time to the specified port on localhost (every 2 sec), but each time sendto() function returns error. I'll be grateful if you just take a quick look on the code, I suppose the solution may be quite obvious
Code:/* http://www.gomorgan89.com (based on) */ /* Link with library file wsock32.lib */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock.h> #include <time.h> void wait ( int seconds ) { clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } int main() { WSADATA w; SOCKET sd; struct sockaddr_in server; struct sockaddr_in client; unsigned short port_number = 5123; int a1 = 127, a2 = 0, a3 = 0, a4 = 1; int client_length; time_t current_time; WSAStartup(0x0101, &w); sd = socket(AF_INET, SOCK_DGRAM, 0); memset((void *)&server, '\0', sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_port = htons(port_number); server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1; server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2; server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3; server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4; bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)); while(1) { client_length = (int)sizeof(struct sockaddr_in); current_time = time(NULL); if (sendto(sd, (char *)¤t_time, (int)sizeof(current_time), 0, (struct sockaddr *)&client, client_length) == (int)sizeof(current_time)) { printf("Time sent\n"); } else { printf("Error sending time\n"); } wait(2); } closesocket(sd); WSACleanup(); return 0; }



LinkBack URL
About LinkBacks



