我是套接字编程的新手。我想编写一个简单的客户端 - 服务器程序,但是当我运行它时,我在接收函数中得到一个(" 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);
}
答案 0 :(得分:1)
在您的服务器代码中,您在传递给recv()
和listen()
的套接字上调用accept()
。
accept()
返回表示已连接客户端的套接字。这是您应传递给send()
和recv()
的套接字。
此外,服务器代码中的do/while
循环很乱。究竟你想做什么?