Thread: Socket Programming Help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Socket Programming Help

    Hi im trying to write client code for UDP Socket Programming and havin trouble trying to receive a msg and get it back into a struct. the Errors i'm getting are:

    UDPclient.c:171: error: request for member ‘err’ in something not a structure or union
    UDPclient.c:175: error: request for member ‘errStr’ in something not a structure or union
    UDPclient.c:178: error: request for member ‘x’ in something not a structure or union
    UDPclient.c:179: error: request for member ‘y’ in something not a structure or union
    UDPclient.c:180: error: request for member ‘temp’ in something not a structure or union
    UDPclient.c:181: error: request for member ‘tLeft’ in something not a structure or union
    UDPclient.c:182: error: request for member ‘mLeft’ in something not a structure or union
    UDPclient.c:202: error: request for member ‘mLeft’ in something not a structure or union
    UDPclient.c:203: error: request for member ‘tLeft’ in something not a structure or union

    these are towards the bottom of the code in the receiving response section of the code

    Code:
    #include <stdio.h>      
    #include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
    #include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
    #include <stdlib.h>     /* for atoi() and exit() */
    #include <string.h>     /* for memset() */
    #include <unistd.h>     /* for close() */
    #include <strings.h>
    #include <time.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <math.h>
    #include <errno.h>
    #include <assert.h>
    
    #define ECHOMAX 255     /* Longest string to echo */
    
    #define SERV_PORT    31180      /* server UDP port */
    #define SERV_IP		 "149.171.37.233" /* server IP Address */
    #define GRID_SIZE    100        /* 100 x 100 grid */
    #define PASSWD       "thunt10"  /* password in each packet */
    
    #define MAX_MSG_LEN  64        /* max size of any msg */
    #define MAX_TIME     120       /* time (seconds) limit for a game */
    #define MAX_MOVES    100       /* mximum number of moves allowed */
    
    #define MSG_REGISTER    1
    #define MSG_MOVE        2
    
    typedef struct {
      char passwd[8];              /* password */
      unsigned long mType;         /* message type = MSG_REGISTER */
      unsigned long id;            /* student id */
      unsigned short x, y;         /* x and y co-ordinates of starting point */
    } RegMsg_t;
    
    typedef struct {
      char passwd[8];              /* password */
      unsigned long mType;         /* message type = MSG_MOVE */
      unsigned long id;            /* student id */
      char dir;                    /* direction: N, S, E, or W */
      char pad[3];                 /* padding for word-alignment */
    } MoveMsg_t;
    
    typedef struct {
      unsigned long err;           /* error code: 0 if all ok */
      unsigned short x, y;         /* x and y co-ordinates of current location */
      long temp;                   /* temperature */
      unsigned long tLeft;         /* time left */
      unsigned long mLeft;         /* moves left */
      char errStr[108];            /* error string */
    } RespMsg_t;
    
    int main(int argc, char *argv[]) {
    	int sock;                        /* Socket descriptor */
    	int e;
    	long err;
    	int inMsgLen;
    	//int outMsgLen;
    
    	char passwd[8];
    	long temp, tleft, mleft;
    	unsigned short x, y;
    	unsigned long mType, id;
    
    	//long ttaken, mtaken;
    
    	//char dir;
    	//char pad[3];
    
    	char inMsg[MAX_MSG_LEN];
    	//char outMsg[MAX_MSG_LEN];
    
        struct sockaddr_in servAddr;	 /* Echo server address */
        struct sockaddr_in fromAddr;     /* Source address of echo */
    
    	RegMsg_t regmsg;			 	 /* Registration Message */
    	RespMsg_t *respmsg;				 /* Response Message from the Server */
    	//MoveMsg_t movmsg;				 /* Move Message */
    
        //unsigned short echoServPort;     /* Echo server port */
     
        unsigned int fromSize;           /* In-out of address size for recvfrom() */
    
    	unsigned short servPort = 31180; /* Server UDP port number */
    
        char *servIP = "149.171.37.233"; /* IP address of server */
    
        //char *echoString;                /* String to send to echo server */
        //char echoBuffer[ECHOMAX+1];      /* Buffer for receiving echoed string */
    
    	int regmsgLen;					 /* Length of Registration message */
    
        //int echoStringLen;               /* Length of string to echo */
    
        //int respStringLen;               /* Length of received response */
    
    
    
    	/* GETTING REGISTER-MESSAGE PACKAGED & SENT */
        if ((argc < 5) || (argc > 6)) {	  /* Test for correct number of arguments */
        	printf("Please enter <password> <message-type> <student-id> <x-location> <y-location>\n");
    
            exit(1);
        }
    
    	/* Scanning data from user input */
    	strcpy(passwd, argv[1]);    /* First arg: password "thunt10" */
    	mType = atoi(argv[2]);		/* Second arg: message-type */
    	id = atoi(argv[3]);			/* Third arg: student-id */
    	x = atoi(argv[4]);			/* Fourth arg: x-coordinate */
    	y = atoi(argv[5]);			/* Fifth arg: y-coordinate */
    
        //servIP = argv[1];
        //echoString = argv[2];
    
    	regmsgLen = sizeof(regmsg);
    
        //if ((echoStringLen = strlen(echoString)) > ECHOMAX)  /* Check input length */
            //printf("Echo word too long");
    
        //if (argc == 4)
            //echoServPort = atoi(argv[3]);  /* Use given port, if any */
        //else
            //echoServPort = 7;  /* 7 is the well-known port for the echo service */
    	
        /* Create a datagram/UDP socket */
        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
            printf("socket() failed");
    	}
    
        /* Construct the server address structure */
        memset(&servAddr, 0, sizeof(servAddr));    		/* Zero out structure */
        servAddr.sin_family = AF_INET;                 	/* Internet addr family */
        servAddr.sin_addr.s_addr = inet_addr(servIP);   /* Server IP address */
        servAddr.sin_port   = htons(servPort);     		/* Server port */
    
    	memset(&regmsg, 0, regmsgLen);
    	strcpy(regmsg.passwd, passwd);
    	regmsg.mType = htonl(mType);
    	regmsg.id = htonl(mType);
    	regmsg.x = htons(x);
    	regmsg.y = htons(y); 
    
        /* Send the string to the server */
        if ((e = sendto(sock, (char *)&regmsg, regmsgLen, 0, (struct sockaddr *)&servAddr, sizeof(servAddr))) != regmsgLen) {
            printf("sendto() sent a different number of bytes than expected");
    	}
      	else if (e < 0) {
    		printf("Error with sending\n");
    		exit(1);
    	}
    
    
        /* RECEIVING A RESPONSE MESSAGE AFTER REGISTRATION */
        fromSize = sizeof(fromAddr);
        inMsgLen = recvfrom(sock, inMsg, MAX_MSG_LEN, 0, (struct sockaddr *)&fromAddr, &fromSize);
    
        if (servAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) {
            fprintf(stderr,"Error: received a packet from unknown source.\n");
            exit(1);
        }
    
    	respmsg = (RespMsg_t *)inMsg;
    	err = ntohl(respmsg.err);
    
    	if (err != 0) {
            printf("Registration failed.\n");
    		printf("%s\n", respmsg.errStr);
    	}
    	else {
    		x = ntohs(respmsg.x);
    		y = ntohs(respmsg.y);
    		temp = ntohl(respmsg.temp);
    		tleft = ntohl(respmsg.tLeft);
    		mleft = ntohl(respmsg.mLeft);
    
    		printf("SERVER RESPONSE:\n");
    		printf("x-location = %d\ty-location = %d\n", x, y);
    		printf("temperature = %ld\n", temp);
    		printf("time-left = %ld\n", tleft);
    		printf("moves-left = %ld\n", mleft);
    	}

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    27
    You are using DOT thats causing you problem. Since you created "respmsg" dynamically. So you'd need "->" to use its members.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    thanks buddy

    so whats the general rule for '->' or '.'

    sorry not really sure what u mean by dynamic

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    27
    Well I would suggest to read your book.
    For pointers (*) they are allocated at run time i.e. dynamically. We use "->" for it.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why repost the same question in a new thread 15 min after the first thread? I already answered your question, and the error the compiler gives you tells you exactly what the problem is.

    http://cboard.cprogramming.com/c-pro...ure-union.html

    This has nothing to do with socket programming per se.
    Last edited by Subsonics; 05-18-2010 at 05:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  2. Problem with socket descriptors
    By McKracken in forum C Programming
    Replies: 1
    Last Post: 07-22-2009, 08:51 AM
  3. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM