i have 2 questions that i'm sure are easy to answer but i can't find the answer anywhere. I'm trying to write a program that will sent information though a UDP datagram from port 2006 on the source computer to port 2007 on the destination computer. i cannot figure out how to set the source port to 2006. i got the destination port to work.
second in the data part of the datagram i need it to be padded in front and in back with hex '00'.
here is my code
Code:
#include <ws2tcpip.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
#include <sys\types.h>
using namespace std;
#define MDYN2 "10.0.1.20" //IP address for MDYN2 computer
#define OPCON "10.0.1.99" //IP address for OPCON computer
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//main program: this program creates a connection between this computer (should be OPCON) and MDYN2 (10.0.1.20). it then
// sends a datagram to MDYN2 containg the ":cabsave" command to try and get MDYN2 to save information as if it
// was comming form the OpCon program on OPCON computer.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0),&wsaData) != 0)
{
fprintf(stderr,"WSAStartup()failed");
exit(1);
}
struct addrinfo hints; //used to help fill in results
struct addrinfo* results; //where the adder info goes (linked list)
// struct addrinfo* pointer; //points to current result in linked list. might need this...
int status; //status of all the functions that return a status (reused multiple times)
int sock; //socket
char* message; //the message that will be sent
message = ":cabsave saveFromProgram";
//intilize hints first set to 0 then fill
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; //IPv4 only (computer is too old for IPv6)
hints.ai_socktype = SOCK_DGRAM; //UDP connection (or lack there of)
hints.ai_flags = AI_PASSIVE; //sets ip to this computer (should be on Opcon: 10.0.1.99 (94?))
//sets results, checkes status, and tell you if it doesn't work all on one line.
if((status = getaddrinfo(MDYN2, "2007", &hints, &results)) != 0)
{
printf("getaddrinfo: %s\n", gai_strerror(status));
system("pause");
return 0;
}
//
//might need to shearch thought the linked list; though, i dout it.
//
//set socket and connect
sock = socket(results->ai_family, results->ai_socktype, results->ai_protocol);
//bind(sock, results->ai_addr, results->ai_addrlen);
status = sendto(sock, message, strlen(message), 0, results->ai_addr, sizeof(*results->ai_addr));
//connect is recived at a spcific socket. (mightnot need this eather...
//connect(sock, results->ai_addr, results->ai_addrlen);
//bind (might not have to to this) sends though a spcific socket
status = shutdown(sock, 2);
printf("program completed\n");
system("pause");
return 0;
}