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(®msg, 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 *)®msg, 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); }



LinkBack URL
About LinkBacks


