C ++发送和接收TCP / IP结构

时间:2015-08-13 19:01:47

标签: c++ sockets tcp

这是我的客户:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <string.h>

using namespace std;

typedef struct TEMPIO
{
    unsigned int id;
    unsigned short messageSender;
    unsigned short length;
    unsigned int secs;
    unsigned int usecs;
    unsigned short videoId;
    unsigned short outChannel;
}TEMPIO_msg;

int client();
int server();
void error();


void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(){
    int sockfd, portno, n;
    char * address;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    TEMPIO_msg message;
    struct timeval time;

    portno=2015;//TBD take portno
    address="127.0.0.1";//TBD take address

    message.id=htonl(1694367746);
    message.messageSender=htons(100);
    message.length=htons(20);
    gettimeofday(&time,NULL); 
    message.secs=htonl(time.tv_sec);
    message.usecs=htonl(time.tv_usec);
    message.videoId=htons(44);
    message.outChannel=htons(38);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0){
        error("ERROR opening socket");
        exit(0);
    }

    server = gethostbyname(address);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
    serv_addr.sin_port = htons(portno);

    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
        error("ERROR connecting");
        exit(0);
    }

    while(true){
        if (sendto(sockfd,&message,sizeof(TEMPIO_msg),MSG_EOR,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
        {
            perror("sendto");
            exit(1);
        }else{
            printf("CLI: sent something...\n"); 
        }
        sleep(3);
    }

    close(sockfd);
    return 0;
}

这是我的服务器:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <string.h>

using namespace std;

typedef struct TEMPIO
{
    unsigned int id;
    unsigned short messageSender;
    unsigned short length;
    unsigned int secs;
    unsigned int usecs;
    unsigned short videoId;
    unsigned short outChannel;
}TEMPIO_msg;

int client();
int server();
void error();


void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(){
    int sockfd, newsockfd, portno;
    char * address;
    socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    TEMPIO_msg message;
    struct timeval time;

    address="127.0.0.1";
    portno = 2015; // TBD

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) error("ERROR opening socket");

    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    printf("SRV: started! \n");
    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
    if (newsockfd < 0) error("ERROR on accept");
    printf("SRV: client connected! \n");

    printf("SRV: Listening... ");
    if (recvfrom(sockfd,&message,sizeof(TEMPIO_msg),MSG_PEEK,(struct sockaddr *) &cli_addr, &clilen) > 0) 
    {
        printf("SRV catched something!\n");
    }
    close(newsockfd);
    close(sockfd);
    return 0; 

}

如您所见,我正在使用以下函数发送/接收数据结构:

ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
       int flags, struct sockaddr *restrict address,
       socklen_t *restrict address_len);
ssize_t sendto(int socket, const void *message, size_t length,
       int flags, const struct sockaddr *dest_addr,
       socklen_t dest_len);

我的代码编译正确,但工作不正常。 这很奇怪,因为我在客户端之后启动了服务器,服务器似乎陷入了这一点:

$ ./srv SRV: started!
SRV: client connected!

客户端正在正确发送数据包,服务器仅在客户端发送4次后继续执行

$ ./cli 
CLI: sent something...
CLI: sent something...
CLI: sent something...
CLI: sent something...

这很奇怪,因为你可以看到,似乎服务器卡在两个printf(客户端连接和侦听)之间:

./srv SRV: started! 
SRV: client connected! 
SRV: Listening... 

你能抓到什么问题吗?

1 个答案:

答案 0 :(得分:2)

您在服务器中的recvfrom调用正在尝试从sockfd读取,这是侦听套接字。您应该从newsockfd读取,这是连接的套接字。

此外,您应该将recvfrom的返回值复制到变量中。您首先检查它是否为-1,在这种情况下您将处理错误。如果没有,您将检查以确保您读取预期的字节数。

关于recvfrom:由于您正在处理TCP套接字,因此sendtorecv应分别替换为sendnode --harmony myscript.js 。所以终点已经知道了。