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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:56:23+00:00 2026-06-05T12:56:23+00:00

Here is the code of program, which should send a simple ‘ping’. I know

  • 0

Here is the code of program, which should send a simple ‘ping’. I know that i could use IcmpSendEcho2() function, but i decide to do this this way:

struct ICMPh
{
unsigned char typ;
unsigned char kod;
unsigned short suma_spr;
unsigned short id;
unsigned short seq;
};
unsigned short CalcChecksum (char *pBuffer, int nLen)
 { 
  ////Checksum for ICMP is calculated in the same way as for IP header
  //This code was taken from: http://www.netfor2.com/ipsum.htm

  unsigned short nWord;
  unsigned int nSum = 0;
  int i;


  for (i = 0; i < nLen; i = i + 2)
  {
      nWord =((pBuffer [i] << 8)& 0xFF00) + (pBuffer [i + 1] & 0xFF);
      nSum = nSum + (unsigned int)nWord;  
  }


 while (nSum >> 16)
  {
      nSum = (nSum & 0xFFFF) + (nSum >> 16);
  }

  //One's complement the result
  nSum = ~nSum;

  return ((unsigned short) nSum);
 }
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)==SOCKET_ERROR)
{
    cout<<"ERROR at startup:\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    return -1;
}
SOCKET soc;
int seq=0;

soc=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(soc==INVALID_SOCKET)
{
    cout<<"ERROR: INVALID SOCKET\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    return -1;
}
char* addres=new char;
cin>>addres;

SOCKADDR_IN adr;
adr.sin_addr.S_un.S_addr=inet_addr(addres);
adr.sin_family=AF_INET;
adr.sin_port=rand();

ICMPh icmp;
icmp.kod=0;
icmp.seq=htons(seq++);
icmp.typ=8;
icmp.suma_spr=0;
icmp.id=htons(rand());

int size=32;
char *buffer;

buffer=new char[sizeof(ICMPh)+size];
memcpy_s(buffer,sizeof(ICMPh),&icmp,sizeof(ICMPh));
memset(buffer+sizeof(ICMPh),'x',size);

icmp.suma_spr=htons(CalcChecksum(buffer,sizeof(ICMPh)+size));

memcpy_s(buffer,sizeof(ICMPh),&icmp,sizeof(ICMPh));

if(sendto(soc,buffer,sizeof(ICMPh)+size,0,(SOCKADDR*)&adr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
{
    cout<<"FAIL at 'sendto':\n";
    cout<<WSAGetLastError()<<"\n";
    system("pause");
    WSACleanup();
    delete []buffer;
    return -1;
}
else
    cout<<"FINE";
WSACleanup();
system("pause");
delete []buffer;
return 0;
} 

And here is the problem. Everything is working fine untill it comes to sendto() function. I get an 10013 Error code form WSAGetLastError() which means WSAEACCES Permission denied
I start this application witch admin privliges, so i have no idea why i cant send data using this socket configuration.

  • 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-06-05T12:56:25+00:00Added an answer on June 5, 2026 at 12:56 pm

    For sendto(), WSAEACCES is not related to user permissions. You would have gotten an error on socket() instead if you did not have enough rights to use a RAW socket. Read the documentation. When sendto() reports WSAEACCES, it means your sockaddr_in struct was not filled in correctly. I see three bugs in your code related to that:

    1) You are not zeroing out the sockaddr_in before using it. It is important to do so.

    2) You are allocating a 1-character buffer to receive the target address from the user. The buffer needs to be 16 characters instead for an IPv4 address (including the null terminator). So you have a buffer overflow.

    3) You are not calling htons() when assigning the sin_port field.

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

Sidebar

Related Questions

Here's a piece of code that takes most time in my program, according to
I've got a program that worked until recently. The offending code is shown here:
hi i have a cuda program which run successfully here is code for cuda
Here's some code (full program follows later in the question): template <typename T> T
I am writing a program in Haskell here it is the code module Main
First, here is the C# code and the disassembled IL: public class Program<T> {
I'm trying to create a client-server program that send and receive a string (its
I have a program which plays two sounds. I want the user to know
I'm writing a little program, and here is what it should do. In the
Alright, I made a program which should work, but unfortunately doesn't. It loads a

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.