gpt4 book ai didi

c套接字revcv接收截断的字符串

转载 作者:行者123 更新时间:2023-11-30 18:56:20 26 4
gpt4 key购买 nike

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

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

服务器.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;


}

客户端.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。

最佳答案

在客户端代码中,您有

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

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

关于c套接字revcv接收截断的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24531661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com