UNIX上编译C语言写的SOCKET程序时出错,该怎么解决
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_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;
}
------解决方案--------------------
这样改了当然就没问题了,不过这不是解决问题,而是在掩盖问题:)
------解决方案--------------------
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!
------解决方案--------------------
struct hostent *he
少了分号;
我在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;
}
------解决方案--------------------
这样改了当然就没问题了,不过这不是解决问题,而是在掩盖问题:)
------解决方案--------------------
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!
------解决方案--------------------
struct hostent *he
少了分号;