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

C语言网络编程遇到问题了,求高手帮助!
小弟是初学者,深知让前辈帮忙看代码是一件极其愚蠢,极其没良心的事,不过这小段代码小弟是反复琢磨了几万遍有了,还是找不出哪里出问题了阿。麻烦高手帮忙看看,代码也不长,是简单的客户端请求连接服务端,然后发送了 一个字符串过去,可是老连接失败啊,拜托拜托。。。。
服务端:
C/C++ code
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define portnumber 3333

void main()
{
    struct sockaddr_in serve_sock ;
    struct sockaddr_in client_sock;
    int sockfd;
    int new_sockfd;
    char *s;

    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
    {
        printf("creat sock fail !\n");
        exit(1);
    }
    /*填写 sockaddr_in 的成员数据*/
    bzero(&serve_sock,sizeof(struct sockaddr_in));   /*清 0 */
    serve_sock.sin_family = AF_INET;                 /*设置协议为 IPV4*/
    serve_sock.sin_port = htons(portnumber);         /*设置端口号为 3333*/
    serve_sock.sin_addr.s_addr = htonl(INADDR_ANY);  /*设置IP为任意IP地址*/
    if(bind(sockfd,(struct sockaddr *)(&serve_sock),sizeof(struct sockaddr)) == -1)  /*绑定*/
    {
        printf("bind sock fail!\n");
        exit(1);
    }
    if(listen(sockfd,5) == -1) /*设置最大连接数为5*/
    {
        printf("listen sock fail!\n");
        exit(1);
    }
    while(1)
    {
        if((new_sockfd = accept(sockfd,(struct sockaddr *)(&client_sock),sizeof(struct sockaddr_in))) == -1)  /*堵塞,等待连接请求*/
        {
            printf("accept fail!\n");
            exit(1);
        }
        printf("connect from the client process successfully!\n");
        read(new_sockfd,s,1024);  /*读取客户端程序发送来的字符串*/
        printf("read the string %s\n",s);   /*将字符串打印出来*/
        close(sockfd);            /*关闭本次连接*/
    }
}





客户端:
C/C++ code
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define portnumber 3333

void  main(int argc,char *argv[])
{
    struct sockaddr_in client_sock;
    struct hostent *host;
    int fd;
    char *s;

    if((host=gethostbyname(argv[1]))==NULL)  /*通过命令行参数传进来的ip地址获取 hostent 变量*/
     {
          fprintf(stderr,"Gethostname error\n");
          exit(1);
     }
    if(fd = socket(AF_INET,SOCK_STREAM,0) == -1)
    {
        printf("creat socket fail!\n");
        exit(1);
    }
    bzero(&client_sock,sizeof(client_sock));    /*填写各所需参数*/
    client_sock.sin_family = AF_INET;
    client_sock.sin_port = htons(portnumber);
    client_sock.sin_addr=*((struct in_addr *)host->h_addr);  /*设置远程 IP ,通过 hostent 变量获得 h->addr*/

    if(connect(fd,(struct sockaddr *)(&client_sock),sizeof(struct sockaddr)) == -1) /*连接请求*/
    {
        printf("connet fail!\n");        /***执行失败了,我执行的时候打印出 connet fail!,我不解为什么连接失败啊,反复查了好几遍了还是没有看出来***/
        exit(1);
    }
    printf("connet from the serve successfully!\n");
    printf("please inpu :");
    scanf("%s",s);
    close(fd);
}






------解决方案--------------------
客户端
if(fd = socket(AF_INET,SOCK_STREAM,0) == -1)

漏括号了。。
if((fd = socket(AF_INET,SOCK_STREAM,0)) == -1)