c socket revcv接收截断的字符串

时间:2014-07-02 12:55:42

标签: c string sockets recv

我有一个客户端 - 服务器套接字应用程序。客户端正在发送一些文本,服务器在标准输出上打印出来。我遇到的问题是rcv函数只接收文本的一部分。发送的字符串在第五个字符处被截断。

我该如何解决这个问题?有人可以帮帮我吗?

server.c

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#endif

#define SERVERADDRESS "10.10.10.10"
#define PROTOCOLPORT 45678
#define MAXCONNESSIONI 10

#define MAXBUFFERSIZE 256

#include <stdio.h>
#include <stdlib.h>



void errorManager(const char* messaggioErrore)
{
    const char* erroreString="\nError: ";

    printf (erroreString);
    printf (messaggioErrore);
    printf ("\n");
}

void clearWinsock()
{
#if defined WIN32
    WSACleanup();
#endif
}


int main(int argc, char** argv) {

#if defined WIN32

    WSADATA wsaData;
    int iResult=WSAStartup(MAKEWORD(2,2),&wsaData);

    if (iResult!=0)
    {
        errorManager("Error WSASTARTUP");
        return 0;
    }
#endif



    char buffer[MAXBUFFERSIZE];
    char* Nome;



    int serverSocket;
    serverSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);


    struct sockaddr_in serverSocketAddress;
    serverSocketAddress.sin_family=AF_INET;
    serverSocketAddress.sin_addr.s_addr=inet_addr(SERVERADDRESS);
    serverSocketAddress.sin_port=htons(PROTOCOLPORT);


    if (bind(serverSocket,(struct sockaddr*)&serverSocketAddress,sizeof(serverSocketAddress))<0)
    {

        errorManager("bind failed.");

        closesocket(serverSocket);
        clearWinsock();
        return EXIT_FAILURE;
    }


    if (listen(serverSocket,MAXCONNESSIONI)<0)
    {

        errorManager("listen  failed.");

        closesocket(serverSocket);
        clearWinsock();
        return EXIT_FAILURE;
    }




    int clientSocket;
    struct sockaddr_in clientSocketAddress;
    int clientSocketAddressLength;
    clientSocketAddressLength=sizeof(clientSocketAddress);




    while(1)
    {

        printf ("Waiting for client connections...\n");

        if ((clientSocket=accept(serverSocket,(struct sockaddr*)&clientSocketAddress,&clientSocketAddressLength))<0)
        {

            errorManager("accept failed.");

            closesocket(serverSocket);
            clearWinsock();
            return EXIT_FAILURE;
        }

        printf("Connection from client %s:%i accepted.\n",inet_ntoa(clientSocketAddress.sin_addr),ntohs(clientSocketAddress.sin_port));


        memset(buffer,0,sizeof(buffer));
        if ((recv(clientSocket,buffer,MAXBUFFERSIZE,0))<0)
        {

            errorManager("recv failed.");

            closesocket(serverSocket);
            clearWinsock();
            return EXIT_FAILURE;
        }

        Name=buffer;

        printf("Name: %s\n",Name);



        ///ECHO
        if ((send(clientSocket,Name,sizeof(Name),clientSocketAddressLength))!=sizeof(Name))
        {

            errorManager("send failed.");

            closesocket(serverSocket);
            clearWinsock();
            return EXIT_FAILURE;
        }


        closesocket(clientSocket);
    }

    printf("Terminating server...");


    closesocket(serverSocket);
    clearWinsock();
    return EXIT_SUCCESS;


}

client.c

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#endif



#include <stdio.h>
#include <stdlib.h>



#define DEFAULTSERVERADDRESS "10.10.10.10"
#define DEFAULTPROTOCOLPORT 45678

#define MAXBUFFERSIZE 256


void ErrorManager(const char* messaggioErrore)
{
    const char* erroreString="\nError: ";

    printf (erroreString);
    printf (messaggioErrore);
    printf ("\n");
}

void clearWinsock()
{
#if defined WIN32
    WSACleanup();
#endif
}


int main(int argc, char** argv) {

    char* serverAddress;
    int serverPort;


    if (argc>1)
    {
        serverAddress=argv[1];

        if (argc>2)
        {
            serverPort=atoi(argv[2]);
        }
        else
        {
            serverPort=DEFAULTPROTOCOLPORT;
        }
    }
    else
    {
        serverAddress=DEFAULTSERVERADDRESS;
        serverPort=DEFAULTPROTOCOLPORT;
    }

#if defined WIN32

    WSADATA wsaData;
    int iResult=WSAStartup(MAKEWORD(2,2),&wsaData);

    if (iResult!=0)
    {
        ErrorManager("Error WSASTARTUP");
        return 0;
    }

#endif



    int clientSocket;
    clientSocket=socket(PF_INET,SOCK_STREAM, IPPROTO_TCP);



    struct sockaddr_in serverSocketAddress;
    serverSocketAddress.sin_family=AF_INET;
    serverSocketAddress.sin_addr.s_addr=inet_addr(serverAddress);
    serverSocketAddress.sin_port=htons(serverPort);

    if (connect(clientSocket,(struct sockaddr*)&serverSocketAddress,sizeof(serverSocketAddress))<0)
    {

        ErrorManager("connect failed.");

        closesocket(clientSocket);
        clearWinsock();
        return EXIT_FAILURE;
    }

    const char* Name="SOMETEXT TO SEND OVER";


    if (send(clientSocket,Name,sizeof(Name),0)!=sizeof(Name))
    {

        ErrorManager("send failed.");

        closesocket(clientSocket);
        clearWinsock();
        return EXIT_FAILURE;
    }

    printf("client ha inviato: %s",Name);





    char buffer[MAXBUFFERSIZE];
    if (recv(clientSocket,buffer,MAXBUFFERSIZE,0)<0)
    {

        ErrorManager("recv failed.");

        closesocket(clientSocket);
        clearWinsock();
        return EXIT_FAILURE;
    }

    char *Name1=buffer;
    printf("Name: %s/n",Name1);






    clearWinsock();
    closesocket(clientSocket);
    return EXIT_SUCCESS;
}

我正在使用MinGW。

1 个答案:

答案 0 :(得分:2)

在客户端代码中

const char* Name="SOMETEXT TO SEND OVER";

if (send(clientSocket,Name,sizeof(Name),0)!=sizeof(Name))
                             ^               ^
                             |...............|               |
                             | 
                              ...... //Wrong.  need strlen(Name)+1