All pastes #406880 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-03-22 19:27 UTC
rendered paste body
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>

#define ADDRESS "/tmp/sock"
#define MAXMSG  4096


int main(void) {

  int ns,len,s;
  socklen_t fromlen;
  struct sockaddr_un saun, fsaun;
  char message[MAXMSG], buf[1024];


  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
      perror("server: socket");
      exit(1);
  }

    saun.sun_family = AF_UNIX;
    strcpy(saun.sun_path, ADDRESS);
    unlink(ADDRESS);
    len = sizeof(saun.sun_family) + strlen(saun.sun_path);
    
    if (bind(s, (struct sockaddr *)&saun, len) < 0) {
        perror("server: bind");
        exit(1);
    }
    if (listen(s, 5) < 0) {
        perror("server: listen");
        exit(1);
    }
    if ((ns = accept(s, (struct sockaddr *)&fsaun, &fromlen)) < 0)
    {
        perror("server: accept");
        exit(1);
    } 
  
    if (read(ns, message, sizeof(buf)) < 0)
    {
      printf("error %d reading socket\n", errno);
      exit(1);
    }

        return 0;
}