這裏是我正在努力工作在紅帽子6上的功能..C#define導致Seg Fault?
而且我對C很少有經驗,特別是使用#define,所以我不確定這部分甚至試圖do:SP-> s_port = htons(SP-> s_port);
#ifdef __linux
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL) \
char GSBN_servbuf[HOSTBUFFERLENGTH] = {0}; \
struct servent GSBN_sp; \
struct servent *GSBN_serv_result; \
int GSBN_s = 0; \
GSBN_s = getservbyname_r(SERVICE, \
PROTOCOL, \
&GSBN_sp, \
GSBN_servbuf, \
sizeof(GSBN_servbuf), \
&GSBN_serv_result); \
SP = GSBN_serv_result; \
SP->s_port = htons(SP->s_port); \
if (SP && SOCKET_DEBUG) { \
printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \
get_timestamp(), SP->s_name, SP->s_port, SP->s_proto); \
} \
if (SP == NULL) { \
fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n", \
get_timestamp(), SERVICE); \
}
#else
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL) \
char GSBN_servbuf[HOSTBUFFERLENGTH] = {0}; \
struct servent GSBN_serv_result; \
SP = getservbyname_r(SERVICE, \
PROTOCOL, \
&GSBN_serv_result, \
GSBN_servbuf, \
sizeof(GSBN_servbuf)); \
if (SP && SOCKET_DEBUG) { \
printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \
get_timestamp(), SP->s_name, SP->s_port, SP->s_proto); \
} \
if (SP == NULL) { \
fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n", \
get_timestamp(), SERVICE); \
}
#endif
這是我收到的錯誤:
根據gdb的我在調用這個函數得到一個賽格故障:
GET_SERVICE_BY_NAME(SP,SERV,PROT);
下面是GDB輸出:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x456c6c90 (LWP 14645)]
0x420b1e69 in gi_get_port (serv=Unhandled dwarf expression opcode 0x9c
)
at /home/user1/Common/src/socket.c:282
282 GET_SERVICE_BY_NAME(sp, serv, prot);
Current language: auto; currently c
下面是該函數的調用:
int gi_get_port (char *serv, char *prot)
/* obtain the port for the named service */
{
int p, s;
/* Data for resolving service name to a socket description. */
struct servent *sp = NULL;
GET_SERVICE_BY_NAME(sp, serv, prot);
if (sp != NULL) {
p = sp->s_port;
} else {
p = -1;
};
return p;
}
什麼是SP類型'? –
htons =「主機到網絡短路」:將主機字節順序(英特爾上的LSB)的短整數轉換爲servent結構的網絡字節順序(MSB)。 – Rup
我認爲sp是struct servent *,因爲它是聲明爲 struct ser vent * sp = NULL; – systemoutprintln