无法通过套接字发送或接收数据

时间:2019-11-03 14:34:47

标签: c++ c sockets

我尝试进行简单的服务器-客户端套接字通信,但是服务器似乎无法正常工作。每当我发送或接收时都会收到此错误:

  

非套接字上的套接字操作:非套接字上的套接字操作

奇怪的是,它仅在服务器发送时出现。 客户似乎还可以:

//server
#include <iostream>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>

using namespace std;

int main(){
    const int PORT = 5000;
    int serverfd, clientfd, opt=1, n_send, n_recv;
    sockaddr_in server_address, client_address;
    char recvbuf[1024], sendbuf[1024];
    bzero(&server_address , sizeof(server_address));
    inet_pton(AF_INET, "127.0.0.1",&server_address.sin_addr);
    int x = INADDR_ANY;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(PORT);

    socklen_t addrSize = sizeof(server_address);

    if((serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    if((setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < 0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    if((bind(serverfd, (sockaddr*)&server_address, sizeof(server_address)))< 0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    if((listen(serverfd, 1))< 0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    if((clientfd = accept(serverfd, (sockaddr *)&server_address, (socklen_t*)&addrSize) < 0)){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }

    while (true){
        cout << "SAY TO CLIENT: ";
        bzero(sendbuf, sizeof(sendbuf));
        cin >> sendbuf;
        cout << endl;
        send(clientfd, sendbuf, strlen(sendbuf), 0);
        errno = 0;
        bzero(recvbuf, sizeof(recvbuf));
        if((n_recv = recv(clientfd, recvbuf, sizeof(recvbuf), MSG_WAITALL))< 0){
            perror(strerror(errno));
            exit(EXIT_FAILURE);
        }
        cout << recvbuf << endl;
    }

    return 0;
}
//client
#include <iostream>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>

using namespace std;

int main(){
    const int PORT = 5000;
    int sockfd, n_send, n_recv;
    char recvbuf[1024], sendbuf[1024];
    sockaddr_in server_address;
    bzero(&server_address, sizeof(server_address));
    server_address.sin_family= AF_INET;
    server_address.sin_port = htons(PORT);
    inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);
    if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    if(connect(sockfd, (sockaddr *) &server_address, sizeof(server_address))<0){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    while (true){
        if((n_recv = recv(sockfd, recvbuf, sizeof(recvbuf), MSG_WAITALL)) < 0){
            perror(strerror(errno));
            exit(EXIT_FAILURE);
        }
        cout << "Server: " << recvbuf << endl;
        cout << "Say to server: ";
        cin >> sendbuf;
        cout << endl;
        if((n_send = send(sockfd, sendbuf, sizeof(sendbuf), 0)) < 0){
            perror(strerror(errno));
            exit(EXIT_FAILURE);
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

clientfd被分配了一个非套接字值。如果我们更改您的if中的空格:

if((clientfd =
     accept(serverfd, (sockaddr *)&server_address, (socklen_t*)&addrSize) < 0
   )) {
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }

由于编写方式的原因,检查了accept(可能是有效的套接字)的结果,并将该结果设置为clientFd。请注意,您的括号位置与其他行不匹配。