cancel
Showing results for 
Search instead for 
Did you mean: 

why winsock cannot get my UDP packet ?

mehmet.karakaya
Associate III
Posted on December 28, 2012 at 15:01

#include ''stm32f4xx_conf.h'' 
#include ''ethernet.h'' 
#include <stdio.h> 
#include <string.h> 
//source IP 
#define S_IP1 192 
#define S_IP2 168 
#define S_IP3 1 
#define S_IP4 100 
//dest IP 
#define D_IP1 192 
#define D_IP2 168 
#define D_IP3 1 
#define D_IP4 2 
struct
ip_eth_addr { 
uint8_t addr[4]; 
}; 
struct
mac_addr { 
uint8_t addr[6]; 
}; 
struct
ip_eth_addr s_ip = { {S_IP1, S_IP2, S_IP3, S_IP4}}; 
struct
ip_eth_addr d_ip = { {D_IP1, D_IP2, D_IP3, D_IP4}}; 
struct
mac_addr mymac = { {0x1c, 0x9a, 0x65, 0x4a, 0xd9, 0x6c} } ; 
struct
mac_addr destmac = { {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} } ; 
char
ipstring [20]; 
void
* first_data_byte; 
//define ethernet II header 
struct
eth_header 
{ 
//ether frame 
uint8_t ether_dest[6]; 
//destination mac address 
uint8_t ether_src[6]; 
//source mac address 
uint16_t ether_type; 
//ether type 
//ip frame 
uint8_t vhl;
//version / IHL 
uint8_t tos;
//type of service 
uint16_t len;
//len 
uint16_t ipid; 
uint16_t ipoffset; 
uint8_t ttl; 
uint8_t proto; 
uint16_t ipchksum; 
uint16_t srcipaddr[2]; 
//26 
uint16_t destipaddr[2]; 
//udp 
uint16_t srcport; 
uint16_t destport; 
uint16_t udplen; 
uint16_t udpchksum; 
}; 
//struct eth_header frame; 
#define frame ((struct eth_header*)&uip_buf[0]) 
unsigned 
char
frame_counter; 
unsigned 
int
test_counter; 
char
test1[] = 
''TEST''
; 
unsigned 
int
data_len = 64; 
//udp data len 
void
send_UDP_frame(
void
) 
{ 
frame_counter++; 
uip_len = 
sizeof
(
struct
eth_header); 
//get struct header len = 42 
memcpy
(frame->ether_dest, &destmac, 6); 
//0xff = broadcast 
memcpy
(frame->ether_src, &mymac, 6); 
frame->ether_type = HTONS(0x0800); 
//IP following 
frame->vhl = 0x45; 
//IP4, 20byte header 
frame->tos = 0x00; 
// 
frame->len = HTONS(28 + data_len); 
frame->ipid = HTONS(frame_counter);
// 
frame->ipoffset = HTONS(0); 
frame->ttl = 0x80; 
//time to live 
frame->proto = 0x11; 
//UDP (17) 
frame->ipchksum = 0; 
//reset checksum (will be calculated and inserted later) 
memcpy
(frame->srcipaddr, &s_ip,4); 
//insert source ip 
memcpy
(frame->destipaddr,&d_ip,4); 
//insert dest ip 
//calculate IP4 header checksum 
frame->ipchksum=~uip_chksum((uint16_t*)&uip_buf[14],20); 
//20 byte header checksum 
frame->srcport = HTONS(1024); 
//source port 
frame->destport = HTONS(1023); 
//destination port 
frame->udplen = HTONS(data_len+8); 
//data len + 8 byte udp header 
tapdev_send(uip_buf,uip_len+data_len); 
} 

hello forum, I am trying to send an empty UDP packet to visual basic 6.0 winsock as you can see in attached picture wireshark ''sees'' theUDP packets howeverwinsock never go into below subroutine ( it doesnot getthe packets) what is the missing thinghere? thank you

Private
Sub
Winsock1_DataArrival(
ByVal
bytesTotal 
As
Long
) 
Text1 = bytesTotal 
Dim
strData 
As
String
Winsock1.GetData strData, vbString 
End
Sub

0690X00000602jIQAQ.jpg #udp-winsock
5 REPLIES 5
Posted on December 28, 2012 at 16:44

Not familiar with how it works in VB, but building an app using WinSock you need to register the application with the Windows Firewall for it to work as expected.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dandan9
Associate II
Posted on December 28, 2012 at 19:31

The easiest way to debug your VB program is to create a second socket in VB and send data to yourself. You can either use your own IP or the loopback address 127.0.0.1; whether it will actually make it out on the wire or not depends on the stack in Windows (though I wouldn't count on it). Either way you can test your construction of listening on port 1023 and the data receive function without even leaving your PC.

Dan

mehmet.karakaya
Associate III
Posted on April 22, 2013 at 14:00

hello forum ,

I succeeded to send ''TEST'' to PC from my STM32F4

however only with a cross cable ( directly from STM to PC )

why cant I send when I conect STM to adsl router ?

has anybody a working UDP code for STM32F4

thank you

Posted on April 22, 2013 at 15:23

''why cant I send when I conect STM to adsl router?''

Probably because your rouer configuration does not allow it?

This might be useful:

http://www.simplecomtools.com/productcart/pc/viewPrd.asp?idproduct=6&idcategory=5

You should start by seeing if you can successfully send & receive UDP packets between 1 PCs on your network...
Posted on April 22, 2013 at 16:43

You'd want to confirm the IP/MAC addresses involved, if you have the IP for a Gateway and DNS.

I'd generally recommend the use of standard cables, and a dumb hub (ie not a switch), this reduces complication, and permits you to ''T'' connect into an active network and observe traffic.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..