2015-11-20 108 views
0

我想爲我的覆盆子pi編寫一個程序,它將系統時間從同一網絡上的GPS單元更改爲時間。 GPS通過端口3000發送一個72字節的UDP數據包。我是套接字編程的新手,所以我不確定哪裏出錯。Raspbian C++錯誤:'結構'之前預期的主要表達式

我遇到的麻煩是我似乎無法用g ++編譯它。我收到以下錯誤:

enter image description here

所以今天的主要錯誤似乎是在該行

char A = struct sockaddr_in address; 

這是我的計劃的開始和在那裏我創建的插座,其中,該方法該錯誤位於,如果你想我的程序的主要方法,那麼我也將它添加。

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <time.h> 
#include <sys/time.h> 
#include <errno.h> 
#include <math.h> 

// defines the socket used by the GPS 
#define PORT 3000 

/****************************/ 
int CreateSocket(int port) 
/****************************/ 
{ 
    // Create an UDP-socket 
    int sock = socket(AF_INET, SOCK_DGRAM, 0); 

    // Check if UDP-socket was created 
    if(sock==-1) 
    { 
     fprintf(stderr, "1CreateSocket: socket failed\n"); 
     return -1; 
    } 

    // Bind it to the local IP-address 
    struct sockaddr_in address; 
    char A = struct sockaddr_in address; 
    fprintf(stderr, A); 

    // Pointer to the block of memory to fill with address data 
    memset(&address, 0, sizeof(address)); 

    address.sin_family  = AF_INET;    // Address family for IP-address 
    address.sin_addr.s_addr = htonl(INADDR_ANY); // converts the unsigned integer hostlong from host byte order to network byte order 
    address.sin_port  = htons(port);   // converts the unsigned short integer hostshort from host byte order to network byte order 

    // Check if IP-address is correct, if not Socket failed. Otherwise it returns the socket 
    if(bind(sock, (struct sockaddr *) &address, sizeof(address))==-1) 
    { 
     fprintf(stderr, "2CreateSocket: bind failed\n"); 
     close(sock); 
     return -1; 
    } 
    return sock; 
} 

任何人都可以在這裏看到任何明顯的錯誤?由於

+0

我很困惑;這條線甚至應該完成什麼? – zenzelezz

回答

0

你並不真的需要這兩條線:

char A = struct sockaddr_in address; 
fprintf(stderr, A); 

您可以刪除它們,因爲他們沒有做任何有用的事情,他們有一個語法錯誤。

爲了做一些額外的清理,上面那些可以刪除的行的綁定的註釋實際上應該高於對bind()的調用。

相關問題