Is this a "typedef" problem?
This is the definition in one file.

typedef BYTE SOCKET;   //Socket descriptor

BYTE is defined elsewhere as type char.
(I made sure the file where SOCKET is assigned a type is included in the file the problem is in.)
This is one place it is used but my compiler is saying "BerkeleyTCPServerDemo.c:83:19: error: 'bsdServerSocket' undeclared (first use in this function)" in the following (line 83 equates to the third line in this excerpt)...

void BerkeleyTCPServerDemo(void)
{
    static SOCKET bsdServerSocket;
    static SOCKET ClientSock[MAX_CLIENT];
    struct sockaddr_in addr;
    struct sockaddr_in addRemote;
    int addrlen = sizeof(struct sockaddr_in);
    char bfr[15];
    int length;
    int i;
    static enum
    {
    BSD_INIT = 0,
        BSD_CREATE_SOCKET,
        BSD_BIND,
        BSD_LISTEN,
        BSD_OPERATION
    } BSDServerState = BSD_INIT;

    switch(BSDServerState)
    {
        case BSD_INIT:
            // Initialize all client socket handles so that we don't process
            // them in the BSD_OPERATION state
            for(i = 0; i < MAX_CLIENT; i++)
                ClientSock[i] = INVALID_SOCKET;

            BSDServerState = BSD_CREATE_SOCKET;
            // No break needed

        case BSD_CREATE_SOCKET:
            // Create a socket for this server to listen and accept connections on
            bsdServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if(bsdServerSocket == INVALID_SOCKET)
                return;

            BSDServerState = BSD_BIND;
            // No break needed

        case BSD_BIND:
            // Bind socket to a local port
            addr.sin_port = PORTNUM;
            addr.sin_addr.S_un.S_addr = IP_ADDR_ANY;
            if( bind( bsdServerSocket, (struct sockaddr*)&addr, addrlen ) == SOCKET_ERROR )
                return;

            BSDServerState = BSD_LISTEN;
            // No break needed

      case BSD_LISTEN:
            if(listen(bsdServerSocket, MAX_CLIENT) == 0)
                BSDServerState = BSD_OPERATION;

            // No break.  If listen() returns SOCKET_ERROR it could be because
            // MAX_CLIENT is set to too large of a backlog than can be handled
            // by the underlying TCP socket count (TCP_PURPOSE_BERKELEY_SERVER
            // type sockets in TCPIPConfig.h).  However, in this case, it is
            // possible that some of the backlog is still handleable, in which
            // case we should try to accept() connections anyway and proceed
            // with normal operation.

I thought this is where "bsdServerSocket" is declared.

(code is from Microchip.)

Recommended Answers

All 6 Replies

SOCKET shold be an int, not char because it's value can be a lot larger than what an char can hold.

The file that contains SOCKET declaration is probably not reachable to the *.c file or there could be an #ifdef around it.

Good to know, but, how big can SOCKET get? We have an option of "int8", "int16", "int32", or "int64". I know it's not int8 because that is type char. And should it be unsigned?

This is illegal unless you are working on an embedded system or a very low tech system that only works with 1 byte addresses:

typedef BYTE SOCKET;

And unless you are reinventing the wheel you should not be actually defining a typedef for SOCKET. Its defined in your socket library headers. In Windows its winsock2.h and linux its sys/socket.h. Find out which is right for your system. You are not responsible for defining this structure unless you are correcting a conflicting definition which a situation you want to avoid as much as you can for scalability sake.

Long story short just include the correct header. The compiler error you mentioned implies that you are not.

This is for an embedded system using a PIC32 processor. I get my processor specific questions answered on the Microchip forums but my coding questions get answered better here.

I would classify your question a platform specific issue since again the definition of SOCKET is not your responsability. As the programmer of the PIC32 and user of your compiler you need to have certain expectations of compliance with the standard libraries as perscribed by the C standard. If there are bugs with its functionality thats the platform's fault, not yours.

Unless of course you are not including the correct libraries as defined in your platform documentation. You could always try this and see what happens:

#include <socket.h>

Both the SOCKET definition and the program are from Microchip but their programs all supposedly work. I just figure I'm setting something up wrong. So, yes, this problem probably belong in the the Microchip forums. I'll ask it there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.