本文介绍了如何在用户收到的消息末尾添加\ R \ N Crlf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #includestdafx.h #include< stdio.h> # include< winsock2.h> #pragma comment(lib,ws2_32.lib)// Winsock Library int main(int argc,char * argv []) { WSADATA wsa; SOCKET s; struct sockaddr_in服务器; char server_reply [5000]; char * message = new char [20]; int recv_size; Append_EOL = yes; EOL = CRLF; printf(\\\Initialising Winsock ......); if(WSAStartup(MAKEWORD(2,2),& wsa)!= 0) { printf(失败。错误代码:%d,WSAGetLastError()); 返回1; } printf(Initialised.\); //创建套接字 if((s = socket(AF_INET,SOCK_STREAM,0))== INVALID_SOCKET) { printf(无法创建套接字:%d,WSAGetLastError()); } printf(Socket created.\\\); server.sin_addr.s_addr = inet_addr(169.254 .1.10); server.sin_family = AF_INET; server.sin_port = htons(23); //连接到远程服务器 if(connect(s,(struct sockaddr *)& server,sizeof(server))< 0) { put(连接错误); 返回1; } put(已连接); //发送一些数据 b $ b printf(输入要发送的消息\ n); gets_s(消息,19 ); printf(%s,消息); if(send(s,message,strlen(message),0)< 0) { put(发送失败); 返回1; } puts(Data Send\\\); //收到服务器的回复 if((recv_size = recv(s,server_reply,2000,0))== SOCKET_ERROR) { puts(recv failed); } puts(Reply received\\\); //添加一个NULL终止字符来实现它打印前正确的字符串 server_reply [recv_ size] ='\ 0'; printf(\ n); printf(\ n); puts(server_reply); closesocket(s) ; WSACleanup(); getchar(); 返回0; }#include "stdafx.h"#include<stdio.h>#include<winsock2.h>#pragma comment(lib,"ws2_32.lib") //Winsock Libraryint main(int argc , char *argv[]){ WSADATA wsa; SOCKET s; struct sockaddr_in server; char server_reply[5000];char *message = new char[20]; int recv_size;Append_EOL=yes;EOL=CRLF; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { printf("Failed. Error Code : %d",WSAGetLastError()); return 1; } printf("Initialised.\n"); //Create a socket if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) { printf("Could not create socket : %d" , WSAGetLastError()); } printf("Socket created.\n"); server.sin_addr.s_addr = inet_addr("169.254.1.10"); server.sin_family = AF_INET; server.sin_port = htons( 23); //Connect to remote server if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0) { puts("connect error"); return 1; } puts("Connected"); //Send some data printf("enter message to be sent\n");gets_s(message,19);printf("%s",message); if( send(s , message , strlen(message) , 0) < 0) { puts("Send failed"); return 1; } puts("Data Send\n"); //Receive a reply from the server if((recv_size = recv(s , server_reply ,2000, 0)) == SOCKET_ERROR) { puts("recv failed"); } puts("Reply received\n");//Add a NULL terminating character to make it a proper string before printing server_reply[recv_size] = '\0';printf("\n");printf("\n"); puts(server_reply);closesocket(s); WSACleanup();getchar(); return 0;}推荐答案使用 strcat 追加缺失的字符。缓冲区必须有足够的空间容纳其他字符。use strcat to append for missing characters. The buffer must have enough space for that additional characters. 这篇关于如何在用户收到的消息末尾添加\ R \ N Crlf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 17:47