LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cannot open ethernet device (https://www.linuxquestions.org/questions/programming-9/cannot-open-ethernet-device-406822/)

lucky6969b 01-23-2006 02:47 AM

cannot open ethernet device
 
I'm simulating a client/server environment
Here is the error "can't open ethernet device"
It is supposed to be the simpliest. How come it doesn't work?

#define ETHERNET_DEVICE "/dev/eth0"

void main()
{
// char *str = "hello LAN\n";
char *str;
char *filenametosave = "/root/hello.txt";
FILE *filetosave;

FILE *fp;
int bytes_read = 0;
fp = fopen (ETHERNET_DEVICE, "r");
if (fp == NULL)
{
fprintf (stderr, "can't open ethernet device\n");
exit (-1);
}


filetosave = fopen (filenametosave, "w");
if (filetosave == NULL)
{
fclose (fp);
fprintf (stderr, "can't open database\n");
exit(-1);
}
str = (char *)malloc (10000);

for (;bytes_read == 0;) {
bytes_read = fread (str, 10000, 1, fp);
printf ("Waiting for data...\n");
usleep(10000);
}
fwrite (str,1, strlen(str), filetosave);
fclose (fp);
fclose (filetosave);

}



Thanks
Jack

lucky6969b 01-23-2006 03:00 AM

Sorry about it. I did not find eth0 in that dir.
But there were several eth0 scattered around different places.
Which one should I use?

./proc/irq/10/eth0
./proc/sys/net/ipv6/conf/eth0
./proc/sys/net/ipv6/neigh/eth0
./proc/sys/net/ipv4/conf/eth0
./proc/sys/net/ipv4/neigh/eth0
./proc/net/dev/snmp6/eth0
./sys/class/net/eth0

pretty messy hey :)
Thanks
Jack

Wim Sturkenboom 01-23-2006 04:52 AM

I've never tried to open a connection like that. Network programming examples

FLLinux 01-23-2006 10:38 AM

Lucky, An ethernet device does not act like a standard I/O device. It does not use open and read. you have to use sockets. I would suggest getting Linux Socket Programming by example. It is a good book to learn the basic socket interface.

lucky6969b 01-24-2006 12:47 AM

But I learned from school that all devices on L/UNIX behave like files
I think "connect" ultimately would call open ("dev/eth0, ?)
Am I right or not?
Thanks
Jack

Wim Sturkenboom 01-24-2006 05:12 AM

Quote:

Originally Posted by lucky6969b
But I learned from school that all devices on L/UNIX behave like files

Once you have a socket open, it behaves the same as a filedescriptor. So you can use read and write (although there are different functions available).

I think that that's where the confusion comes from.


All times are GMT -5. The time now is 04:13 PM.