moved server functions into why2-misc
This commit is contained in:
parent
a51cfae3b0
commit
22ff5a3e7d
@ -20,5 +20,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#define WHY2_CHAT_MISC_H
|
||||
|
||||
void send_socket(char *text, int socket); //send socket.... wtf did you expect
|
||||
char *read_socket(int socket); //read lol
|
||||
void *communicate_thread(void *arg); //COMMUNICATION THREAD
|
||||
|
||||
#endif
|
@ -17,10 +17,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <why2/chat/common.h>
|
||||
#include <why2/chat/misc.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <why2/chat/misc.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
|
||||
|
@ -18,11 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include <why2/chat/common.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
char *read_socket(int socket);
|
||||
void *communicate_thread(void *arg);
|
||||
#include <why2/chat/misc.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@ -56,59 +52,3 @@ int main(void)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *communicate_thread(void *arg)
|
||||
{
|
||||
printf("User connected.\t%d\n", *((int*) arg));
|
||||
|
||||
const time_t startTime = time(NULL);
|
||||
char *received = NULL;
|
||||
|
||||
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS
|
||||
{
|
||||
received = read_socket(*((int*) arg)); //READ
|
||||
|
||||
if (received == NULL) return NULL; //FAILED; EXIT THREAD
|
||||
|
||||
if (strcmp(received, "!exit\n") == 0) break; //USER REQUESTED PROGRAM EXIT
|
||||
|
||||
printf("Received:\n%s\n\n", received);
|
||||
|
||||
why2_deallocate(received);
|
||||
}
|
||||
|
||||
printf("User exited.\t%d\n", *((int*) arg));
|
||||
|
||||
//DEALLOCATION
|
||||
close(*((int*) arg));
|
||||
why2_deallocate(received);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *read_socket(int socket)
|
||||
{
|
||||
if (socket == -1)
|
||||
{
|
||||
fprintf(stderr, "Reading socket failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned short content_size = 0;
|
||||
char *content_buffer = why2_calloc(2, sizeof(char));
|
||||
|
||||
//GET LENGTH
|
||||
if (recv(socket, content_buffer, 2, 0) != 2) return NULL;
|
||||
|
||||
content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]);
|
||||
|
||||
why2_deallocate(content_buffer);
|
||||
|
||||
//ALLOCATE
|
||||
content_buffer = why2_calloc(content_size + 1, sizeof(char));
|
||||
|
||||
//READ FINAL MESSAGE
|
||||
if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n");
|
||||
|
||||
return content_buffer;
|
||||
}
|
||||
|
@ -18,8 +18,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include <why2/chat/misc.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <why2/memory.h>
|
||||
|
||||
@ -42,3 +45,59 @@ void send_socket(char *text, int socket)
|
||||
|
||||
why2_deallocate(final);
|
||||
}
|
||||
|
||||
void *communicate_thread(void *arg)
|
||||
{
|
||||
printf("User connected.\t%d\n", *((int*) arg));
|
||||
|
||||
const time_t startTime = time(NULL);
|
||||
char *received = NULL;
|
||||
|
||||
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS
|
||||
{
|
||||
received = read_socket(*((int*) arg)); //READ
|
||||
|
||||
if (received == NULL) return NULL; //FAILED; EXIT THREAD
|
||||
|
||||
if (strcmp(received, "!exit\n") == 0) break; //USER REQUESTED PROGRAM EXIT
|
||||
|
||||
printf("Received:\n%s\n\n", received);
|
||||
|
||||
why2_deallocate(received);
|
||||
}
|
||||
|
||||
printf("User exited.\t%d\n", *((int*) arg));
|
||||
|
||||
//DEALLOCATION
|
||||
close(*((int*) arg));
|
||||
why2_deallocate(received);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *read_socket(int socket)
|
||||
{
|
||||
if (socket == -1)
|
||||
{
|
||||
fprintf(stderr, "Reading socket failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned short content_size = 0;
|
||||
char *content_buffer = why2_calloc(2, sizeof(char));
|
||||
|
||||
//GET LENGTH
|
||||
if (recv(socket, content_buffer, 2, 0) != 2) return NULL;
|
||||
|
||||
content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]);
|
||||
|
||||
why2_deallocate(content_buffer);
|
||||
|
||||
//ALLOCATE
|
||||
content_buffer = why2_calloc(content_size + 1, sizeof(char));
|
||||
|
||||
//READ FINAL MESSAGE
|
||||
if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n");
|
||||
|
||||
return content_buffer;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user