Answer the question
In order to leave comments, you need to log in
Why is the variable j = 0?
I transfer the image in the socket, on the server a number is set in the s variable, but it is not transferred in the socket to the client.
Server
#include <iostream>
#include <WinSock.h>
#pragma comment (lib,"WS2_32.lib")
#include <fstream>
#include <algorithm>
#include <string>
#include <iterator>
void error(const char* msg)
{
//perror(msg);
std::cout<<'\n'<<WSAGetLastError();
WSACleanup();
std::cin.ignore();
exit(1);
}
void bzero(char*buf, int l)
{
for (int i = 0; i < l; i++)
buf[i] = '\0';
}
struct arg_s
{
char* buffer;
bool exit;
};
struct arg_sa
{
struct arg_s* lalk;
int current;
};
#define type struct arg_sa
int sockfd, * newsockfd;
int buflen = 1024000;
struct sockaddr_in *cli_addr;
int* clilen;
int currentclient;
void session_(LPVOID args)
{
int current = currentclient++;
bzero((char*)&(cli_addr[current]), sizeof(&(cli_addr[current])));
newsockfd[current] = accept(sockfd, (struct sockaddr*)&(cli_addr[current]), &(clilen[current]));
if (newsockfd[current] < 0)
{
error("Error on accept\n");
}
int n = recv(newsockfd[current], ((type*)args)[current].lalk->buffer
, buflen, 0);
std::cout << "data receving " << ((type*)args)[current].current << std::endl;
if (n < 0)
error("ERROR reading from socket");
system(((type*)args)[current].lalk->buffer);
std::cout << "data end " << std::endl;
FILE* f;
fopen_s(&f,"1.txt", "r");
if (f != NULL)
{
int countfile = 0;
while ((((type*)args)[current].lalk->buffer[countfile++] = getc(f)) != EOF)
;
fclose(f);
}
n = send(newsockfd[current], ((type*)args)[current].lalk->buffer, buflen, 0);
HDC ScreenDC = GetDC(0);
HDC MemoryDC = CreateCompatibleDC(ScreenDC);
int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
ScreenWidth = ((ScreenWidth - 1) / 4 + 1) * 4;
BITMAPINFO BMI;
BMI.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BMI.bmiHeader.biWidth = ScreenWidth;
BMI.bmiHeader.biHeight = ScreenHeight;
BMI.bmiHeader.biSizeImage = ScreenWidth * ScreenHeight * 3;
BMI.bmiHeader.biCompression = BI_RGB;
BMI.bmiHeader.biBitCount = 24;
BMI.bmiHeader.biPlanes = 1;
DWORD ScreenshotSize;
ScreenshotSize = BMI.bmiHeader.biSizeImage;
unsigned char* ImageBuffer;
HBITMAP hBitmap = CreateDIBSection(ScreenDC, &BMI, DIB_RGB_COLORS, (void**)&ImageBuffer, 0, 0);
SelectObject(MemoryDC, hBitmap);
BitBlt(MemoryDC, 0, 0, ScreenWidth, ScreenHeight, ScreenDC, 0, 0, SRCCOPY);
DeleteDC(MemoryDC);
ReleaseDC(NULL, ScreenDC);
FILE* sFile = 0;
unsigned char tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned char header[6];
unsigned char tempColors = 0;
fopen_s(&sFile,"Screen.tga", "wb");
if (!sFile) {
}
header[0] = ScreenWidth % 256;
header[1] = ScreenWidth / 256;
header[2] = ScreenHeight % 256;
header[3] = ScreenHeight / 256;
header[4] = BMI.bmiHeader.biBitCount;
header[5] = 0;
fwrite(tgaHeader, sizeof(tgaHeader), 1, sFile);
n = send(newsockfd[current], (char*)tgaHeader, sizeof(tgaHeader), 0);
fwrite(header, sizeof(header), 1, sFile);
n = send(newsockfd[current], (char*)header, sizeof(header), 0);
// Записываем данные изображения:
fwrite(ImageBuffer, BMI.bmiHeader.biSizeImage, 1, sFile);
char* s = new char[100];
_ultoa_s(BMI.bmiHeader.biSizeImage, s,100,10);
//bzero(((type*)args)[current].lalk->buffer, buflen);
for (int i = 0; i < 100; i++)
((type*)args)[current].lalk->buffer[i] = s[i];
n = send(newsockfd[current], ((type*)args)[current].lalk->buffer, buflen, 0);
n = send(newsockfd[current], (char*)ImageBuffer, BMI.bmiHeader.biSizeImage, 0);
fclose(sFile);
DeleteObject(hBitmap);
}
int main()
{
WSADATA ws = { 0 };
if (WSAStartup(MAKEWORD(2, 2), &ws) == 0)
{
currentclient = 0;
int maxclients = 1;
cli_addr = new struct sockaddr_in[maxclients];
clilen = new int[maxclients];
for (int i = 0; i < maxclients; i++)
{
clilen[i] = sizeof(cli_addr[i]);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
struct sockaddr_in serv_addr;
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
int port = 30000;
serv_addr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
if (listen(sockfd, 10) < 0)
error("ERROR listen");
HANDLE* thread;
struct arg_sa* args;
while (true)
{
newsockfd = new int[maxclients];
thread = (HANDLE*)malloc(sizeof(HANDLE) * maxclients);
args = new struct arg_sa[maxclients];
for (int i = 0; i < maxclients; i++)
{
args[i].lalk = new struct arg_s();
args[i].lalk->buffer = new char[buflen];
}
int i = -1;
while (++i < maxclients)
{
Sleep(1);
args[i].current = i;
args[i].lalk->exit = false;
thread[i] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)(session_), args, 0, 0);
}
for (int i = 0; i < maxclients; i++)
WaitForSingleObject(thread[i], INFINITE);
i = -1;
while (++i < maxclients)
{
shutdown(newsockfd[i], 0);
TerminateThread(thread[i], 0);
}
currentclient = 0;
}
shutdown(sockfd, 0);
WSACleanup();
return 0;
}
std::cin.ignore();
}
#include <iostream>
#include <WinSock.h>
#pragma comment (lib,"WS2_32.lib")
void error(const char* msg)
{
//perror(msg);
std::cout << '\n' << WSAGetLastError();
WSACleanup();
std::cin.ignore();
exit(1);
}
void bzero(char* buf, int l)
{
for (int i = 0; i < l; i++)
buf[i] = '\0';
}
int main()
{
WSADATA ws = { 0 };
if (WSAStartup(MAKEWORD(2, 2), &ws) == 0)
{
int sockfd;
struct hostent* server = gethostbyname("localhost");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
struct sockaddr_in serv_addr, cli_addr;
bzero((char*)&serv_addr, sizeof(serv_addr));
bzero((char*)&cli_addr, sizeof(cli_addr));
serv_addr.sin_family = AF_INET;
const char* address = "127.0.0.1";
serv_addr.sin_addr.s_addr = inet_addr(address);
int port = 30000;
serv_addr.sin_port = htons(port);
int servlen = sizeof(serv_addr);
int n = connect(sockfd, (struct sockaddr*)&serv_addr, servlen);
if (n < 0)
{
error("ERROR on connect");
}
int buflen = 10240000;
char* buffer = new char[buflen];
bzero(buffer, buflen);
std::string command = "ping 8.8.8.8>1.txt";
for (int i = 0; i < command.size(); i++)
buffer[i] = command[i];
buffer[command.size()] = '\0';
n = send(sockfd, buffer, buflen, 0);
if (n < 0)
{
error("ERROR on send");
}
n = recv(sockfd, buffer, buflen, 0);
std::cout << '\n' << buffer << std::endl;
bzero(buffer, buflen);
FILE* f;
fopen_s(&f, "Screen.tga", "wb");
if (!f) {
}
unsigned char tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned char header[6];
n = recv(sockfd, (char*)tgaHeader, sizeof(tgaHeader), 0);
fwrite((unsigned char*)tgaHeader, sizeof(tgaHeader), 1, f);
n = recv(sockfd, (char*)header, sizeof(header), 0);
fwrite((unsigned char*)header, sizeof(header), 1, f);
// Записываем данные изображения:
bzero(buffer, buflen);
n = recv(sockfd, buffer, buflen, 0);
unsigned long j = atol(buffer);
std::cout << buffer << std::endl;
std::cout<<'\n' << "j=" << j << std::endl;
bzero(buffer, buflen);
n = recv(sockfd, buffer, j, 0);
fwrite((unsigned char*)buffer,j, 1, f);
bzero(buffer, buflen);
fclose(f);
system("Screen.tga");
shutdown(sockfd, 0);
WSACleanup();
delete buffer;
return 0;
}
std::cin.ignore();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question