Networking :: Broadcast Packets Failed To Receive - Socket Beginner
Feb 24, 2011
I am working on uClinux/almost same as Linux. I am new to socket programming. I have two micro controllers running on same code. Simple run with arguments mean send merged string.
run 1st micro controller (Send): ./Name "anystring"
run 2st micro controller (Recive):/Name
My code is:
int receive() {
// Create socket
int sock_fd;
struct sockaddr_in addr;
char buffer[kBufferSize];
int bytes_received=0;
int addr_len = sizeof(addr);
printf("receive start ");
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if ( sock_fd == -1 ) {
printf("receive Create ");
// Error occurred return 0; }
printf("Res Create sucee ");
// Create address from which we want to receive, and bind it
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(kPortNumber);
if ( bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
printf("receive bind "); // Error occurred return 0; }
printf("Bind Succeful sucee ");
while ( 1 ) {
// Receive a message, waiting if there's nothing there yet
bytes_received = recvfrom(sock_fd, buffer, kBufferSize-0, 0,
(struct sockaddr*)&addr, &addr_len);
if ( bytes_received < 0 ) { // Error occurred
printf("receive bytes "); return 0; }
printf(" bytes_received succeful ");
printf("Ressocketstring : %s ",buffer);
memset(Ressocketstring,0x00 , sizeof(Ressocketstring));
strcpy(buffer,Ressocketstring); printf("Ressocketstring : %s
",Ressocketstring); printf(" ");
printf("Hello Receive finished");
// Now we have bytes_received bytes of data in buffer. Print it!
fwrite(buffer, sizeof(char), bytes_received, stdout); } }
int transmit(char * data, int length) {
int sock_fds[kMaxSockets];
// Obtain list of all network interfaces
/* struct ifaddrs *addrs;
if ( getifaddrs(&addrs) < 0 ) {
// Error occurred return 0; } */
// Loop through interfaces, selecting those AF_INET devices that support broadcast, but aren't loopback or point-to-point
struct sockaddr_in addr; int number_sockets = 0; struct hostent *he;
/* const struct ifaddrs *cursor = addrs;
while ( cursor != NULL && number_sockets < kMaxSockets ) {
if ( cursor->ifa_addr->sa_family == AF_INET
&& !(cursor->ifa_flags & IFF_LOOPBACK)
&& !(cursor->ifa_flags & IFF_POINTOPOINT)
&& (cursor->ifa_flags & IFF_BROADCAST) ) {
// Create socket*/
sock_fds[number_sockets] = socket(AF_INET, SOCK_DGRAM, 0);
if ( sock_fds[number_sockets] == -1 ) // Error occurred {
printf("Error Create "); return 0; }
he = gethostbyname((char *)BCASTADDRESS) ;
if (he==NULL ) {printf("Error gethostbyname ");
herror("gethostbyname"); printf("Error host "); exit(1);
} printf("Res He ");
// Create address from which we want to send, and bind it
memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET;
addr.sin_addr = *((struct in_addr *)he->h_addr);//((struct sockaddr_in *)cursor->ifa_addr)->sin_addr; addr.sin_port = htons(kPortNumber);
if ( bind(sock_fds[number_sockets], (struct sockaddr*)&addr, sizeof(addr)) < 0 )
{ // Error occurred printf("Error bind "); return 0; }
// Enable broadcast int flag = 1;
if ( setsockopt(sock_fds[number_sockets], SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)) != 0 ) // Err occur { printf("Error Enable "); return 0; }
number_sockets =1; printf("Succes 1 "); printf("transmit Create ");
// Initialise broadcast address memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_BROADCAST;
addr.sin_port = htons(kPortNumber);
// Send through each interface int i;
for ( i=0; i<number_sockets; i++ ) {
if ( sendto(sock_fds[i], data, length, 0, (struct sockaddr*)&addr, sizeof(addr)) < 0 )
{ printf("Error Send "); // Error occurred return 0;
} printf("Succes 2 "); } return 1; } void MergeMessage( ) {
memset(socketstring,0x00,sizeof(socketstring));
sprintf(socketstring,"%s@%s@%s@%s@%s@%s@%s@%s@%s@%s@%s
",Tmeg.s1,Tmeg.s2,Tmeg.s3,Tmeg.s4,Tmeg.s5,Tm eg.s6,Tmeg.s7,Tmeg.s8,Tmeg.s9,Tmeg.s10,Tmeg.s11);
printf(" MergeSocketMessage : %s ",socketstring); }
int main (int argc, char** argv) {
int fd=0,bdc=0; struct ifreq ifr; printf("Tsarting man ");
fd = socket(AF_INET, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr); close(fd);
memset(MYIP,'-',sizeof(MYIP));
memset(BCASTADDRESS,'-',sizeof(BCASTADDRESS));
strcpy(MYIP, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
printf("Etho: %s ,len:%d ", MYIP,strlen(MYIP));
for(bdc=strlen(MYIP);MYIP[bdc-1]!='.';bdc--) {}
strncpy(BCASTADDRESS,MYIP,bdc-1); strcat(BCASTADDRESS,".255");
printf("BCASTADDRESS: %s ,len:%d
", BCASTADDRESS,strlen(BCASTADDRESS));
printf("Initial Broad Cast message "); {
/*s0"0" (id=76)
s1"500" (id=77)s2"100" (id=78)s3"100" (id=78)s4"startVD" (id=79)
s5"lighting" (id=80)s6"reading" (id=81)s7"Anna" (id=82)s8"0" (id=76)
s9"";s10"" (id=64)s11"" (id=64)*/
strcpy(Tmeg.s0,"0"); strcpy(Tmeg.s1,"500");strcpy(Tmeg.s2,"100");strcpy(Tmeg.s3,"100");
strcpy(Tmeg.s4,"startvd");strcpy(Tmeg.s5,"lighting");strcpy(Tmeg.s6,"reading");
strcpy(Tmeg.s7,"anna");strcpy(Tmeg.s8,"0"); }
MergeMessage (Tmeg); if( strlen(argv[1]) ) {
//strcpy(socketstring,Tmeg,sizeof(Tmeg));
if(transmit(socketstring, strlen(socketstring) ) ) {
printf(""%s" transmitted. ", socketstring); } else {
printf("Error occurred: %s ", strerror(errno)); return 1; } }
else { for( ; ; ) {
if ( argc < 2 ) // No argument: Just listen {
printf("Listening... "); if ( !receive() ) {
printf("Error occurred: %s ", strerror(errno)); return 1;
} return 0; } } } printf("Finished "); return 0;
} //////////////// .h file is /////////////
//#define WIN32_LEAN_AND_MEAN// Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#define SubLen50
//#include <ifaddrs.h>
#include <errno.h>
char MYIP[20];
char BCASTADDRESS[20];
typedef struct {
char s0[SubLen];
char s1[SubLen];
char s2[SubLen];
char s3[SubLen];
char s4[SubLen];
char s5[SubLen];
char s6[SubLen];
char s7[SubLen];
char s8[SubLen];
char s9[SubLen];
char s10[SubLen];
char s11[SubLen];
}msg_struct;
msg_struct Sendmsg;
msg_struct Tmeg; //Recivemsg;
Boolean pri[6];
char socketstring[SubLen*12];
char Ressocketstring[SubLen*12];
int kBufferSize = (SubLen*12);
I've got an application that compiles in Windows and Linux and is using UDP multicast to communicate. I'm running it on a Windows machine, a Linux machine and a small board (Gumstix) that is running Linux as well. The Windows and Linux machines have no problems sending/receiving packets with each other. The Gumstix and Linux machines have no problems sending/receiving packets with each other. The Gumstix can also send/receive packets with two instances of the application both running on the Gumstix.
But when running on the Gumstix I can send udp multicast packets to Windows, but cannot receive them. (It works if I do point to point with a known IP port#, but not multicast.) I can run tcpdump on the Gumstix and see the packets, but they are not being received on my socket. Here's what tcpdump (running on the Gumstix) outputs when sending the same packet from different sources. Notice the IP header ID and flags (don't fragment) are different when coming from Windows.
WINDOWS to Gumstix -7:-45:-2.015784 IP (tos 0x0, ttl 15, id 35331, offset 0, flags [none], proto: UDP (17), length: 444) 172.30.42.3.1165 > 239.255.183.180.47028: UDP, length 416
LINUX to GUMSTIX -7:-43:-38.451991 IP (tos 0x0, ttl 15, id 0, offset 0, flags [DF], proto: UDP (17), length: 444) 172.30.42.13.32771 > 239.255.183.180.47028: UDP, length 416
GUMSTIX to GUMSTIX -7:-33:-10.955608 IP (tos 0x0, ttl 15, id 0, offset 0, flags [DF], proto: UDP (17), length: 444) 172.30.42.110.1025 > 239.255.183.180.47028: UDP, length 416
I've tried everything I can think of to get this to work but am not getting anywhere. Also I'm pretty inexperienced when it comes to Linux. Anything I could try to be able to receive udp multicast packets from Windows?
Assume this: Machine A sends a packet to machine B, no application in machine B is waiting for the packet, Now: What happens in kernel? What happens to this packet exactly?
I need to receive a number of raw ethernet packets (say, 100 packets) into a user-mode accessible buffer large enough to hold all the packets. The way I have done this so far is by looping over the recvfrom() system call 100 times, passing an incremented pointer addressing the location in my buffer to store the packet. Is there a way to receive the 100 packets into my buffer with one system call, perhaps by instructing the kernel to DMA the 100 packets into my buffer?
I am trying to do something outlandish with iptables (or so I think!).I have a source sending udp packets to a destination (say dst11). Using port mirroring I am able to get all these packets to a different machine (say dst22). I am able to see these packets on dst22 interface using tcpdump.I want to analyze the packets on dst22. So what I do is put dst22 interface in promiscuous mode (using ifconfig eth0 promisc). This in theory should get the packet through the MAC layer. Now using iptables I am trying to DNAT the packets in nat prerouting to change the packets destination IP to dst22's interface and change the destination port.
I'm having problems with my internet connection; it seems to be working fine then every other click of a page there is no connection, then I click it again a second later and it works. I am using a wired network connection plugged into an addon wireless router.
When I use ping under network tools it seems to be fine, then freezes halfway through and doesn't receive those few packets, giving me a transmission percentage of about 70%.
When # shutdown +5 is run all the terminals (including pseudo terminals) are sent broadcast messages saying system is about to go down. Is it possible for user to receive this in the form of OSD or system tray notification so that he will be informed even if he is not running any terminal emulator or running it but minimized it and working with something else?
I am trying to build a socket to retrieve the ethernet packets from ECU(I do not know much about the ECU). When i run my code on windows there is no problem and the code runs correctly. But when i run my code on Debian it gets stuck at s.recv(1024).
I have already set static ip in /etc/network/interfaces as follows:
Code: Select all import socket import sys HOST = "160.48.199.91" port = 30490 s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP) s.bind((HOST, 30490)) while True: data = s.recvfrom(1024) print(data)
There is no LAN or Router. When i check netstat -s i see that there are 0 UDP and 0 TCP messages received. But when i check in Wireshark on Debian i could see the displayed UDP packets. Does it mean that the UDP packets are reaching the Raspberry Pi but not received by Debian ? Or are these packets being dropped?
(Let me first of all state that I am a newbie to any form of programming.) I have been trying to create an IP header + TCP header and send this to another machine on my network.using C)I used the normal stuff: two structures for the headers, a sockaddr_in structure, call to function socket with SOCK_RAW, setsockopt with HDRINCL and call to sendto.All functions seem to return fine (values other then -1) the function that I have used to calculate the checksum for the IPheader matches the value that I manually calculated. I just don?t see the anything coming out of the interface on whireshark.I assumed that it had something do with my piece of code so I used two examples (including mixter void ru rawip html A brief programming tutorial in C for raw sockets[/url]). They show exactly the same thing functions return fine but no packets being send.I use Ubuntu 9.04 2.6.28-14-genericThe machine has two interfaces one with an ip address the other interface is in promiscuous mode. (both interfaces connected to a switch with port mirroring) I can see all normal traffic in/out.
I'm trying implement a small server for a university work, my server is creating, binding and listening but it don't accept any connection, these are the files I'm using in server:
Code:
// Definition of the Socket class #ifndef SOCKET_H #define SOCKET_H #include <sys/types.h> code....
I've made a client just for test it, but no ones run well, the server is always returning -1 when accept is called!!
What should I do to send and receive messages in this server??
It's been awhile since I posted anything which is a good sign my install has been working well and I have been able to handle most everything. However, I'm not able to handle this issue. I recently installed F11 and everything went well. But, when trying to see my other computers on the local network, I cannot. I receive this error message: Unable to mount location Failed to receive shared list from server. I understand the message as it is obvious, but do not know how to fix it.
i have a linux server runnig oracle applications. i need to access this server from putty using ssh through internet. i did by registering my static ip with the dnydns.org and i am able to connect to the server. but now there is no security to authenticate any user as any one knowing the password can login to it.
i thought of configuring the firewall of linux server but the client ip`s are not static and they change continiously. so thought of keeping one more pc between the server and the router which will do the work of authenticating. but i am confuse as how to configure it to allow the packets coming from the internet after authenticating and to by pass the packets generated from internal LAN?
I want to configure socket timer to release socket(port) once the connection is terminated. Do we have something in Linux OS to configure this delay to release socket?.
Any command, link or man-page anything will be helpful.
Linux 2.6.27.5-117.fc10.i686.PAE #1 SMP Tue Nov 18 12:08:10 EST 2008 i686 i686 i386 GNU/Linux
I need to enable telnet service on it, then found xinetd and telnet-server not installed,
Then I had installed below First I had installed Xinetd: xinetd-2.3.14-21.fc10.i386.rpm then I had installed telnet-server: xinetd-2.3.14-21.fc10.i386.rpm
After installation I executed below commands
Service xinetd start Chkconfig xinetd on Chkconfig telnet on Service iptables stop Chkconfig iptables off Iptables �F
After I had tried telnet localhost ----IT FAILED
when I run telnet server manually by issuing the below command /usr/sbin/in.telnetd, I get below error [COLOR="Red"]/usr/sbin/in.telnetd: getpeername: Socket operation on non-socket[/COLOR]
I have other system running same OS, I had followed the same steps discussed above to enable telnet, It works fine on it.
Friends i have an idea to broadcast few local TV channels to the world via internet.But friends i can't directly broadcast from my country because of bandwidth.friends is there a way to do this through a remote server server? i just need to input my stream to a remote server and then broadcast it from the server.I can stream channels to the server from my country through a 10mbbs connection.
I am pragmatically trying to upload a list of files from my client machine to a proFTPd server I have running on Ubuntu. Every time I get several (around fifty) files into the transfer, I get the following error: Quote: Connection reset by peer: socket write error
I am able to remotely upload 25,000 small files of ~ 1K in size, but when I go to upload hundreds of 2-4 Mb files, I get a socket write error. Am I using up all available sockets before they can be released? If so, how do I release the sockets? If not, what does this error mean and why am I getting it?
I've been getting the following for well over 2 months now and this is really really irritating: Code: Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubuntu.com --recv 1781BD45C4C3275A34BB6AEC6E871C4A881574DE gpg: requesting key 881574DE from hkp server keyserver.ubuntu.com gpg: keyserver timed out gpg: keyserver receive failed: keyserver error For any bloody key, it times out?
I have a new install of Ubuntu 10.04.1 LTS server. But I get errormessages trying to start samba.
caesar@klem:/etc/samba$ restart smbd restart: Unable to connect to system bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
I have tride to remove an install samba. And deleted smb.conf. And startet with the original.
smb.conf: workgroup = Easyways server string = %h server (Samba, Ubuntu) log file = /var/log/samba/log.%m max log size = 1000
I can't seem to get on my Twinkle SIP Phone, it says: "Failed to create a UDP Socket (SIP) on port 5060 Address already in use" after that message it brings me back to the window were I'm supposed to run my profile, how do I fix this?
I have a hardware device with two ethernet ports, eth0 and eth1 running Centos 5. Basically my goal is to forward packets from eth0->eth1 and eth1->eth0 as well as get a copy of these packets for analysis. If I set IP routing to do the forwarding then I won't get a copy of the packets for analysis.
I finally got the certs to configure: openvpn --config server.conf Tue May 3 17:26:27 2011 OpenVPN 2.1.1 i686-redhat-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11] built on Jan 5 2010 Tue May 3 17:26:27 2011 NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables Tue May 3 17:26:27 2011 Diffie-Hellman initialized with 1024 bit key Tue May 3 17:26:27 2011 TLS-Auth MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ] Tue May 3 17:26:27 2011 ROUTE default_gateway=192.168.122.1 Tue May 3 17:26:27 2011 TUN/TAP device tun0 opened Tue May 3 17:26:27 2011 TUN/TAP TX queue length set to 100 Tue May 3 17:26:27 2011 /sbin/ip link set dev tun0 up mtu 1500 Tue May 3 17:26:27 2011 /sbin/ip addr add dev tun0 local 10.8.0.1 peer 10.8.0.2 Tue May 3 17:26:27 2011 /sbin/ip route add 10.8.0.0/24 via 10.8.0.2 Tue May 3 17:26:27 2011 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:0 EL:0 AF:3/1 ] Tue May 3 17:26:27 2011 Socket Buffers: R=[114688->131072] S=[114688->131072] Tue May 3 17:26:27 2011 UDPv4 link local (bound): [undef]:1194 Tue May 3 17:26:27 2011 UDPv4 link remote: [undef] Tue May 3 17:26:27 2011 MULTI: multi_init called, r=256 v=256 Tue May 3 17:26:27 2011 IFCONFIG POOL: base=10.8.0.4 size=62 Tue May 3 17:26:27 2011 IFCONFIG POOL LIST Tue May 3 17:26:27 2011 Initialization Sequence Completed
But openvpn still won't start; where to go from here. Tue May 3 17:54:25 2011 TCP/UDP: Socket bind failed on local address 192.168.122.3:1194: Address already in use Tue May 3 17:54:25 2011 Exiting
all I get is the broadcast address can be used to send packets of information to all computers on a network simultaneously. Can that be used during MPI programming or anything of the such? What is the day to day use of the broadcast address?
This is not a linux specific question more of a general network issue with the hope that someone may have already done this under linux.My problem:I have a red hat linux machine transmitting IP multicast packets onto an ethernet gigabit network (cisco switch).Wireshark (running on a different red hat linux box confirms packets on network.
I need to configure network. I have configured a labtop "1" as a wireless Access point. And connected 2 laptops. I want to broadcast from a labtop "2" a video and read it on labtop 3. This broadcast will be done first time using VLC server then using apache server.