c - recvfrom() is stuck and I don't know why -
i have project school. goal create ping program use echo service (c programming). echo service runs on udp port 7 , have activated simple tcp/ip. echo supposed reply message if gets one. here code made:
// ping.cpp : entry point #include "ping.h" #pragma comment(lib,"ws2_32") //library int main(int argc, char *argv[]) { //variable char ipadress[] = "127.0.0.1"; word versionprotocolesocket; socket pingsocket; struct sockaddr_in destination; char packet[max_packet_size], reply[max_packet_size]; int err, msglength; //initialisation of windows socket printf("initialising wsa 2.2...\n"); versionprotocolesocket = makeword(2, 2); err = initialisesocket(versionprotocolesocket); if (err != 0) { errsocket(err); printf("the program stop\n"); _getch(); return exit_failure; } else printf("initialisation success\n\n"); //socket creation printf("socket creation...\n"); if ((pingsocket = socket(af_inet, sock_dgram, ipproto_udp)) == socket_error) { printf("can't create socket\nthe program going stop\n"); _getch(); return exit_failure; } else printf("creation sucess\n\n"); //préparation des variables pour l'utilisation de la fonction sendto() destination.sin_family = af_inet; destination.sin_port = htons(7); destination.sin_addr.s_addr = inet_addr(adresseip); msglength = sizeof(message); int addrsize = sizeof(destination); //beginning of ping (int = 0; < 5; i++) { //sending if (sendto(pingsocket, message, messagelength, 0, (struct sockaddr *)&destination, addrsize) == socket_error) printf("can't send message : %d\n", wsagetlasterror()); else printf("message %d sent\n", i); //reception if (recvfrom(pingsocket, reply, max_packet_size, 0, null, null) == socket_error) printf("error : %d\n", wsagetlasterror()); else printf("message %d received\n", i); } _getch(); return exit_success; } int initialisersocket(word version) { wsadata wsadata; int err = wsastartup(version, &wsadata); return err; } void errsocket(int err) { switch (err) { case wsasysnotready: printf("system not ready\n"); break; case wsavernotsupported: printf("unsuported protocol version\n"); break; case wsaeinprogress: printf("a version running\n"); break; case wsaeproclim: printf("max task reached\n"); break; case wsaefault: printf("pointer lpwsadata not valid\n"); break; default: printf("unknown error\n"); break; } }
now ping.h code :
#pragma once #include "stdio.h" #include "winsock2.h" #include "conio.h" #include "stdint.h" int initialisesocket(word version); void errsocket(int err); #define max_packet_size 10*1024
the problem program stuck on recvfrom function , have no idea why. can explain?
you have no socket listening packets. need bind receiving socket address , port want use.
also note udp not guarantee delivery. if nothing listening packet, thrown away. need listening before send packet. obviously, presents problem in single thread. typically, have 2 separate processes (either different programs or created forking). 1 process create socket , listen on port , other create socket , send data.
Comments
Post a Comment