6 changed files with 126 additions and 15 deletions
@ -0,0 +1,67 @@ |
|||||
|
#include "client.h" |
||||
|
|
||||
|
client_t* client_open(conn_handler handler) { |
||||
|
struct sockaddr_in servaddr; |
||||
|
client_t *client = malloc(sizeof(client_t)); |
||||
|
if(NULL == handler) { |
||||
|
printf("client_open: received empty handler, stopping"); |
||||
|
return NULL; |
||||
|
} |
||||
|
if (NULL == client) { |
||||
|
printf("client_open: malloc failed"); |
||||
|
return NULL; |
||||
|
} |
||||
|
client->conn = connection_create(CONN_TYPE_CLIENT); |
||||
|
if (NULL == client->conn) { |
||||
|
free(client); |
||||
|
return NULL; |
||||
|
} |
||||
|
client->handler = handler; |
||||
|
client->conn->fd = socket(AF_INET, SOCK_STREAM, 0); |
||||
|
if (-1 == client->conn->fd) { |
||||
|
printf("client_listen: failed to create endpoint.\n"); |
||||
|
return NULL; |
||||
|
} |
||||
|
return client; |
||||
|
} |
||||
|
|
||||
|
int client_connect(client_t *client) { |
||||
|
struct sockaddr_in servaddr; |
||||
|
int servaddr_len = 0; |
||||
|
|
||||
|
if(client == NULL) { |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
servaddr.sin_family = AF_INET; |
||||
|
servaddr.sin_addr.s_addr = inet_addr(SERVERIP); |
||||
|
servaddr.sin_port = htons(PORT); |
||||
|
|
||||
|
if (0 != connect(, MAX_CLIENTS)) { |
||||
|
printf("client_accept: listen failed\n"); |
||||
|
return -1; |
||||
|
} |
||||
|
printf("client_accept: listening\n"); |
||||
|
servaddr_len = sizeof(servaddr); |
||||
|
connfd = connect(client->conn->fd, &servaddr, &servaddr_len); |
||||
|
if(0 >= connfd) { |
||||
|
printf("client_accept: connection to client failed, "); |
||||
|
return -1; |
||||
|
} else { |
||||
|
client->handler(client->conn->fd); |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int client_close(client_t *client) { |
||||
|
int err = 0; |
||||
|
if (NULL == client) { |
||||
|
return 0; |
||||
|
} |
||||
|
if (0 != connection_destroy(client->conn)) { |
||||
|
printf("client_close: closing socket failed\n"); |
||||
|
err = -1; |
||||
|
} |
||||
|
free(client); |
||||
|
return err; |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
//
|
||||
|
// Created by root on 10/30/19.
|
||||
|
//
|
||||
|
|
||||
|
#ifndef ECDAA_ISSUER_SERVER_H |
||||
|
#define ECDAA_ISSUER_SERVER_H |
||||
|
#include <sys/socket.h> |
||||
|
#include <netinet/in.h> |
||||
|
#include <unistd.h> |
||||
|
#include "connection.h" |
||||
|
|
||||
|
#define PORT 6590 |
||||
|
#define MAX_BUFSIZE 1024 |
||||
|
#define SERVERIP "127.0.0.1" |
||||
|
|
||||
|
typedef struct client { |
||||
|
connection_t* conn; |
||||
|
conn_handler handler; |
||||
|
}client_t; |
||||
|
|
||||
|
client_t* client_open(conn_handler handler); |
||||
|
|
||||
|
int client_connect(client_t* client); |
||||
|
|
||||
|
int client_close(client_t* client); |
||||
|
|
||||
|
#endif //ECDAA_ISSUER_SERVER_H
|
||||
Loading…
Reference in new issue