首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Socket编程connection refused有关问题

2012-03-31 
Socket编程connection refused问题学习socket,自己试着编了一个C/S程序代码如下:C/C++ code/* * * client

Socket编程connection refused问题
学习socket,自己试着编了一个C/S程序
代码如下:

C/C++ code
/* * * client * * */#include<stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<netdb.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#define SERVPORT 1234#define MAXDATASIZE 100int main(int argc, char *argv[]) {    int sockfd, sendBytes;    char buf[MAXDATASIZE];    struct hostent *host;    struct sockaddr_in servAddr;    if(argc != 2) {        fprintf(stderr,"usage:./client [hostname]");        exit(1);    }    /*translate the address*/    if((host = gethostbyname(argv[1])) == NULL) {        perror("fail to get host by name");        exit(1);    }    printf("Success to get host by name...\n");    /*establish a socket*/    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {        perror("fail to establish a socket");        exit(1);    }    printf("Success to establish a socket...\n");       /*init sockaddr_in*/    servAddr.sin_family = AF_INET;    servAddr.sin_port = SERVPORT;    servAddr.sin_addr = *((struct in_addr *)host -> h_addr);    bzero(&(servAddr.sin_zero), 8);    /*connect the socket*/    if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr)) == -1) {        perror("fail to connect the socket");        exit(1);    }    printf("Success to connect the socket...\b");    /*send datas to server*/    if(sendBytes = send(sockfd, "hello", 5, 0) == -1) {        perror("fail to send datas");        exit(1);    }    printf("Success to send data!");    close(sockfd);}


C/C++ code
    /*     *     * server     *     * */    #include<stdio.h>    #include<stdlib.h>    #include<errno.h>    #include<string.h>    #include<netdb.h>    #include<unistd.h>    #include<sys/types.h>    #include<sys/types.h>    #include<sys/socket.h>    #include<netinet/in.h>    #define SERVPORT 1234    #define BACKLOG 20    #define MAX_CON_NO 10    #define MAX_DATA_SIZE 5    int main() {        struct sockaddr_in serverSockaddr, clientSockaddr;        int sinSize, recvBytes;        int sockfd, clientfd;        char buf[MAX_DATA_SIZE];        /*establish a socket*/        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {            perror("fail to establish a socket");            exit(1);        }        printf("Success to establish a socket...(sockfd = %d)\n", sockfd);        /*init sockaddr_in*/        serverSockaddr.sin_family = AF_INET;        serverSockaddr.sin_port = htons(SERVPORT);        serverSockaddr.sin_addr.s_addr = INADDR_ANY;        bzero(&(serverSockaddr.sin_zero), 8);        /*bind socket*/        if(bind(sockfd, (struct sockaddr *)&serverSockaddr, sizeof(struct sockaddr))== -1) {            perror("fail to bind");            exit(1);        }        printf("Success to bind the socket...\n");        /*listen on the socket*/        if(listen(sockfd, BACKLOG) == -1) {            perror("fail to listen");            exit(1);        }        printf("Success to listen on the socket...\n");        /*accept a client's request*/        if((clientfd = accept(sockfd, (struct sockaddr *)&clientSockaddr, &sinSize)) == -1) {            perror("fail to acceot");            exit(1);        }        printf("Success to accpet...\n");        /*call recv to receive datas*/        if((recvBytes = recv(sockfd, buf, MAX_DATA_SIZE, 0)) == -1) {                perror("fail to receive datas");                exit(1);        }        printf("Receive a connection:%s \n", buf);        close(sockfd);    } 



在UBUNTU防火墙关闭情况下终端1执行
Assembly code
    jimmy@MyPet:~$ gcc -o server server.c    jimmy@MyPet:~$ gcc -o client client.c    jimmy@MyPet:~$ ./server    Success to establish a socket...(sockfd = 3)    Success to bind the socket...    Success to listen on the socket...


终端2执行
Assembly code
    jimmy@MyPet:~$ ./client MyPet    Success to get host by name...    Success to establish a socket...    fail to connect the socket: Connection refused    jimmy@MyPet:~$ ./client 127.0.0.1    Success to get host by name...    Success to establish a socket...    fail to connect the socket: Connection refused    jimmy@MyPet:~$ ./client 127.0.1.1    Success to get host by name...    Success to establish a socket...    fail to connect the socket: Connection refused    jimmy@MyPet:~$ 


查看端口
Assembly code
    jimmy@MyPet:~$ nmap MyPet    Starting Nmap 5.21 ( http://nmap.org ) at 2012-02-19 14:35 CST    Nmap scan report for MyPet (127.0.1.1)    Host is up (0.00043s latency).    Not shown: 998 closed ports    PORT     STATE SERVICE    21/tcp   open  ftp    1234/tcp open  hotline    Nmap done: 1 IP address (1 host up) scanned in 0.19 seconds


请问众位大神这client端运行时的connection refused是怎么回事呢?服务器的问题吗?
但是根据端口情况应该是在进行监听的。

谢谢!

[解决办法]
客户端的PORT没做htons
[解决办法]
if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr)) == -1) {

第三个参数用sizoef(struct sockaddr_in)

热点排行