UNIX上编译C语言写的SOCKET程序时出错

bwpic 2007-10-05 11:02:56
我在HP UNIX上编译C写的SOCKET程序时老是在opi_addr.sin_addr = *((struct in_addr*)he->h_addr);这一行提示变量类型不匹配.但我是按教程上来写的,不知道错在哪里。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <stdarg.h>
#include <malloc.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>

#define FAIL -1
#define SUCCEED 1
#define MYPORT 4950
#define h_addr ip_addr[20+1]

int main(int argc, char *argv[])
{
int* JUDGE_FLAG;
char* lot_id;
char IP_ADDRESS[10+1][20+1];
char ip_addr[20+1];

int sockfd;
struct sockaddr_in opi_addr;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct hostent *he

strcpy( ip_addr, IP_ADDRESS[a]);
opi_addr.sin_family = AF_INET;
opi_addr.sin_port = htons(MYPORT);
opi_addr.sin_addr = *((struct in_addr*)he->h_addr);
sendto(sockfd, lot_id, strlen(lot_id), 0, (struct sockaddr *)&opi_addr, sizeof(struct sockaddr));
sendto(sockfd, JUDGE_FLAG, 1, 0, (struct sockaddr *)&opi_addr, sizeof(struct sockaddr));

close(sockfd);
return SUCCEED;
}
...全文
125 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmggic 2007-10-08
  • 打赏
  • 举报
回复
怎么这么多头文件我没见过啊,我说我怎么不知道怎么用C做东西,到哪能知道这些东西啊
学校里就教了个C的基础
bwpic 2007-10-07
  • 打赏
  • 举报
回复
晕,楼上的,谢谢了!明天再到公司试试
lina520 2007-10-06
  • 打赏
  • 举报
回复
struct hostent *he

少了分号;
bwpic 2007-10-06
  • 打赏
  • 举报
回复
3楼的兄弟,我这个程序是要往两个固定的IP地址发送数据包的,所以不能设为ANY
5楼的兄弟,能说一下怎样初始化"*he"吗?
另外,"- >"当中有个空格的,不知道是否应该无空格.
谢谢!
arong1234 2007-10-05
  • 打赏
  • 举报
回复
不匹配的错误具体是啥,一般会指出两者类型到底有啥差别的

尽信书则不如无书,教程上的东西算不得数
avalonBBS 2007-10-05
  • 打赏
  • 举报
回复
我没编译到那就出错了
boxban 2007-10-05
  • 打赏
  • 举报
回复
Notice that the definition of strut hostent like this:

struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
short h_addrtype; /* host address type */
short h_length; /* length of address */
char **h_addr_list; /* list of addresses from name server */
#define h_addr h_addr_list[0] /* address, for backward compatiblity */
};

Try:
opi_addr.sin_addr = *((struct in_addr*)(he->h_addr));

Another thing I have to mention is that "*he" is NOT initialized!
arong1234 2007-10-05
  • 打赏
  • 举报
回复
这样改了当然就没问题了,不过这不是解决问题,而是在掩盖问题:)
StarRib 2007-10-05
  • 打赏
  • 举报
回复
opi_addr.sin_addr = *((struct in_addr*)he- >h_addr)
改为:
opi_addr.sin_addr.s_addr=INADDR_ANY

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧