工作上遇到getpeername的問題.
每當我accept一個連線後,使用getpeername都沒有問題,但問題是接下來recv後,再去使用
getpeername所抓出來的ip都不一樣.
看了國外的論壇,似乎也有人也遇到相同的問題.其解決的方法如下
------------------------------------------------------------------------------------------------
struct sockaddr_in peer;
int peer_len;
peer_len = sizeof(peer); //<---要加入這一行才可以
getpeername(ClientFD,(struct sockaddr *)&peer,&peer_len);
------------------------------------------------------------------------------------------------
這樣問題就可以解決了~~
Wednesday, April 21, 2010
Monday, December 21, 2009
compiler的問題
最近有遇到compiler的問題
廠商有提供幾個library給我們使用,libcommon.a
而我自己寫了幾個測試程式要來測試這個library能不能使用。
當我寫完以後要compiler成執行檔結果就發生底下的問題
#gcc -O3 -I../Include -c Testlib.c
#gcc -L../lib -lcommon Testlib.o -o Testlib
Testlib.o: In function `main':
Testlib.o(.text+0x44): undefined reference to `Lib_Init'
Testlib.o(.text+0x7c): undefined reference to `Lib_Write'
Testlib.o(.text+0x98): undefined reference to `Lib_UnInit'
collect2: ld returned 1 exit status
疑~~~奇怪了,明明Lib_Init,Lib_Write和Lib_Uninit都在libcommon.a裡面啊??
上網找尋gcc的使用手冊
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.
喔~~真相大白,原來gcc使用library也有順序可言喔!!
只要將
gcc -L../lib -lcommon Testlib.o -o Testlib 改成
gcc Testlib.o -L../lib -lcommon -o Testlib
這樣就可以了。
就以我這個例子來說,Testlib.o裡面有參考到libcommon.a裡面所提供的function,所以要放在libcommon.a之前,這樣gcc就會把undefine的symbol先找出來再往後找尋library。
廠商有提供幾個library給我們使用,libcommon.a
而我自己寫了幾個測試程式要來測試這個library能不能使用。
當我寫完以後要compiler成執行檔結果就發生底下的問題
#gcc -O3 -I../Include -c Testlib.c
#gcc -L../lib -lcommon Testlib.o -o Testlib
Testlib.o: In function `main':
Testlib.o(.text+0x44): undefined reference to `Lib_Init'
Testlib.o(.text+0x7c): undefined reference to `Lib_Write'
Testlib.o(.text+0x98): undefined reference to `Lib_UnInit'
collect2: ld returned 1 exit status
疑~~~奇怪了,明明Lib_Init,Lib_Write和Lib_Uninit都在libcommon.a裡面啊??
上網找尋gcc的使用手冊
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.
喔~~真相大白,原來gcc使用library也有順序可言喔!!
只要將
gcc -L../lib -lcommon Testlib.o -o Testlib 改成
gcc Testlib.o -L../lib -lcommon -o Testlib
這樣就可以了。
就以我這個例子來說,Testlib.o裡面有參考到libcommon.a裡面所提供的function,所以要放在libcommon.a之前,這樣gcc就會把undefine的symbol先找出來再往後找尋library。
Saturday, December 5, 2009
Google 開放DNS 的查詢
看到網路新聞,Google開放它們自己的DNS Server給全世界。
以前在台灣網路剛盛起,台灣只有一個DNS Server可供查詢,那就是中華電信
的DNS Server 168.95.1.1,現在又多出一個選擇
8.8.8.8
8.8.4.4
以上是Google開放的DNS,上面的IP選的真的很好,也很好記。
以前在台灣網路剛盛起,台灣只有一個DNS Server可供查詢,那就是中華電信
的DNS Server 168.95.1.1,現在又多出一個選擇
8.8.8.8
8.8.4.4
以上是Google開放的DNS,上面的IP選的真的很好,也很好記。
Monday, November 2, 2009
如何從Linux系統抓MAC Address
在Linux 平台上面,可以使用ioctl的方式來得知網路的的MAC Address.
-----------------------
Ref http://www.geekpage.jp/en/programming/linux-network/get-macaddr.php
-----------------------
#include
#include /* for strncpy */
#include
#include
#include
#include
#include
int
main()
{
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
/* display result */
printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
return 0;
}
Ref http://www.geekpage.jp/en/programming/linux-network/get-macaddr.php
Tuesday, June 23, 2009
Subscribe to:
Posts (Atom)