У меня есть клиентская программа в которой вводится массив чисел и сервер в котором вычисляется среднее значение этого массива.
Сервер:
#include <yvals.h>
#include <ymath.h>
#include <iostream>
#include <string>
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#pragma warning(disable : 4996)
int main()
{
setlocale(LC_CTYPE, "rus");
WSAData v_2_0;
int WSAStartup_ = WSAStartup(MAKEWORD(2, 0), &v_2_0);
if (WSAStartup_ != 0)
{
cout << "Initialization error" << endl;
}
SOCKET ServerSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ServerSock == INVALID_SOCKET)
{
cout << "Socket not created" << endl;
}
sockaddr_in getting = {};
getting.sin_family = AF_INET;
getting.sin_port = htons(4567);
getting.sin_addr.s_addr = inet_addr("127.0.0.1");
int bind_ = bind(ServerSock, (sockaddr*)&getting, sizeof(getting));
if (bind_ == SOCKET_ERROR)
{
cout << "Error" << endl;
}
listen(ServerSock, SOMAXCONN);
if (listen(ServerSock, SOMAXCONN) == SOCKET_ERROR)
{
cout << "Network port not found" << endl;
}
sockaddr_in clientAddr = {};
int nAddrLen = sizeof(sockaddr_in);
SOCKET accept_ = accept(ServerSock, (sockaddr*)&clientAddr, &nAddrLen);
if (accept_ == INVALID_SOCKET)
{
cout << "Error" << endl;
}
char buff[255] = {};
char mess[255] = {};
string word, res;
int n;
int sum = 0;
while (true)
{
int len = recv(accept_, buff, sizeof(buff), 0);
if (len <= 0)
{
cout << "The client broke the connection" << endl;
break;
}
word = (string)buff;
n = stof(word.substr(0, word.find_first_of(' ')));
string word_ = word.substr(word.find_first_of(' '), word.find_last_of(' '));
sum = stof(word.substr(word.find_last_of(' ') + 1, word.size()));
cout << "The sum of numbers =" << sum << endl;
cout << "The arithmetic mean :" << sum/n << endl;
}
closesocket(ServerSock);
WSACleanup();
system("pause>>void");
}
Клиент:
#include <iostream>
#include <cstdlib>
#include <WS2tcpip.h>
#include <string>
#pragma comment(lib, "ws2_32.lib")
#pragma warning(disable : 4996)
using namespace std;
int main()
{
setlocale(LC_CTYPE, "rus");
WSAData v_2_0;
WORD make = MAKEWORD(2, 2);
int WSAStartup_ = WSAStartup(make, &v_2_0);
if (WSAStartup_ != 0)
{
cout << "Error starting WinSock" << WSAStartup_ << endl;
}
SOCKET ClientSock = socket(AF_INET, SOCK_STREAM, 0);
if (ClientSock == INVALID_SOCKET)
{
cout << "Socket not created" << WSAGetLastError() << endl;
}
else
{
cout << "Connection to server successful" << endl;
}
sockaddr_in getting = {};
getting.sin_family = AF_INET;
getting.sin_port = htons(4567);
getting.sin_addr.s_addr = inet_addr("127.0.0.1");
int connResult = connect(ClientSock, (sockaddr*)&getting, sizeof(sockaddr_in));
if (connResult == SOCKET_ERROR)
{
cout << "Error! " << WSAGetLastError() << endl;
}
else cout << "Connection established" << endl;
while (true)
{
char buff[255] = {};
char mess[255] = {};
int n, num, s;
int sum = 0;
int option;
string word = "";
string final = "";
cout << "Enter a sequence of numbers (Enter 1)" << endl;
cout << "Exit (Enter 2)" << endl;
cout << "Your number:";
cin >> option;
switch (option)
{
case 1:
{
cout << "input n: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << i + 1 << " / " << n << " : ";
cin >> num;
sum += num;
}
word = to_string(n)+' '+to_string(sum);
}
break;
case 2:
{
cout << "exit ? ";
cin >> word;
break;
}
}
for (int i = 0; i < word.size(); i++)
{
buff[i] = word[i];
}
if (strcmp(buff, "exit") == 0)
{
cout << "The connection to the server has been dropped" << endl;
break;
}
else
{
int send_ = send(ClientSock, buff, sizeof(buff), 0);
if (send_ == SOCKET_ERROR)
{
cout << "Error" << endl;
}
}
}
closesocket(ClientSock);
WSACleanup();
system("pause>>void");
}
Я хочу, чтоб результат полученный на сервере передался клиенту и там тоже вывелся. Я пробовал обратный метод когда передаешь с клиента на сервер, но результат не передается.