#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "1337"
using namespace std;
void main()
{
WSADATA wsaData;
SOCKET ListenSocket = INVALID_SOCKET,
ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
hints;
char recvbuf[DEFAULT_BUFLEN];
char *sendbuf;
char hoststorage[32];
int iResult, iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
int hold = 0;
char buffer[512] = {1, 2, 3, 4, 5, 6, 7, 8};
sockaddr_in myaddr;
int addrLen = sizeof(sockaddr_in);
int i;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
cin>>hold;
return;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, "14597", &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed: %d\n", iResult);
cin>>hold;
WSACleanup();
return;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
cin>>hold;
freeaddrinfo(result);
WSACleanup();
return;
}
// Allocate address structure and set data
sockaddr_in addr, remoteaddr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1337);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_port = htons(1337);
remoteaddr.sin_addr.s_addr = inet_addr("72.x.x.x");
//Set socket to non-blocking mode.
u_long option = 1;
ioctlsocket(ListenSocket, FIONBIO, &option);
connect(ListenSocket, (SOCKADDR*)&remoteaddr, addrLen);
// Send to server
send(ListenSocket, buffer, 8, 0);
printf("Done sending data.");
cin>>hold;
closesocket(ListenSocket);
WSACleanup();
cin>>hold;
printf("Finished.\n");
}
Benjamin, I think I'm starting to get a hold of winsock commands. This is a UDP Client that I carved out of your example and made to go with a Server that is nearly identical to your example. Can you help me figure out why this code works with IP: 127.0.0.1 and yet when I change it to my internet IP, the server doesn't recieve the data? I thought the problem was my router, but I've got port forwarding for UDP packets on ports 1-65000 set up to be forwarded to this computer. So is there anything else in my code that might be messing this up? The Client is up top, and I've included the server below. Thanks again for all your help!
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27016"
using namespace std;
void main()
{
WSADATA wsaData;
SOCKET ListenSocket = INVALID_SOCKET,
ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
hints;
char recvbuf[DEFAULT_BUFLEN];
char *sendbuf;
char hoststorage[32];
int iResult, iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
int hold = 0;
char buffer[512] = {1, 2, 3, 4, 5, 6, 7, 8};
sockaddr_in myaddr;
int addrLen = sizeof(sockaddr_in);
int i;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
cin>>hold;
return;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, "14597", &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed: %d\n", iResult);
cin>>hold;
WSACleanup();
return;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
cin>>hold;
freeaddrinfo(result);
WSACleanup();
return;
}
// Allocate address structure and set data
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1337);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Bind the socket to address
if (bind(ListenSocket, (SOCKADDR*)&addr, sizeof(sockaddr_in)) == SOCKET_ERROR) {
printf("Failed to bind, error: %i.n", WSAGetLastError());
cin>>hold;
return;
}
//Set socket to non-blocking mode.
u_long option = 1;
ioctlsocket(ListenSocket, FIONBIO, &option);
// Listening loop
while(iResult < 1)
{
// Block, waiting for data
//iResult = recvfrom(ListenSocket, buffer, 8, 0, (SOCKADDR*)&addr, &addrLen);
iResult = recv(ListenSocket, buffer, 8, 0);
/*if (WSAGetLastError() == WSAEWOULDBLOCK) {
printf("Socket Failed to Block.");
WSACleanup();
break;
}*/
// Output IP:port
if(iResult > 0)
{
printf("%i.%i.%i.%i:%hu : ", addr.sin_addr.S_un.S_un_b.s_b1,
addr.sin_addr.S_un.S_un_b.s_b2, addr.sin_addr.S_un.S_un_b.s_b3,
addr.sin_addr.S_un.S_un_b.s_b4, ntohs(addr.sin_port));
}
// Output data in hex
if(iResult > 0)
{
for (int x = 0; x<iResult; x++)
printf("%.2X ", (unsigned char) buffer[x]);
}
}
closesocket(ListenSocket);
WSACleanup();
cin>>hold;
printf("Finished.\n");
}