在其他源文件

时间:2016-05-31 20:45:22

标签: c sockets struct tcp

对于学校项目,我制作了这个程序,其中客户端(tcpsock_t)可以与服务器建立TCP连接并通过它传递数据。我轮询不同的连接,看看是否有数据要读取。

我有一个tcpsock.c,其中包含用作库的TCP套接字的函数,作为服务器的connmgr.c和客户端代码(不相关)。

在tcpsock.h中,我输入tcpsock_t的结构 (以下其他功能)

typedef struct tcpsock tcpsock_t;

并在tcpsock.c中我声明了结构(它应该留在这里,我不能把它放在de .h文件中)

struct tcpsock{
 long cookie;
 int sd;
 char * ip_addr;
 int port;
 };

在connmgr.c中我想创建一个这个tcpsock_t的数组来存储我所有连接的客户端,但是当我编译它时总会出现同样的错误:

  

错误:无效使用未定义类型'struct tcpsock'        客户[conn_counter] = * requestclient;

有人可以告诉我我做错了吗?

非常感谢!

这是我的服务器代码:

#include "tcpsock.h" 
#include "connmgr.h"       
int conn_counter = 0;

    void add_client(tcpsock_t *newclient, tcpsock_t * array){
        conn_counter++;
        array= realloc(test,sizeof(get_size_tcpsock())*conn_counter);
        array[conn_counter-1]=*newclient; //here i have the error

    }

    void add_poll(struct pollfd * polllist, tcpsock_t *client, tcpsock_t *server){
        polllist = realloc(polllist,sizeof(struct pollfd)*conn_counter+1);
        int clientsd;
        tcp_get_sd(server, &clientsd);
        polllist[conn_counter].fd= clientsd;
        polllist[conn_counter].events = POLLIN;

    }


    int main(void){

        tcpsock_t *server;
        tcpsock_t *client;
        client = malloc(sizeof(get_size_tcpsock()));
        struct pollfd *poll_list;
        poll_list = malloc(sizeof(struct pollfd));
        sensor_data_t data;

        int bytes,result;

        if(tcp_passive_open(&server,PORT)!=TCP_NO_ERROR)exit(EXIT_FAILURE);
        int serversd;
        tcp_get_sd(server, &serversd);
        poll_list[0].fd = serversd;
        poll_list[0].events = POLLIN;

        printf("start polling \n");

        while(1){

            int retval,i;
            retval = poll(poll_list,conn_counter+1,5000);
            if (retval>0){
                if(poll_list[0].revents && POLLIN>0){
                    tcpsock_t *requestclient;
                    tcp_wait_for_connection(server,&requestclient);
                client[conn_counter] = *requestclient; //here i have the error
                    add_client(requestclient,client);
                    add_poll(poll_list, requestclient, server);

                }
                for (i=0; i<conn_counter+1;i++){
                    if(poll_list[i+1].revents & POLLIN){

                        bytes = sizeof(data.id);
                        result = tcp_receive(client[i],(void *)&data.id,&bytes); //here i have the error

                        bytes = sizeof(data.value);
                        result = tcp_receive(client[i],(void *)&data.value,&bytes); //here i have the error

                        bytes = sizeof(data.ts);
                        result =tcp_receive(client[i],(void *)&data.ts,&bytes); //here i have the error
                        if ((result==TCP_NO_ERROR) && bytes){

                            printf("sensor id = %" PRIu16 " - temperature = %g - timestamp = %ld\n", data.id, data.value, (long int)data.ts);

                                }

                        fflush(stdout);
                    }
                    if (poll_list[i+1].revents & POLLHUP){
                        printf("client disconnected \n");
                        poll_list[conn_counter+1].fd=-1;
                        poll_list[conn_counter+1].events=0;
                        fflush(stdout);
                    }
                }       
            }

        }



        tcp_close(&server);
        return 1;
    }

这是tcpsock.h,但功能正常。

typedef struct tcpsock tcpsock_t;

int get_size_tcpsock();

int tcp_passive_open(tcpsock_t ** socket, int port);
/* Creates a new socket and opens this socket in 'passive listening mode' (waiting for an active connection setup request) 
 * The socket is bound to port number 'port' and to any active IP interface of the system
 */

int tcp_active_open(tcpsock_t ** socket, int remote_port, char * remote_ip);
/* Creates a new TCP socket and opens a TCP connection to the system with IP address 'remote_ip' on port 'remote_port'
 * The newly created socket is return as '*socket'
 */

int tcp_close(tcpsock_t ** socket); 

int tcp_wait_for_connection(tcpsock_t * socket, tcpsock_t ** new_socket); 
/* Puts the socket 'socket' in a blocking wait mode
 * Returns when an incoming TCP connection setup request is received
 * A newly created socket identifying the remote system that initiated the connection request is returned as '*new_socket'
 */

int tcp_send(tcpsock_t * socket, void * buffer, int * buf_size );
/* Initiates a send command on the socket 'socket' and tries to send the total '*buf_size' bytes of data in 'buffer' (recall that the function might block for a while)
 * The function sets '*buf_size' to the number of bytes that were really sent, which might be less than the initial '*buf_size'
 */

int tcp_get_ip_addr( tcpsock_t * socket, char ** ip_addr);
/* Set '*ip_addr' to the IP address of 'socket' (could be NULL if the IP address is not set)
 */

int tcp_get_port(tcpsock_t * socket, int * port); 

int tcp_get_sd(tcpsock_t * socket, int * sd); 

0 个答案:

没有答案