Thread: network programming

  1. #1
    Registered User
    Join Date
    Sep 2008

    network programming

    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
    #include <ws2tcpip.h>
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <winsock.h>
    #include <sys\types.h>
    using namespace std;
    #define MDYN2 ""	//IP address for MDYN2 computer
    #define OPCON ""	//IP address for OPCON computer
    //main program:	this program creates a connection between this computer (should be OPCON) and MDYN2 ( 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)
        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: (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));
    		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");
    	return 0;

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Generally speaking from what I have seen which isn't much sockets are bidirectional. Now you could do something like once you have a connection then start a new connection going back to the ip that just connected to you on your 2nd port. I think there is a flag in winsock that makes a socket unidirectional, but I don't know it off the top of my head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c network programming
    By paoloC in forum C Programming
    Replies: 1
    Last Post: 01-23-2010, 12:01 PM
  2. network programming
    By kishorkundan in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-22-2005, 12:54 PM
  3. Network programming
    By CompiledMonkey in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 09:45 PM
  4. HOW TO: Network Programming in C++
    By govi in forum Windows Programming
    Replies: 7
    Last Post: 08-06-2002, 06:02 AM
  5. Want Some Help on Network Programming..
    By Ankit_mcg in forum C Programming
    Replies: 2
    Last Post: 04-17-2002, 12:40 PM
Website Security Test