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

socket 小问题
我的代码是想让客户端一直向服务器发数据,服务端一直接收数据并输出。
现在运行结果是服务端只接收第一条数据,之后的数据就不接收了。
我是昨天开始学socket,所以不是很懂,多谢大家的指点。

客户端
C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main(int argc, char* argv[])
{
    int clientfd, n;
    struct sockaddr_in servaddr;
    char recvline[MAXLINE], sendline[MAXLINE];
    
    if(argc != 2)
    {
        printf("usage: ./client <ip address>\n");
        exit(0);
    }
    
    if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("create socket error %d: %s", errno, strerror(errno));
        exit(-1);
    }
    
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(6666);
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        printf("inet_pton error for ip address: %s", argv[1]);
        exit(0);
    }

    if ( connect(clientfd, (struct sockaddr*)(&servaddr), sizeof(servaddr)) < 0)
    {
        printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
        exit(0);
    }

    printf("send messgae to server: \n");
    fgets(sendline, MAXLINE, stdin);
    while (strcmp(sendline, "exit") != 0)
    {
        if (send(clientfd, sendline,strlen(sendline),0) < 0)
        {
              printf("send message error: %s(errno: %d)\n", strerror(errno), errno);
              exit(0);
        }
        fgets(sendline, MAXLINE, stdin);
    }
    close(clientfd);
    return 0;
}




=========================================================

服务器
C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main()
{
    int listenfd, connfd;
    struct sockaddr_in serveraddress;
    char buff[MAXLINE];
    int n;

    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "create socket error %d: %s", errno, strerror(errno));
        exit(-1);
    }

    memset(&serveraddress, 0, sizeof(serveraddress));
    serveraddress.sin_family = AF_INET;
    serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddress.sin_port = htons(6666);

    if (bind(listenfd, (struct sockaddr*)(&serveraddress), sizeof(serveraddress)) == -1)
    {
        printf("blind socket error %d: %s", errno, strerror(errno));
        exit(-1);
    }

    if (listen(listenfd, 10) == -1)
    {
        printf("listen socket error %d: %s", errno, strerror(errno));
        exit(-1);
    }

    printf("================listen client request================\n");
    memset(buff, 0, MAXLINE);
    while (strcmp(buff,"exit") != 0)
    {
        if ((connfd = accept(listenfd, NULL, NULL)) == -1)
        {
            printf("accept socket error %d: %s", errno, strerror(errno));
            continue;
        }
        n = recv(connfd, buff, sizeof(buff), 0);
        buff[n] = '\0';
        fprintf(stdout, "server has received : %s\n", buff);
    }
    close(connfd);
    close(listenfd);
    return 0;
}



------解决方案--------------------
if ((connfd = accept(listenfd, NULL, NULL)) == -1)
{
printf("accept socket error %d: %s", errno, strerror(errno));
continue;
}