adds files
This commit is contained in:
36
socket.c
Normal file
36
socket.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
void recv_message(int sfd, char** message)
|
||||
{
|
||||
int numbytes;
|
||||
|
||||
*message = malloc(sizeof(char) * MAX_DATA_SIZE);
|
||||
|
||||
numbytes = recv(sfd, *message, MAX_DATA_SIZE - 1, 0);
|
||||
if (numbytes == -1)
|
||||
perror("recv");
|
||||
|
||||
(*message)[numbytes] = '\0';
|
||||
}
|
||||
|
||||
void send_message(int sfd, char* message)
|
||||
{
|
||||
int err;
|
||||
size_t len = strlen(message);
|
||||
char* msg_to_send = malloc(sizeof(char) * len);
|
||||
|
||||
strncpy(msg_to_send, message, len);
|
||||
|
||||
err = send(sfd, msg_to_send, len, 0);
|
||||
if (err < 0)
|
||||
perror("send");
|
||||
|
||||
free(msg_to_send);
|
||||
}
|
||||
|
||||
|
36
socket.h
Normal file
36
socket.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#define MAX_DATA_SIZE 2048
|
||||
|
||||
#define MAX_CONNECTIONS 50
|
||||
|
||||
/**
|
||||
* void recv_message
|
||||
*
|
||||
* int sfd - The socket to use
|
||||
* char** message - The EMPTY buffer we're using
|
||||
*
|
||||
* Receives the message and malloc's the buffer to store the message
|
||||
*
|
||||
* Result must be free'd
|
||||
*/
|
||||
void recv_message(int sfd, char** message);
|
||||
|
||||
/**
|
||||
* void send_message
|
||||
*
|
||||
* int sfd - The socket to use
|
||||
* char* message - The message we're sending
|
||||
*
|
||||
* Sends the message to the socket
|
||||
*/
|
||||
void send_message(int sfd, char* message);
|
||||
|
||||
/**
|
||||
* A simple macro that simplifies the usage of recv_message
|
||||
*/
|
||||
#define DO_MSG_RECV(sfd, buf) recv_message(sfd, &buf);puts(buf);free(buf)
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user