【求助】c 程序连接mysql编译成功但运行总是出现问题,着急ing
最近作毕业设计是有关抓包和事后分析的程序。。。现在抓包基本实现用的是libpcap,但存储功能总是一步一个坎。程序段如下:
。。。。。。。。
void DecodeTCP(u_int8_t * pkt, const u_int32_t len, Packet * p)
{
MYSQL mysql;
char query_str[1024];
char source_ip[1024];
char destination_ip[1024];
char sport[1024];
char dport[1024];
char num[1024];
struct pseudoheader /* pseudo header for TCP checksum calculations */
{
u_int32_t sip, dip; /* IP addr */
u_int8_t zero; /* checksum placeholder */
u_int8_t protocol; /* protocol number */
u_int16_t tcplen; /* tcp packet length */
};
u_int32_t hlen; /* TCP header length */
u_short csum; /* checksum */
struct pseudoheader ph; /* pseudo header declaration */
if(len < 20)
{
--pktnum;
p->tcph = NULL;
return;
}
p->tcph = (TCPHdr *) pkt;
hlen = TCP_OFFSET(p->tcph) << 2;
if(hlen < 20)
{
--pktnum;
p->tcph = NULL;
return;
}
if(hlen > len)
{
p->tcph=NULL;
--pktnum;
return;
}
ph.sip = (u_int32_t)(p->iph->ip_src.s_addr);
ph.dip = (u_int32_t)(p->iph->ip_dst.s_addr);
ph.zero = 0;
ph.protocol = p->iph->ip_proto;
ph.tcplen = htons((u_short)len);
p->sp = ntohs(p->tcph->th_sport);
p->dp = ntohs(p->tcph->th_dport);
/* if options are present, decode them */
p->tcp_options_len = hlen - 20;
p->data = (u_int8_t *) (pkt + hlen);
char *protocol_type="TCP";
sprintf(source_ip,"%s",inet_ntoa(p->iph->ip_src));
sprintf(destination_ip,"%s",inet_ntoa(p->iph->ip_dst));
sprintf(sport,"%d",p->sp);
sprintf(dport,"%d",p->dp);
printf("It is %s protocol\n",protocol_type);
printf("The source IP is %s\n",source_ip);
sprintf(num,"%ld",pktnum);
sprintf(query_str,"insert into tcp01(packetnumber,protocol_type,source_ip,sport,destination_ip,dport) values('%s','%s','%s','%s','%s','%s')",num,protocol_type,source_ip,sport,destination_ip,dport);
printf("%s\n",query_str);
mysql_init(&mysql);
if(!(mysql_real_connect(&mysql,"localhost","root","zhubing",NULL,3306,"/var/lib/mysql/mysql.sock",0)));
{
printf( "Error connecting to database: %s\n",mysql_error(&mysql));
mysql_close(&mysql);
return;
}
mysql_real_query(&mysql,query_str,strlen(query_str));
mysql_close(&mysql);
}
。。。。。。
现在的情况是这样的:confused: ,编译可以通过。但是在运行到mysql_real_connect的时候总是 会出现Error connecting to database... 联不上mysql服务器。不知为什么。希望好心人帮忙看看。现谢过了