日期:2014-05-16  浏览次数:20657 次

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 100
int 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_DA