LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 714|回复: 7

编译提示问题?

[复制链接]
发表于 2005-5-16 10:46:48 | 显示全部楼层 |阅读模式
编译程序的时候总是提示:two or more data type in delaration of 'main',
parse error before "sock"
是什么意思  
其中第一条总是针对第一个声明的函数。但是我检查多次,都找不到问题在哪里。
发表于 2005-5-16 12:33:16 | 显示全部楼层
如果你愿意把程序贴出来,别人可以帮你检查。
回复 支持 反对

使用道具 举报

发表于 2005-5-16 13:30:13 | 显示全部楼层
int ret;
char ret;
可能是这个错误吧。
去掉一个重复的定义。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-16 16:50:50 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <sys/socket.h>

  3. #include <sys/ioctl.h>
  4. #include <net/if.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <unistd.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/tcp.h>
  10. #define INTERFACE "eth0"

  11. struct ipp{
  12. unsigned int ip_length:4;
  13. unsigned int ip_version:4;
  14. unsigned short ip_total_length;
  15. unsigned short ip_id;
  16. unsigned short ip_flags;
  17. unsigned char ttl;
  18. unsigned char ip_protocol;
  19. unsigned short ip_cksun;
  20. unsigned int ip_sourse;
  21. unsigned int ip_dest;
  22. }
  23. struct tcp{
  24. unsigned short        tcp_sourse_port;
  25. unsigned short        tcp_dest_port;
  26. unsigned int        tcp_seqno;
  27. unsigned int        tcp_ackno;
  28. unsigned int        tcp_resl:4,
  29.                 tcp_hlen:4,
  30.                 tcp_fin:1,
  31.                 tcp_syn:1,
  32.                 tcp_rst:1,
  33.                 tcp_psh:1,
  34.                 tcp_ack:1,
  35.                 tcp_urg:1,
  36.                 tcp_res2:2;
  37. unsigned short        tcp_winsize;
  38. unsigned short        tcp_cksum;
  39. unsigned short        tcp_urgent;
  40. }

  41. int Moshi(char * interface,int sock);

  42. int main()
  43. {
  44. int sock,byte_recieved,fromlen;
  45. char buff[65535];
  46. struct sockaddr_in from;
  47. struct ipp *ip;
  48. struct tcp *tcp;

  49. sock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
  50. if sock<0 exit(0);





  51. Moshi(INTERFACE,sock);
  52. while(1)
  53. {
  54.         fromlen=sizeof from;
  55.         byte_recieved=recvfrom(sock,buff,sizeof buff,0,(struct sockaddr *)&from,&fromlen);
  56.         printf("\nReceived bytes:::%5d\n",byte_recieved);
  57.         printf("Message from:::%s\n",inet_ntoa(from.sin_addr));
  58.         ip=(struct ipp *)buff;
  59.         if (ip->ip_protocol==6){
  60.                 tcp=(struct tcp *)(buff+4*(ip->ip_length));
  61.                 printf("From port:::%u\n",tcp->tcp_sourse_port);
  62.                 printf("End port:::%d\n",tcp->tcp_dest_port);
  63.         }
  64. }
  65. }

  66. int Moshi(char * interface,int sock)
  67. {
  68.         struct ifreq ifr;
  69.         strncpy(ifr.ifr_name,interface,(strlen(interface)+1));
  70.         if(ioctl(sock,SIOCGIFFLAGS,&ifr)==-1){
  71.                 exit(0);
  72.                 }
  73.         ifr.ifr_flags|=IFF_PROMISC;
  74.         if(ioctl(sock,SIOCSIFFLAGS,&ifr)==-1){
  75.                 exit(0);
  76.                 }
  77.         printf("\nZhunbei jiuxu!\n");
  78.         return(0);
  79. }

复制代码
回复 支持 反对

使用道具 举报

发表于 2005-5-16 17:31:55 | 显示全部楼层
定义结构体时出错了,应改为:
struct ip
{
/* ... */
};
最后要加上分号。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-16 17:42:27 | 显示全部楼层
哦 谢谢 原来是低级错误 不好意思。
不过这样只解决了一个问题  还是会提示说:parse error before "sock"
回复 支持 反对

使用道具 举报

发表于 2005-5-16 17:58:41 | 显示全部楼层
if sock<0

这是C语言吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-16 19:46:50 | 显示全部楼层
汗。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表