Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // srednia.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #ifndef UNICODE
- #endif
- #define WIN32_LEAN_AND_MEAN
- #pragma warning(disable:4996)
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdio.h>
- #include <conio.h>
- #include <string>
- #include <string.h>
- #include <vector>
- #include <algorithm>
- #pragma pack(1)
- #pragma comment(lib, "ws2_32.lib")
- int wmain()
- {
- WSADATA wsaData;
- int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != NO_ERROR) {
- wprintf(L"WSAStartup function failed with error: %d\n", iResult);
- return 1;
- }
- SOCKET ConnectSocket;
- ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (ConnectSocket == INVALID_SOCKET) {
- wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- sockaddr_in clientService;
- clientService.sin_family = AF_INET;
- clientService.sin_addr.s_addr = inet_addr("192.168.0.3");
- clientService.sin_port = htons(7600);
- iResult = connect(ConnectSocket, (SOCKADDR *)& clientService, sizeof(clientService));
- if (iResult == SOCKET_ERROR) {
- wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
- iResult = closesocket(ConnectSocket);
- if (iResult == SOCKET_ERROR)
- wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- wprintf(L"Connected to server.\n");
- char recvbuf[1000];
- int recvbuflen = sizeof(recvbuf);
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
- recvbuf[iResult] = 0;
- printf("Bytes received: %s\n", recvbuf);
- std::string s = "";
- s += recvbuf[0];
- s += recvbuf[1];
- s += recvbuf[2];
- if (s == "200")
- {
- int n = 0, size = 10000;
- int i = 0;
- int mod = 0;
- int l = 0;
- float sum;
- int counter = 0;
- const char* ss = "SORT\r\n";
- iResult = send(ConnectSocket, ss, strlen(ss), 0);
- //sssss
- do {
- iResult = recv(ConnectSocket, recvbuf + n, recvbuflen - n, 0);
- n += iResult;
- if(n>=4)
- {
- mod = n%4;
- n = mod;
- sum += (float)recvbuf[l];
- printf("Aktualna suma to %d", sum);
- l += 4;
- counter++;
- if(counter%2==0)
- {
- sum = sum/2;
- iResult = send(ConnectSocket, (char*)&sum, sizeof(sum), 0);
- sum = 0;
- }
- if(counter%3==0)
- {
- char a = 0xDD;
- iResult = send(ConnectSocket,(char*)&a, sizeof(a), 0);
- }
- }
- } while (iResult != 0);
- }
- else
- {
- WSACleanup();
- return 1;
- }
- iResult = closesocket(ConnectSocket);
- if (iResult == SOCKET_ERROR) {
- wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- _getch();
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement