Thread: Simple sockets - sending error

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Simple sockets - sending error

    Hi all,
    I have set up this small program to accept udp broadcasts and redirect them to another IP (it will eventually be set up to re-broadcast)

    I am able to get the message, but i am getting a -1 error when sending.

    I'm at my wits end trying to figure out this problem I have, but i figure i'm just missing something easy ....
    The program compiles fine (using Microsoft Visual c++ 6.0)

    If someone could please point out my error I would be eternally grateful !
    Thanks !

    Code:
    #include "C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\stdafx.h"
    #include <stdio.h>						// For perror() call
    #include <stdlib.h>						// For exit() call
    #include <windows.h>
    #include <winsock.h>					// Include Winsock Library
    #pragma comment (lib, "wsock32.lib")	// Visual C++ -> Include library
    
    #ifndef HAVE_ZERO
    #define bzero(ptr,n)	memset(ptr,0,n)	// to initalize client and server structures
    #define SA struct sockaddr
    #define SB struct sockaddr
    #endif
    
    #define CLIIP "205.207.115.132"			//Testing - ip address of DESTINATION
    //#define CLIIP "205.207.115.255"			    //Testing - broadcast ip address
    
    int main(int argc, char**argv) {
    
    	int sockfd, socksd;					// Handle to sockets
    	struct servent *sp=NULL;			// Pointer to servent structure
    	struct sockaddr_in cliaddr, servaddr;
    	char mesg[1000];						   // Message buffer for incoming message
    	char tmesg[1000]="MISC     XXX     XXX     HELLO WORLD"; // Outgoing test message
    	typedef int socklen_t;
    	socklen_t len, clen;
    	long n, m=0;
    
    	// ***** socket option vars ***
    	BOOL bOptVal = TRUE;
    	int bOptLen = sizeof(BOOL);
    	int iOptLen = sizeof(int);
    	// ****************************
    
    	WSADATA wsaData;					// Windows socket
    
    //INITIALIZE WINSOCK
    	if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
    		perror("connection Error.");
    		exit(EXIT_FAILURE);
    	}
    	
    
    //OPEN SOCKET - gather server info and bind for receive.
    		if ((sp=getservbyname("monitorudp",NULL))==NULL) {
    			printf("error %d", WSAGetLastError());
    		}
    
    		sockfd=socket(AF_INET, SOCK_DGRAM, 0);
    		if (sockfd<0) {
    			perror("cannot open socket ");
    			exit(EXIT_FAILURE);
    		}
    		bzero(&servaddr.sin_addr, sizeof(servaddr)); //initialize server structure
    		servaddr.sin_family=AF_INET;
    		servaddr.sin_port=sp->s_port;
    		
    		if(bind (sockfd, (SA*) &servaddr, sizeof(servaddr))<0) perror("ERROR on binding");
    
    //OPEN SOCKET and fill in client structure for send.
    		
    		socksd=socket(AF_INET, SOCK_DGRAM, 0);
    		if (socksd<0) {
    			perror("cannot open socket ");
    			exit(EXIT_FAILURE);
    		}
    
    		bzero(&cliaddr.sin_addr, sizeof(cliaddr)); //initialize client structure
    		cliaddr.sin_family=AF_INET;
    		//cliaddr.sin_port=htons(10001);
    		cliaddr.sin_port=0;
    		cliaddr.sin_addr.s_addr = inet_addr(CLIIP);
    
      if (setsockopt(socksd, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != SOCKET_ERROR) {
        printf("Set SO_BROADCAST: ON\n");
      }
    
    //LOOP ->RECEIVE DATAGRAMS FROM SERVER, AND OUTPUT TO CLIENTS (will be changed to BROADCAST)
    		len=sizeof(servaddr);
    		clen=sizeof(cliaddr);
    		for (;;){
    
    		n=recvfrom(sockfd, mesg, 1000, 0, (SA*) &servaddr, &len);
    
    		if (n<0) {
    			perror("receive failed");
    			exit(EXIT_FAILURE);
    		}
    		else if (n>0) {
    			mesg[n]='\0'; // NULL terminate
    			printf("received %s\n",mesg);
    			printf("received %d\n", n);
    		}
    
    		m=sendto(socksd, tmesg, n, 0,(SB*) &cliaddr, sizeof(clen));
    		if (m<0) {
    			perror("send failed");
    			//exit(EXIT_FAILURE); commented out for testing
    		}
    		else if (m>0) {
    			tmesg[m]='\0';	// add NULL terminate
    			printf("sending %s\n", tmesg);
    			printf("sent %d\n", m);
    		}
    	}	
    //CLOSE SOCKET
    	if(closesocket(sockfd)<0) perror("ERROR on closing");
    
    //CLEAN UP WINSOCK
    	if(WSACleanup()==NULL) {
    		printf("error %d", WSAGetLastError());
    	}
    //END PROGRAM
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include "C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\stdafx.h"
    You don't need this line

    > perror("send failed");
    You'll get a lot more information from calling GetLastError(), as described in the MSDN help.
    perror() only works on libc API calls.

    > n=recvfrom(sockfd, mesg, 1000, 0, (SA*) &servaddr, &len);
    Use sizeof(mesg)-1 rather than 1000, otherwise there is no room for the \0 if the buffer is filled to capacity.

    > m=sendto(socksd, tmesg, n, 0,(SB*) &cliaddr, sizeof(clen));
    What is the value of 'n' here?
    What has sizeof(clen) got to do with any other parameter?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Hello, and thank you for your input.
    >I have removed the #include - thank you

    >I have replaced all the perror() with WSAGetLastError() - thank you

    >I have replaced integer with sizeof() - thank you

    >the value of n would have been the return value from the recvfrom() - I replaced that with sizeof()
    sizeof(clen) is the size of the (cliaddr) defined above, a needed param in the sendto function.

    I'm receiving "referenced memory" errors from the sendto() function. Am I referencing the buffer wrong that i am receiving from the recvfrom() function ?

    Code:
    		len=sizeof(servaddr);
    		clen=sizeof(cliaddr);
    		for (;;){
    
    		n=recvfrom(sockfd, mesg, sizeof(mesg), 0, (SA*) &servaddr, &len);
    
    		if (n<0) {
    			printf("receive failed &#37;s\n", WSAGetLastError());
    			exit(EXIT_FAILURE);
    		}
    		else if (n>0) {
    			mesg[n]='\0'; // NULL terminate
    			printf("received %s\n",mesg);
    			printf("received %d\n", n);
    		}
    
    		//m=sendto(socksd, tmesg, n, 0,(SB*) &cliaddr, sizeof(clen));
    		m=sendto(socksd, mesg, sizeof(mesg), 0,(SB*) &cliaddr, sizeof(clen));
    		if (m<0) {
    			printf("send failed %s\n", WSAGetLastError());
    			exit(EXIT_FAILURE);
    		}
    		else if (m>0) {
    			tmesg[m]='\0';	// add NULL terminate
    			printf("sending %s\n", tmesg);
    			printf("sent %d\n", m);
    		}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sizeof(clen) is the size of the (cliaddr) defined above
    Then the last parameter should be clen itself, not the sizeof(clen).

    What you should be trying to say is how big the previous param is,
    (SB*) &cliaddr, sizeof(cliaddr) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Thank you very much *sigh i knew i did something very dumb.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM