|
One other thought - you have to use sendto() unless you call connect() on your socket.
Calling send() will fail because there's no destination address specified (not even the broadcast
address).
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
Hello Mark,
I'm confused... I must be missing something!
Here is my code (same as yours with error handling). I compile using visual C++ 6.0. I have tested it on Windows XP Service Pack 1 and 2.
If I execute ipconfig /release, and then the following code, I get the "Error sending Packet : 10065" displayed.
Can you please e-mail me your code?
Thank you very much for your help.
#include "stdio.h"
#include "winsock2.h"
int main()
{
//Initialise Winsock
WSADATA wsock;
printf("\nInitialising Winsock...\n");
if (WSAStartup(MAKEWORD(2,2),&wsock) != 0){
fprintf(stderr,"WSAStartup() failed");
exit(EXIT_FAILURE);
}
printf("Initialised successfully.\n");
sockaddr_in bindsocketaddr;
memset(&bindsocketaddr, 0, sizeof(bindsocketaddr));
bindsocketaddr.sin_family = AF_INET;
bindsocketaddr.sin_port = htons(2700);
bindsocketaddr.sin_addr.s_addr = INADDR_ANY;
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock==SOCKET_ERROR){
printf("Creation of socket failed.");
return 0;
}
BOOL fBroadcast = 1;
int iResult = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&fBroadcast, sizeof(BOOL));
if (iResult == SOCKET_ERROR){
printf("Error setsockopt : %d\n",WSAGetLastError());
}
iResult =bind(sock, (sockaddr *)&bindsocketaddr, sizeof(bindsocketaddr));
if (iResult == SOCKET_ERROR ){
printf("Unable to bind.");
return 0;
}
#define DHCP_SERVER_PORT 67
sockaddr_in destsocketaddr;
memset(&destsocketaddr, 0, sizeof(destsocketaddr));
destsocketaddr.sin_family = AF_INET;
destsocketaddr.sin_port = htons(DHCP_SERVER_PORT);
destsocketaddr.sin_addr.s_addr = INADDR_BROADCAST;
// broadcast 5 meaningless bytes to any listening DHCP server
iResult = sendto(sock, "12345", 5, 0, (const struct sockaddr*)&destsocketaddr, sizeof(destsocketaddr));
if (iResult == SOCKET_ERROR){
printf("Error sending Packet : %d\n",WSAGetLastError());
}
closesocket(sock);
return 0;
}
|
|
|
|
|
locquet wrote: If I execute ipconfig /release, and then the following code, I get the "Error sending Packet : 10065" displayed.
Ohhh - yeah that won't work. If you release the ip for the adapter then, well you'll get the
error you are seeing.
I'm going to take a look at that code again - I'll be back.
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
It was me that was confused If I had read your original post carefully I would have noticed
the "after ipconfig /release" part. Sorry.
What did you try with raw sockets? Can you post sample code?
Also, are you required to do the DHCP communication yourself? If not, then maybe the IPHelper
APIs can help. Check out IPReleaseAddress()/IPRenewAddress().
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) Unfortunately, I have to write the DHCP communication myself. I have to set configurable DHCP Options, and to choose how to treat DHCP ACK and OFFER Messages. According to my understanding of the IPHelper api, it is not possible.
Here is an extract of my code using a raw socket, but I do not think that the solution consists in using raw sockets.
Tell me if you want me to email you the whole project.
char host[100],buf[1000],*data=NULL,source_ip[20];
SOCKET s;<br />
int k=1;<br />
<br />
struct pseudo_entete pseudo_udp;<br />
<br />
IPV4_HDR *v4hdr=NULL;<br />
UDP_HDR *udphdr=NULL;<br />
<br />
DhcpMessage * pdmDiscover = new DhcpMessage();
<br />
int payload=DHCP_MESSAGE_SIZE , optval;<br />
SOCKADDR_IN dest;<br />
hostent *server;<br />
<br />
WSADATA wsock;<br />
printf("\nInitialising Winsock...");<br />
if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)<br />
{<br />
fprintf(stderr,"WSAStartup() failed");<br />
exit(EXIT_FAILURE); <br />
} <br />
printf("Initialised successfully.");<br />
<br />
printf("\nCreating Raw Socket...");<br />
if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==SOCKET_ERROR)<br />
{<br />
printf("Creation of raw socket failed.");<br />
return 0;<br />
}<br />
printf("Raw TCP Socket Created successfully.");<br />
<br />
<br />
BOOL OptValue = 1 ;<br />
if (setsockopt( s, SOL_SOCKET, SO_BROADCAST, (char *)&OptValue, sizeof(BOOL))==SOCKET_ERROR) {<br />
printf("failed to set socket in broadcast mode.");<br />
return 0;<br />
}<br />
printf("Set socket in broadcast mode successful.");<br />
<br />
host[0] = 0;<br />
strcpy(host, "255.255.255.255");<br />
printf("\nResolving Hostname...");<br />
if((server=gethostbyname(host))==0)<br />
{<br />
printf("Unable to resolve.");<br />
return 0;<br />
}<br />
dest.sin_family = AF_INET;<br />
dest.sin_port = htons(67);
memcpy(&dest.sin_addr.s_addr,server->h_addr,server->h_length);<br />
printf("Resolved.");<br />
<br />
source_ip[0] = 0;<br />
strcpy(source_ip, "0.0.0.0");<br />
<br />
v4hdr = (IPV4_HDR *)buf;
v4hdr->ip_version=4;<br />
v4hdr->ip_header_len=5;<br />
v4hdr->ip_tos = 0;<br />
v4hdr->ip_total_length = htons ( sizeof(IPV4_HDR) + sizeof(UDP_HDR) + payload );<br />
v4hdr->ip_id = htons(2);<br />
v4hdr->ip_frag_offset = 0;<br />
v4hdr->ip_frag_offset1 = 0;<br />
v4hdr->ip_reserved_zero = 0;<br />
v4hdr->ip_dont_fragment = 0;
v4hdr->ip_more_fragment = 0;<br />
v4hdr->ip_ttl = 8;<br />
v4hdr->ip_protocol = IPPROTO_UDP;<br />
v4hdr->ip_srcaddr = inet_addr(source_ip);<br />
v4hdr->ip_destaddr = inet_addr(inet_ntoa(dest.sin_addr));<br />
v4hdr->ip_checksum = 0;<br />
<br />
udphdr = (UDP_HDR *)&buf[sizeof(IPV4_HDR)];<br />
<br />
udphdr->sport = htons(68);<br />
udphdr->dport = htons(67);<br />
udphdr->Length = htons(8 + payload);<br />
udphdr->Checksum = 0;<br />
<br />
data = &buf[sizeof(IPV4_HDR) + sizeof(UDP_HDR)];<br />
memcpy(data, pdmDiscover->m_rgwbMessage [0].buf, payload);<br />
<br />
v4hdr->ip_checksum = checksum((unsigned short *)&v4hdr, sizeof(ip_hdr));<br />
<br />
<br />
pseudo_udp.ip_source=inet_addr(source_ip);<br />
pseudo_udp.ip_destination=inet_addr(inet_ntoa(dest.sin_addr));<br />
pseudo_udp.mbz=0;<br />
pseudo_udp.type=IPPROTO_UDP;<br />
pseudo_udp.length=htons((unsigned short)(sizeof(udp_hdr)+(unsigned short)payload));<br />
<br />
char tampon[65535];<br />
<br />
memcpy(tampon,&pseudo_udp,sizeof(pseudo_udp));<br />
memcpy(tampon+sizeof(pseudo_udp),&udphdr,sizeof(udphdr));<br />
memcpy(tampon+sizeof(pseudo_udp)+sizeof(udphdr),data,payload);<br />
int checksum1=checksum((unsigned short*)tampon,sizeof(pseudo_udp)+sizeof(udphdr)+payload);<br />
udphdr->Checksum = 0x3b92;<br />
<br />
printf("\nSending packet...\n");<br />
<br />
sockaddr_in saddr ;<br />
saddr.sin_family = AF_INET ;<br />
saddr.sin_port = htons(68) ; <br />
saddr.sin_addr.S_un.S_addr = INADDR_ANY ;<br />
<br />
int iRet = bind(s, (LPSOCKADDR) & saddr, sizeof saddr) ;<br />
if (iRet == SOCKET_ERROR ){<br />
printf("Unable to bind.");<br />
return 0;<br />
}<br />
<br />
<br />
iRet = sendto(s , buf , sizeof(IPV4_HDR)+sizeof(UDP_HDR) + payload, 0,<br />
(SOCKADDR *)&dest, sizeof(dest)))==SOCKET_ERROR);<br />
if (iRet == SOCKET_ERROR ){<br />
<br />
printf("Error sending Packet : %d\n",WSAGetLastError());<br />
break;<br />
}<br />
<br />
|
|
|
|
|
Hi, we finally managed to solve this problem using RawEther.
Thanks a lot for your help.
|
|
|
|
|
|
what do you call "physical address" ? the base address of the file on the Hard disk ? or the UNC name ?
|
|
|
|
|
i need to send the file to NIC card thro DMA
for sending a data thro NIC it converts virtual address to physical address
so its better to dircetly supply ther physical address
help in this
-- modified at 5:34 Thursday 15th February, 2007
|
|
|
|
|
Give your account a name, then maybe you will get an answer.
|
|
|
|
|
Why you delete your post!?
|
|
|
|
|
sorry !!! no one responded to mee!!
i cant express my thing to U
wht shall i do?
|
|
|
|
|
arun kumar spk wrote: wht shall i do?
1) speak well (not in SMS)
2) be patient. waiting for 1/2 of an hour is not enough for a forum. you'r not on a chat room ; think of it.
3) be less selfish and more intelligent. how can one answer you if you delete the qsuestion ?
4) consider rethinking your question if you still got no answers after a long time. It may not be well formulated
|
|
|
|
|
ive re posted the question!!!
mailny i ve never give the name in my main message so i have given this again
SORRY
|
|
|
|
|
Are you sure all members see your post,No you must wating
|
|
|
|
|
sorry the last time i forgot to give my name!!!
soo its hard for u to recognizetht i only posted
so i deleted it
and i have reposted it again
soo pls respond to it!!!
thnks
|
|
|
|
|
|
thank you
but im doing it for windows sooo i think this malloc wont work in windows![Confused | :confused:](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_confused.gif)
|
|
|
|
|
How do you write console based applications(client/server) using CSocket class.
Mahesh
|
|
|
|
|
C Mahesh wrote: How do you write console based applications(client/server)
Why you do you need CSocket for that ? Isn't using winsock suffice ?
Look for articles on winsock in CP.
|
|
|
|
|
C Mahesh wrote: How do you write console based applications(client/server) using CSocket class.
The same way you would for a GUI application. Sockets don't care about the type of UI used.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I'm getting this error despite me having stdafx.h included in the source file where I use it, and inside stdafx.h I've added in
#include <iostream>
#include <fstream>
This is a class wizard based MFC dialog.
I've used this before and it worked fine, and can't spot what the difference is in terms of included headers etc.
|
|
|
|
|
I forgot to include
using namespace std;
#include "stdafx.h"
#include "TestFolderBrowse.h"
#include "TestFolderBrowseDlg.h"
using namespace std;
I thought that was just to resolve names though - how does resolving the namespace fix it? if anything I'd expect an error like "ifstream - can't work out which version of it you're using"
|
|
|
|
|
Because unless you have using namespace std, or using std::ifstream, it plain cannot see it, it remains undefined.
This file doesn't #include fstream, so the using statement here does not help.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Is it possible to pop up the dailog box from property grid cell?
If yes can any one give me a link how to do it.
|
|
|
|