Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // UDPClient.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <WS2tcpip.h>
- #include <iostream>
- #include <conio.h>
- #include <string>
- using namespace std;
- #pragma comment(lib,"ws2_32.lib")
- int _tmain(int argc, char* argv[])
- {
- WSADATA wsaData;
- WORD version = MAKEWORD(2,2);
- struct sockaddr_in remaddr; /* remote address */
- socklen_t addrlen = sizeof(remaddr);
- int wsOk = WSAStartup(version, &wsaData);
- if(wsOk != 0)
- {
- cout<<"Error Winsok sie nie uruchomil";
- return 0;
- }
- // Create a hint structure for the server
- sockaddr_in server;
- server.sin_family = AF_INET; // AF_INET = IPv4 addresses
- server.sin_port = htons(54000); // Little to big endian conversion
- inet_pton(AF_INET, "192.168.1.10", &server.sin_addr); // Convert from string to byte array
- // Socket creation, note that the socket type is datagram
- SOCKET out = socket(AF_INET, SOCK_DGRAM, 0);
- // Write out to that socket
- string s = "Dupa";
- int sendOk = sendto(out, s.c_str(), s.size() + 1, 0, (sockaddr*)&server, sizeof(server));
- if (sendOk == SOCKET_ERROR)
- {
- cout << "That didn't work! " << WSAGetLastError() << endl;
- }
- char *buf;
- for (;;) {
- int recvlen = recvfrom(out, buf, sizeof(buf), 0, (struct sockaddr *)&remaddr, &addrlen);
- printf("received %d bytes\n", recvlen);
- if (recvlen > 0) {
- buf[recvlen] = 0;
- printf("received message: \"%s\"\n", buf);
- }
- }
- // Close the socket
- closesocket(out);
- // Close down Winsock
- WSACleanup();
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement