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 7069137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:24:33+00:00 2026-05-28T05:24:33+00:00

Actually, I want to create an application in C such that 2 people can

  • 0

Actually, I want to create an application in C such that 2 people can chat with each other. Let us assume they know their IP (Actually, I think I am making the mistake here. I get my IPs from http://www.whatismyip.com).

void recv_data(char *from, unsigned short int Port, char *data, int data_length)
{
                WSADATA wsaData;
                SOCKET RecvSocket;
                sockaddr_in RecvAddr;
                char RecvBuf[data_length];
                sockaddr_in SenderAddr;
                int SenderAddrSize = sizeof (SenderAddr);
                WSAStartup(MAKEWORD(2, 2), &wsaData);
                RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                RecvAddr.sin_family = AF_INET;
                RecvAddr.sin_port = htons(Port);
                   RecvAddr.sin_addr.s_addr = inet_addr(from);
                bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
                recvfrom(RecvSocket, RecvBuf, data_length, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
                int i;
            for(i=0;i<=data_length-1;i++)
                *(data+i)=RecvBuf[i];
            WSACleanup();
}

The above is a function to receive what the other person is sending. It works great when “127.0.0.1” is the value of from but when my ip (117.193.52.176) is used, something else appears. Could anyone tell me where I am wrong ?

  • 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-28T05:24:33+00:00Added an answer on May 28, 2026 at 5:24 am

    The address you passing to “bind” is likely wrong. Just use the IP of INADDR_ANY (0) for the call to bind. I suspect 117.193.52.176 is likely your external IP address outside of your home NAT. Your PC’s real IP address is 192.168.1.2 or something like that. Type “ipconfig /all” from the command line. In any case, just bind to INADDR_ANY so you don’t have to know your real IP address.

    Other issues with this code:

    1. Not checking return values from socket APIs
    2. Don’t call WSAStartup and WSACleanup for every recvfrom call. Just call WSAStartup once in your app, and don’t worry about calling WSACleanup.
    3. I’m not entirely sure if the line “char RecvBuf[data_length];” will compile. (Dynamically length static buffer on the stack? Maybe it’s a new compiler feature).
    4. Don’t create a new socket for every recvfrom call. Create it once and bind to it, then use it for all subsequent send/recv calls.

    5.. A more fundamnetal design problem. Unless both you and person you are communicating with are directly connected to the Internet (not NAT and no firewall), sending and receiving UDP packets will be difficult. Read the article on hole-punching here.

    In any case, here’s a cleaner version of your code:

    int g_fWinsockInit = 0;
    
    void initWinsock()
    {
        WSADATA wsaData = {};
    
        if(!g_fWinsockInit)
        {
            WSAStartup(MAKEWORD(2,2), &wsaData);
            g_fWinsockInit = 1;
        }
    }
    
    void recv_data(char *from, unsigned short int Port, char *data, int data_length)
    {
        SOCKET RecvSocket;
        sockaddr_in RecvAddr = {}; // zero-init, this will implicitly set s_addr to INADDR_ANY (0)
    
        sockaddr_in SenderAddr = {}; // zero-init
        int SenderAddrSize = sizeof(SendAddr);
        int ret;
    
        initWinsock();
    
        RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (RecvSocket == INVALID_SOCK)
        {
            printf("Error - socket failed (err = %x)\n", WSAGetLastError());
            return;
        }
    
        RecvAddr.sin_family = AF_INET;
        RecvAddr.sin_port = htons(Port);
    
        ret = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
        if (ret < 0)
        {
           printf("bind failed (error = %x)\n", WSAGetLastError());
           return;
        }
    
        ret = recvfrom(RecvSocket, data, data_length, 0, (SOCKADDR *) &SenderAddr, &SenderAddrSize);
    
        if (ret < 0)
        {
           printf("recvfrom failed (error = %x)\n", WSAGetLastError());
        }
        else
        {
            printf("received %d bytes\n");
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i want to create an application that works like spring quartz i can
I want to create an application that can transform text the user enters in
I want to create an application that logs when I have gone jogging and
I want to develop small application in asp.net using sqlite, actually I don't know
I want to create a truth or dare game for android. I am actually
Hi every one actually I want to make a com.google.gson.JsonArray of data:[1,2,3,4,5].How can i
Does anyone know how to use different provider for membership? Actually i want to
Actually I don't know whether my question is related to this thread. I want
I want to create a bar chart application in windows based .net application, i
Hi I'm trying to create a simple music library for my application that uses

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.