我有一个客户端和一个服务器,当客户端连接到服务器时,服务器将读取一个文本文件,将其转换为大写并逐行发送给客户端。文本文件名为“text.txt”,如下所示:
服务器代码
#include<io.h>
#include<iostream>
#include <string>
#include<fstream>
#include<winsock2.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
using namespace std;
void stringToUpper(string &s)
{
for(unsigned int l = 0; l < s.length(); l++)
{
s[l] = toupper(s[l]);
}
}
int main()
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
char *message;
cout<<"\nInitialising Winsock...";
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
cout<<"Failed. Error Code"<<WSAGetLastError();
return 1;
}
cout<<"Initialised.\n";
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
cout<<"Could not create socket "<< WSAGetLastError();
}
cout<<"Socket created.\n";
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
cout<<"Bind failed with error code : "<< WSAGetLastError();
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
{
puts("Connection accepted");
//Reply to the client
string STRING;
ifstream infile;
infile.open ("text.txt");
while(!infile.eof())
{
getline(infile,STRING);
stringToUpper(STRING);
const char *STRING_mod = STRING.c_str();
send(new_socket , STRING_mod , strlen(STRING_mod) , 0);
}
char *end_msg="end";
send(new_socket , end_msg , strlen(end_msg) , 0);
infile.close();
}
if (new_socket == INVALID_SOCKET)
{
cout<<"accept failed with error code : " << WSAGetLastError();
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
客户代码
#include<iostream>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
using namespace std;
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message , server_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
cout<<"Failed. Error Code : "<<WSAGetLastError();
return 1;
}
cout<<"Initialised.\n";
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
cout<<"Could not create socket : " << WSAGetLastError();
}
cout<<"Socket created.\n";
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
recv_size = recv(s, server_reply, 2000, 0);
server_reply[recv_size] = '\0';
while ( server_reply != "end"){
cout<<server_reply<<endl;
recv_size = recv(s, server_reply, 2000, 0);
server_reply[recv_size] = '\0';
}
closesocket(s);
WSACleanup();
return 0;
}
当我运行服务器然后客户端时,我有以下响应:
但这不是必需的行为,我希望文本出现在文本文件中,“逐行”......那么我的代码中的问题在哪里以及如何解决它?以及为什么客户端卡在 while 循环中?
谢谢。