Search code examples
cnetwork-programmingudpclient-serversendto

sendto(): Transport endpoint is not connected error while trying to send a response from server using UDP


I was trying to do simple client-server program using UDP where i'll send one string form client to server and the server acknowledge "got it". Things are working fine until i try to send the ack. The send to generates a "Transport endpoint is not connected" error. i'm using sentto() to sent in the following format it server.

sendto(sid, message, strlen(message), 0, (struct sockaddr *) &saddr, len);

and receive from like

recvfrom(sid, message, 50, 0, (struct sockaddr*) &caddr, &len);

i'm using AF_UNIX socket. The program is working fine when i ported it to AF_INET.


Solution

  • You should use recvfrom() to read from the socket and pass the received socketinformation back to sendto() like this:

    struct sockaddr_in client;
    socklen_t slen = sizeof(client);
    
    int l  = recvfrom(fd, buf,      sizeof(buf), 0, &client, &slen);
    int st = sendto  (fd, "got it", 6,           0, &client,  slen);
    

    This should work as you expect