gpt4 book ai didi

c++ - 错误34;套接字编程结果太长

转载 作者:行者123 更新时间:2023-11-30 05:35:41 25 4
gpt4 key购买 nike

我是套接字编程的新手。我想编写一个简单的客户端-服务器程序,但是当我运行它时,我在接收函数中得到一个(“error34:结果太长”)。如果你能帮助我,我将不胜感激。我该怎么办?

服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include<winsock2.h>
#include <fstream>
#include <sys/types.h>
#ifdef __WIN32__
# include <winsock2.h>
#else
# include <sys/socket.h>
#endif
#ifdef __WIN32__
# include <winsock2.h>
#else
#include <netinet/in.h>
#endif

int main(){
WORD wVersionRequested;
WSADATA wsaData;
SOCKET intSocket,intRSocket;
struct sockaddr_in recServer,recClient,recSin;
int intErr,intLen,Message;
char charbuffer[256];

wVersionRequested=MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsaData)!=0)
{
printf("Error on initializing");
exit(1);
}

recServer.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
recServer.sin_family=AF_INET;
recServer.sin_port=htons(3666);

intSocket=socket(AF_INET,SOCK_STREAM,0);
if(intSocket ==(unsigned) -1){
printf("\n::Error On Create Socket::%d \n", errno);
perror("socket");
exit(1);
}

intErr=bind(intSocket,(struct sockadd*)&recServer,sizeof(recServer));
if(intErr == -1){
printf("\n::Error On the Call bind Function::\n");
exit(1);
}

else{
printf("Waiting for incoming connection");}

for(;;)
{

intErr=listen(intSocket,SOMAXCONN);
if(intSocket ==(unsigned) -1){
printf("\n::Error On the Call listen Function::\n");
exit(1);
}

intLen=sizeof(recClient);
intErr=accept(intSocket,(struct sockaddr *)&recClient,&intLen);
if(intSocket== (unsigned) -1){
printf("\n::Error On Connection::\n");
exit(1);
}



do{
intErr=recv(intSocket,charbuffer,sizeof(charbuffer),0);
if(intErr==(unsigned)-1)
{
printf("\n::Error On Recev Data:: %d\n" , errno );
perror("recv");
exit(1);
}
}
while( Message=recv(intSocket,charbuffer,sizeof(charbuffer),0));

Message=recv(intSocket,charbuffer,sizeof(charbuffer),0);

Message=send(intRSocket,charbuffer,strlen(charbuffer),0);
system("pause");



std::ofstream output_file("Recieved Data.txt", std::ios::out);

if(!output_file)
{
printf("file can not open");
exit(1);
}

else
{
output_file << charbuffer;
}
return 0;

}

客户端代码:客户端代码:

     #include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#ifdef __WIN32__
# include <winsock2.h>
#else
# include <sys/socket.h>
#endif
#ifdef __WIN32__
# include <winsock2.h>
#else
#include <netinet/in.h>
#endif


int main(){
WORD wVersionRequested;
WSADATA wsaData;
SOCKET intSocket;
struct sockaddr_in recSin;
int intErr;
char charbuffer[256];

wVersionRequested=MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsaData)!=0)
{ printf("Error on initializing");
exit(1);
}

recSin.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
recSin.sin_family=AF_INET;
recSin.sin_port=htons(3666);

intSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(intSocket == (unsigned)-1){
printf("\n::Error On Create Socket:: %d \n", intSocket );
perror("socket");
exit(1);
}

intErr=connect(intSocket,(struct sockaddr *)&recSin,sizeof(recSin));
if(intErr==-1){
printf("\n::Error On connecting::\n");

exit(1);
}

memcpy(charbuffer, "HELLO\0", 6);
intErr=send(intSocket,charbuffer,strlen(charbuffer),0);
if(intErr==-1)
{
printf("\n::Error On Send Data::\n");
exit(1);
}
memset(charbuffer,0,sizeof(charbuffer));


intErr=recv(intSocket,charbuffer,sizeof(charbuffer),0);
if(intErr==-1){
printf("\n::Error On Recev Data::\n");
exit(1);

}
printf("Recived Data,pchrBuffer");
closesocket(intSocket);
}

最佳答案

在您的服务器代码中,您在传递给 listen()accept() 的套接字上调用 recv()

accept() 返回代表已连接客户端的套接字。这是您应该传递给 send()recv() 的套接字。

此外,服务器代码中的 do/while 循环一团糟。你究竟想做什么?

关于c++ - 错误34;套接字编程结果太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840148/

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