Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <winsock.h>
- #pragma comment(lib, "Ws2_32.lib")
- #include <conio.h>
- #include <locale.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include <string.h>
- #include <string>
- #include <time.h>
- using namespace std;
- int main() {
- //formalności
- setlocale(LC_ALL, "Polish");
- WSADATA wsadata;
- WSAStartup(MAKEWORD(2, 2), &wsadata);
- srand(time(NULL));
- //zmienne, bufor itd.
- SOCKET recvsocket = socket(AF_INET, SOCK_DGRAM, 0);
- sockaddr_in recvaddr = { 0 };
- recvaddr.sin_family = AF_INET;
- recvaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- recvaddr.sin_port = htons(6666);
- int size = sizeof(recvaddr);
- int iResult;
- char buff[128] ;
- float rnd=0;
- int sendint = 0;
- //program
- iResult = bind(recvsocket, (SOCKADDR*)&recvaddr, sizeof(recvaddr));
- if (iResult == SOCKET_ERROR) {
- printf("coś się spierdoliło z bindowaniem");
- }
- for (;;) {
- iResult = recvfrom(recvsocket, buff, sizeof(buff), 0, (SOCKADDR*)&recvaddr, &size);
- if (iResult == SOCKET_ERROR) continue;
- else {
- buff[iResult] = 0;
- puts(buff);
- puts("odebrano wiadomość"); //odbiera
- //strcat(buff, "-odbrano");
- sendint = strlen(buff);
- iResult = sendto(recvsocket, (char*)&sendint, sizeof(sendint), 0, (SOCKADDR*)&recvaddr, sizeof(recvaddr));
- if (iResult == SOCKET_ERROR) {
- printf("błąd wysyłki: %d", WSAGetLastError());
- _getch();
- return 1;
- }
- else puts("no coś wysłano");
- }
- }
- //formalności
- shutdown(recvsocket, 2);
- closesocket(recvsocket);
- //shutdown(sendsocket, 2);
- //closesocket(sendsocket);
- WSACleanup();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement