首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

UNIX上编译C语言写的SOCKET程序时出错,该怎么解决

2012-02-27 
UNIX上编译C语言写的SOCKET程序时出错我在HP UNIX上编译C写的SOCKET程序时老是在opi_addr.sin_addr *((s

UNIX上编译C语言写的SOCKET程序时出错
我在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_addrip_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;
}

[解决办法]
这样改了当然就没问题了,不过这不是解决问题,而是在掩盖问题:)

[解决办法]
Notice that the definition of strut hostent like this:

structhostent {
char*h_name;/* official name of host */
char**h_aliases;/* alias list */
shorth_addrtype;/* host address type */
shorth_length;/* length of address */
char**h_addr_list;/* list of addresses from name server */
#defineh_addrh_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!
[解决办法]
struct hostent *he 

少了分号;

热点排行