#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

#define PORT 		666
#define DIRSIZE 	8192

typedef struct {
	char option;
	char[10] comando;
	char[50] nome;
	char[3000] dados;
	int id;

}handle;

main()
{
        char     dir[DIRSIZE];  /* used for incomming dir name, and
					outgoing data */
	int 	 sd, sd_current, cc, fromlen, tolen, fd;
	int 	 addrlen;
	struct   sockaddr_in sin;
	struct   sockaddr_in pin;
	handle shit;
 
	/* get an internet domain socket */
	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	/* complete the socket structure */
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons(PORT);

	/* bind the socket to the port number */
	if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
		perror("bind");
		exit(1);
	}

	/* show that we are willing to listen */
	if (listen(sd, 5) == -1) {
		perror("listen");
		exit(1);
	}
	/* wait for a client to talk to us */
        /* socklen_t addrlen = sizeof(pin); */
	if ((sd_current = accept(sd, (struct sockaddr *)  &pin, &addrlen)) == -1) {
		perror("accept");
		exit(1);
	}

	/* get a message from the client */
	while (1) {
	if (recv(sd_current, shit, sizeof(shit), 0) == -1) {
		perror("recv");
		exit(1);
	}

        /* get the directory contents */
//         read_dir(dir);
	//atender requisicao e enviar resposta para o cliente.
    
      /* strcat (dir," DUDE");
       */
	switch (shit.comando) {
		case "open" : 
			fd = open (shit.nome, shit);
			break;
		case "read" : break;
		case "write" : break;
		case "close" : break;
	}
	
	/* acknowledge the message, reply w/ the file names */
	if (send(sd_current, shit, strlen(shit), 0) == -1) {
		perror("send");
		exit(1);
	}
	}

        /* close up both sockets */
	close(sd_current); close(sd);
        
        /* give client a chance to properly shutdown */
        sleep(1);
}

 

