Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Defines the entry point for the console application.
- //
- #include <time.h>
- #include <locale.h>
- #include "stdafx.h"
- #include <winsock2.h>
- #include <conio.h>
- #include <stdio.h>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #pragma comment(lib,"WS2_32.lib")
- #define PORT 8888
- #define BUFFLEN 512
- int main()
- {
- srand(time(NULL));
- char buf[BUFFLEN];
- char buf2[BUFFLEN]= "TEST!";
- SOCKET s;
- int slen, recv_len;
- WSADATA wsa;
- struct sockaddr_in server, si_other;
- 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)) == INVALID_SOCKET) // ew w miejsce zera dać IPPROTOUDP
- {
- printf("Błąd tworzenia gniazda KOD: %d", WSAGetLastError());
- getch();
- }
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.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");
- int iResult = 0;
- int ibuf=0;
- int ran = 0;
- while (true)
- {
- printf("oczekiwanie na dane: ");
- fflush(stdout);
- //memset ?
- /*double liczba = rand() % 1401;
- sprintf(buf2, "%f",liczba);*/
- /*
- int liczba = rand() % 3871;
- sprintf(buf2, "%d", liczba);*/
- /* 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);
- }
- */
- 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());
- getch();
- }
- /* buf[recv_len] = '\0';
- printf("\nOdebrano pakiet danych z %s : %d \n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
- printf("\nDane %s\n", buf);*/
- printf("\nDane: %d\n", ibuf);
- /* iResult = sendto(s, buf2, strlen(buf2), 0, (SOCKADDR *)&si_other, slen);
- if (iResult == SOCKET_ERROR) {
- printf("Bład wysyłania: %d", WSAGetLastError());
- _getch();
- return 1;
- }*/
- ran = rand() % 65443 + 4;
- iResult = sendto(s, (char*)&ran, sizeof(ran), 0, (SOCKADDR *)&si_other, slen);
- if (iResult == SOCKET_ERROR) {
- printf("Bład wysyłania: %d", WSAGetLastError());
- _getch();
- return 1;
- }
- } // koniec while
- closesocket(s);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement