作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个客户端-服务器套接字应用程序。客户端正在发送一些文本,服务器在标准输出上打印出这些文本。我遇到的问题是 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/
我是一名优秀的程序员,十分优秀!