c ++ linux socket服务器反复调用客户端

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c ++ linux socket服务器反复调用客户端相关的知识,希望对你有一定的参考价值。

所以我很困惑如何让服务器多次与我的客户端来回发送数据。目前,服务器将数据发送到客户端,然后客户端将发回答案,但我不知道如何多次执行此操作,而无需一次又一次地启动客户端服务器。

基本上我想知道如何让服务器和客户端重复通信而无需创建新客户端。

这是我的服务器代码。我正在创建一个随机数,然后使用客户端来确定其因素

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <cmath>

using namespace std;

int *findPrimes(int primeArray[])
{
    int i, j, n = 100000, flag;
    //int primeArray[65];
    int count = 0;

    for(i = 2; i <= n; i++)
    {
        flag = 1;
        for(j =2;j<=i/2;j++)
        {
            if(i%j==0)
            {
                flag = 0;
                break;
            }
        }
        if(flag==1)
        {
            primeArray[count] = i;
            count++;
        }
    }
    return primeArray;
}

int calcNumber()
{
    srand(time(NULL));

    int base = rand()%9 + 2;
    int numCalc[5];
    bool isntBase = false;
    int count = 0;
    int testNum;

    do{
        testNum = rand() % 100000;
        int number = testNum;
        cout << base << ", " << number << endl;
        count = 0;
        isntBase = false;

        for(int i = 0; i < 5; i++)
        {
            numCalc[i] = 0;
        }

        while (number > 0)
        {
            int digit = number%10;
            number /= 10;
            numCalc[count] = digit;
            count++;
            if(digit >= base)
            {
                isntBase = true;
            }
        }
    }while(isntBase == true);

    cout << testNum << "(" << base <<")";
    int decConvert = 0;
    int baseMult = 1;
    for(int i = 0; i < 5; i++)
    {
        //cout << numCalc[i] << endl;
        decConvert += numCalc[i]*(pow(base, i));
        //baseMult = baseMult * 7;
        //cout << decConvert << endl;
    }
    cout << " = " << decConvert << "(10) = ";
    return decConvert;
}


int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];
    time_t ticks; 

    int n = 3;
    int numOfPrime= 0;
    bool isPrime = false;
    bool noMorePlease = false;
    int y[9592];
    int *prime = findPrimes(y);

    int number = calcNumber();

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5575); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 2); 

    int prcount = 0;

    while(noMorePlease == false){
        //Thread

        int client_socket;
        client_socket = accept(listenfd, NULL, NULL);

        int primeNum = prime[prcount];
        send(client_socket, &number, sizeof(number),0);
        send(client_socket, &primeNum,sizeof(primeNum), 0);

        int recvNum = 0;
        //char recvVal[256];   
        //recv(listenfd, &number, sizeof(number), 0);
        recv(client_socket, &recvNum, sizeof(recvNum), 0);

        //cout << endl << recvNum;

        if(recvNum >0)
        {
            cout << prime[prcount] << "^" << recvNum <<"*";
        }
        while(recvNum > 0)
        {
            number = number/prime[prcount];
            recvNum--;
        }
        prcount++;
        if(number == 1)
        {
            cout << endl;
            numOfPrime = 0;
            noMorePlease = true;
        }
        if(prcount > 9592)
        {
            numOfPrime++;
            cout << " a prime number!" << endl;
            numOfPrime++;
            if(numOfPrime == n)
            {
                noMorePlease = true;
                cout << "There were " << n << " primes in a row" << endl;
                close(listenfd);
                return 0;
            }
        }
        //close(client_socket);
    }

    close(listenfd);
    return 0;
}

这是我的客户端代码

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

using namespace std;

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in serv_addr; 


    if(argc != 2)
    {
        printf("
 Usage: %s <ip of server> 
",argv[0]);
        return 1;
    } 

    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("
 Error : Could not create socket 
");
        return 1;
    } 

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5575); 

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("
 inet_pton error occured
");
        return 1;
    } 

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("
 Error : Connect Failed 
");
       return 1;
    } 
    int number = 0;
    int primeNum;
    char recvVal[256];   
    recv(sockfd, &number, sizeof(number), 0);
    recv(sockfd, &primeNum, sizeof(primeNum), 0);

    int divCount = 0;

    cout << number << ", " << primeNum << endl;
    while(number % primeNum == 0)
    {
        cout << number << ",  ";
        number =  number/primeNum;
        divCount++;
    }

    cout << endl << divCount;
    send(sockfd, &divCount,sizeof(divCount), 0);

    /*
    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            printf("
 Error : Fputs error
");
        }
    } 

    if(n < 0)
    {
        printf("
 Read error 
");
    } 
*/
    close(sockfd);
    return 0;
}
答案

在您的客户端代码中,在行之后:

if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)

你应该做recv并在循环内发送,以便连接保持打开状态。完成后,退出循环并关闭套接字。可以在此处找到使用同步套接字的此方法的示例:https://vcansimplify.wordpress.com/2013/03/14/c-socket-tutorial-echo-server/

以上是关于c ++ linux socket服务器反复调用客户端的主要内容,如果未能解决你的问题,请参考以下文章

Socket与系统调用深度分析

使用Socket&反射&Java流操作进行方法的远程调用(模拟RPC远程调用)

Socket.IO-client.java 反复断开重连

Linux网络学习_TCP客户端服务器编程

socket connect 反复使用,为何到一定的时候connect不上服务器?

C/C++ Linux Socket网络编程