Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2_Client.cpp : Defines the entry point for the console application.
- //
- // ConsoleApplication2.cpp : Defines the entry point for the console application.
- //
- #include <exception>
- #include <locale>
- #include "stdafx.h"
- #include <winsock2.h>
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctime>
- #pragma comment(lib,"WS2_32.lib")
- #define SERVER "127.0.0.1"
- #define PORT 8888
- #define BUFFLEN 512
- class SockEx
- {
- SockEx()
- {
- printf("Exception Patronus Sock Ex");
- }
- };
- int main()
- {
- int iResult = 0;
- char buf[BUFFLEN], message[BUFFLEN];
- SOCKET s; // zamiast socket
- int slen, recv_len;
- WSADATA wsa;
- struct sockaddr_in si_other = { 0 };
- slen = sizeof(si_other);
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf("Błąd wsadata KOD: %d", WSAGetLastError());
- getch();
- }
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR) // ew w miejsce zera dać IPPROTOUDP
- {
- printf("Błąd tworzenia gniazda KOD: %d", WSAGetLastError());
- getch();
- }
- // memset((char *)&si_other, 0, sizeof(si_other));
- si_other.sin_family = AF_INET;
- si_other.sin_addr.s_addr = inet_addr(SERVER);
- si_other.sin_port = htons(PORT);
- /*
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
- {
- printf("Błąd Bindowania KOD: %d", WSAGetLastError());
- getch();
- }
- */
- // puts("Bindowanie zakończone");
- while (true)
- {
- fflush(stdout);
- //memset ?
- printf("\nWprowadź wiadomość: ");
- int liczba;
- // scanf("%d", &liczba);
- //sprintf(message, "%d", liczba);
- gets_s(message, 511);
- bool popr = false;
- for (int i = 0; i < strlen(message); i++)
- {
- if (isdigit(message[i]) == false)
- {
- printf("JESTEŚ DEBILEM");
- popr = true;
- getch();
- break;
- }
- }
- if (popr) continue;
- liczba = atoi(message);
- printf("\n<TEST> WPROWADZIŁEŚ: %s", message);
- //scanf("%s", &message);
- /*
- if ((recv_len = recvfrom(s, buf, BUFFLEN, 0, (sockaddr *)&si_other, &slen)) == SOCKET_ERROR)
- {
- printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
- getch();
- exit(EXIT_FAILURE);
- }
- printf("Odebrano pakiet danych z %s : %d \n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
- printf("Dane %s\n", buf);
- */
- /* if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("Błąd Sendto <while> KOD: %d", WSAGetLastError());
- getch();
- exit(EXIT_FAILURE);
- }*/
- /* iResult = sendto(s, message, strlen(message), 0, (SOCKADDR *)&si_other, slen);
- if (iResult == SOCKET_ERROR) {
- printf("Bład wysyłania: %d", WSAGetLastError());
- _getch();
- return 1;
- }*/
- ////////////////////////////////////////////////////////////////////////////////////////
- int licz = 563;
- int ibuf = 0;
- try
- {
- iResult = sendto(s, (char*)&liczba, sizeof(liczba), 0, (SOCKADDR *)&si_other, slen);
- if (iResult == SOCKET_ERROR)
- {
- /*printf("Bład wysyłania: %d", WSAGetLastError());
- _getch();
- return 1;*/
- }
- }
- catch (int eror)
- {
- printf("Błąd SENDTO <while> TO JEST CATCH KOD: %d", WSAGetLastError());
- getch();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- /* if ((recv_len = recvfrom(s, buf, BUFFLEN, 0, (SOCKADDR *)&si_other, &slen)) == SOCKET_ERROR)
- {
- printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
- getch();
- exit(EXIT_FAILURE);
- }*/
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- try {
- if ((recv_len = recvfrom(s, (char*)&ibuf, sizeof(ibuf), 0, (SOCKADDR *)&si_other, &slen)) == SOCKET_ERROR)
- {
- //printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
- throw WSAGetLastError();
- // getch();
- // exit(EXIT_FAILURE);
- }
- }
- catch (int err)
- {
- printf("Błąd recv_len <while> TO JEST CATCH KOD: %d", WSAGetLastError());
- getch();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // buf[recv_len] = '\0';
- printf("Otrzymano: %d", ibuf);
- // printf("\nDane: [%s] IP: %s PORT: %d", buf, inet_ntoa(si_other.sin_addr), si_other.sin_port);
- /* if (atoi(buf) <1401 || atoi(buf) < 1)
- {
- }
- */
- _getch();
- }
- closesocket(s);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement