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

  • SEARCH
  • Home
  • 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 381751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:06:57+00:00 2026-05-12T15:06:57+00:00

I am trying to achieve peer to peer communication using winsock but gethostbyaddr always

  • 0

I am trying to achieve peer to peer communication using winsock but gethostbyaddr always return me NULL ,this thing works only on localhost, server_name is destination ip address

server_name="<--ipaddress-->"

struct sockaddr_in server;
addr = inet_addr(server_name);
cout<<"inet_addr(server_name) "<<addr<<endl;
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
memset(&server, 0, sizeof(server));


memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET, socket_type, 0); 

connect(conn_socket, (struct sockaddr*)&server, sizeof(server))

We have already achieved p2p communication using python and it works perfectly fine on same port no and address .. thanks for any clue..

I do not have any idea how to do it in c++, in python we just used bind(—) , Can somebody show me code snippet how to achieve it.

  • 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-12T15:06:57+00:00Added an answer on May 12, 2026 at 3:06 pm

    Where are you getting server_name from? Are you sure it’s a valid IP address?
    Also, check WSAGetLastError() to see specifically what’s going wrong.

    Remember that not all hostnames have reverse DNS entries. It’s perfectly legitimate for gethostbyaddr to fail on a real, valid IP address. If you’re doing p2p, it’s best not to rely on host names at all, except perhaps for diagnostic displays (and fall back to IP addresses if reverse lookups fail).

    Edit: With your new, expanded code sample, it’s clear that you actually don’t need gethostbyaddr at all.

    struct sockaddr_in server;
    memset((void*)&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.S_un.S_long = inet_addr(server_name);
    server.sin_port = htons(port);
    conn_socket = socket(AF_INET, socket_type, 0); 
    
    connect(conn_socket, (struct sockaddr*)&server, sizeof(server))
    

    gethostbyaddr is only needed when you need the reverse DNS name of the server in question. inet_addr already gives you a suitable address to connect to.

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

Sidebar

Related Questions

Trying to achieve something like this using recursion: if (m > n) return; Foo
Im trying to achieve an output like this {status:ok,0:{id:11,title:digg,url:http://www.digg.com}} but instead i am getting
I am trying to achieve lookup method injection using a simple example. But, seems
I'm trying to achieve the following goal: Using this general singleton class: abstract class
I am trying to achieve the following: Using Skype, call my mailbox (works) Enter
Im trying to achieve an insert statment and return using my function in the
im trying to achieve something but i dont really know how I have set
I am trying to achieve something like this. The Expandable List consists of the
I am trying to achieve something in MySQL but even with all the answers
im trying to achieve the following, in php i have a form like this:

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.