# FreeBSD, eventlib, and sockets



## jake_conhale (Jan 22, 2009)

I believe this is the right forum for this, after all socket communication is a form of networking, yes?

I am attempting to write a client/server pair of programs, one of which triggers the other, and it has been recommended I use the eventlib library so as to avoid potential issues with forks and such.

Anyways, I'm new in general to socket communication, but was at least able to get the general idea of how it works (socket, address, bind, accept, read/write, close, etc), but that doesn't seem to be the case with eventlib, which also seems to have few (if any) bits of sample code beyond the man pages.

I'm working on the server part for right now, testing using a java TCP transmission program I wrote and have confirmed operational. The server is supposed to listen on a specific port for a message and then immediately save that message, to confirm that it's receiving what it's supposed to be receiving.

However, the best I have been able to do so far is to get it to read and store a 12 character string of binary data, always the same exact string, so it's definitely not the message.

Sample code:


```
extern evContext bgprd_ctx;
static socket_t sock;

static void
session_accept(evContext ev_ctx, void *uap UNUSED, int session_socket,
        const void *la UNUSED, int lalen UNUSED, const void *ra UNUSED,
        int ralen UNUSED)
{
    char buffer[201];
    register int bytes, c;

    evTryAccept(ev_ctx, sock.conn_id, NULL);
    bytes = recv(session_socket, buffer, 200, 0);
    data_set(buffer);*/
}

int
socket_init(void)
{
    struct sockaddr_in sa;

    sock.socket = -1;
    sock.port = 4600;

    if ((sock.socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    bzero(&sa, sizeof sa);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(sock.port);
    sa.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sock.socket, (struct sockaddr *)&sa, sizeof sa) < 0) {
        perror("bind");
        return 2;
    }

    evInitID(&sock.conn_id);
    if(evListen(ctx, sock.socket, 4, session_accept,
            &sa, &sock.conn_id)) {
        //do something error related
        return 1;
    }

    return 0;
}
```


any suggestions as to what I'm doing wrong here? Been railing against this for days.


----------

