|

楼主 |
发表于 2004-4-6 14:37:49
|
显示全部楼层
这是我修改过的client
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <netdb.h>
- #include <unistd.h>
- #define BUFSIZE 8129
- int
- main(int argc, char **argv)
- {
- int sockfd;
- struct sockaddr_in cliaddr;
- char buf[BUFSIZE];
- struct hostent *h;
-
- // if (argc !=2){
- // printf(stderr, "usage: client hostname\n");
- // exit(1);
- // }
- if ((h=gethostbyname(argv[1]))==NULL)
- {
- perror("gethostbyname");
- exit(1);
- }
- if (sockfd=socket(AF_INET, SOCK_STREAM, 0)<0)
- {
- perror("socket error");
- exit(1);
- }
- bzero(&cliaddr, sizeof(cliaddr));
- cliaddr.sin_family = AF_INET;
- cliaddr.sin_port = htons(1234);
- // cliaddr.sin_addr = *((struct in_addr*)h->h_addr);
- // cliaddr.sin_addr.s_addr=((struct in_addr *)(h->h_addr))->s_addr;
- inet_pton(AF_INET, argv[1], &cliaddr.sin_addr);
-
- if(connect(sockfd,(struct sockaddr *) &cliaddr, sizeof(cliaddr))<0) {
- perror("connect");
- exit(1);
- }
- if (send(sockfd, buf, BUFSIZE, 0)<0)
- {
- perror("send");
- exit(1);
- }
- if (recv( sockfd, buf, BUFSIZE, 0)<0)
- {
- perror("recv");
- exit(1);
- }
- printf("%s\n", buf);
- close(sockfd);
- exit(0);
- }
复制代码
./client
segmentation fault
想破了头还是不明白,又要麻烦大家了! |
|