Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:03:12+00:00 2026-05-23T11:03:12+00:00

I am trying to send some data with a udp socket and receive them

  • 0

I am trying to send some data with a udp socket and receive them back on the same socket through windows loopback adatper. In my network properties I set the loopback adapter to have the following ip 192.168.1.1

the recvfrom function returns -1 indicating an error. I also monitor the traffic on the loopback adapter with wireshark and nothing seem to be sent to the loopback adapter, I see no trafic.

Is it true that on windows we can’t use the loopback address(127.0.0.1) ? I saw that on some forums, that is why I try to use the loopback adapter.
I also tried to send directly to my own ip, but it gives no better results. Btw it is possible to send to his own ip and get the data back?

I would appreciate any help and just in case, I am new to socket programming.

Below is my code:

#define DST "192.168.1.1"

int _tmain(int argc, char* argv[])
{


int    numbytes;
int    bytes_sent;
int    server_sock;
char   send_msg[100];
int    send_msg_length = 100;
char   rcv_msg[100] = { 0 };
int    rcv_msg_length = 100;
int    i;

WSADATA wsaData;   
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
    fprintf(stderr, "WSAStartup failed.\n");
    return 1;
}

sockaddr_in to_addr;
sockaddr_in me;

unsigned short Port = 27015;
to_addr.sin_family      = AF_INET;
to_addr.sin_port        = htons(Port);
to_addr.sin_addr.s_addr = inet_addr(DST);

me.sin_family           = AF_INET;
me.sin_port             = 0;
me.sin_addr.s_addr      = htonl(INADDR_ANY);
memset( &(me.sin_zero), '\0', 8 );

if ((server_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
    perror("talker: socket");
}

if ( bind( server_sock, (SOCKADDR *)&me, sizeof( me ) ) == -1)
{
    printf("Error binding/n");
    return 1;
};

int length = sizeof( to_addr );
bytes_sent = sendto(server_sock, send_msg, send_msg_length, 0, (SOCKADDR *)&to_addr, length);
if (bytes_sent == -1)
{
    perror("talker: sendto");
    exit(1);
}

printf("Sent %d bytes to %s\n", bytes_sent, DST);
printf("listener: waiting to recvfrom...\n");

if ( numbytes = recvfrom(server_sock, rcv_msg, rcv_msg_length, 0, (SOCKADDR *)&to_addr, &length ) ) 
{
    perror("recvfrom");
    exit(1);
}

closesocket(server_sock);
WSACleanup();

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:03:13+00:00Added an answer on May 23, 2026 at 11:03 am

    You are binding your server socket to port 0, i.e. asking the OS to assign random port number to it. That has to be the same port you are sending to, 27015 in your case.

    Also, you don’t have to byte-swap INADDR_ANY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send some small data over UDP using the AsyncUdpSocket library .
I am trying to send/receive some data across 2 computers (mac, ubuntu) using a
I am trying to send some data from a LINQ query in C# to
I am currently trying to send some data from and Android application to a
I'm trying to send some data to PHP using HTTPService POST but for some
I am trying to send some JSON data to my ASP.NET MVC3 controller action
I'm trying to send some String Data from sub activity to main activity and
Trying to send some data form my previous ViewController. To be more clear trying
I am trying to get some data from the user and send it to
I am trying to get some simple UDP communication working on my local network.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.