add client testing
This commit is contained in:
16
include/client.h
Normal file
16
include/client.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef __CLIENT_H__
|
||||||
|
#define __CLIENT_H__
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int test_client_connection(const char *hostname, const char *port);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -15,6 +15,6 @@
|
|||||||
// how many pending connections the queue will hold
|
// how many pending connections the queue will hold
|
||||||
#define BACKLOG 10
|
#define BACKLOG 10
|
||||||
|
|
||||||
int start_server(void);
|
int start_server(const char *hostname, const char *service);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Print every IPs of a hostname
|
// print every IPs of a hostname
|
||||||
void print_ips(const char *hostname, const char *port);
|
void print_ips(const char *hostname, const char *port);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
project('tutorial', 'c')
|
project('cws', 'c')
|
||||||
|
|
||||||
subdir('src')
|
subdir('src')
|
||||||
incdir = include_directories('include')
|
incdir = include_directories('include')
|
||||||
|
|
||||||
executable('cws', sources, include_directories: incdir)
|
executable('server', server, include_directories: incdir)
|
||||||
|
executable('client', client, include_directories: incdir)
|
||||||
@@ -12,6 +12,12 @@ Before reading, this document could contain errors, please check everything you
|
|||||||
- [struct sockaddr\_storage](#struct-sockaddr_storage)
|
- [struct sockaddr\_storage](#struct-sockaddr_storage)
|
||||||
- [IP Addresses](#ip-addresses)
|
- [IP Addresses](#ip-addresses)
|
||||||
- [getaddrinfo()](#getaddrinfo)
|
- [getaddrinfo()](#getaddrinfo)
|
||||||
|
- [socket()](#socket-1)
|
||||||
|
- [bind()](#bind)
|
||||||
|
- [connect()](#connect)
|
||||||
|
- [listen()](#listen)
|
||||||
|
- [accept()](#accept)
|
||||||
|
- [send() and recv()](#send-and-recv)
|
||||||
|
|
||||||
|
|
||||||
### Socket
|
### Socket
|
||||||
@@ -116,3 +122,43 @@ int getaddrinfo(const char *node, // e.g. "www.example.com" or IP
|
|||||||
|
|
||||||
The `node` could be a host name or IP address. `service` could be a port number or a service found in `/etc/services` (e.g. "http" or "ftp" or "telnet").
|
The `node` could be a host name or IP address. `service` could be a port number or a service found in `/etc/services` (e.g. "http" or "ftp" or "telnet").
|
||||||
`hints` points to a struct you already filled and `res` contains a linked list of results.
|
`hints` points to a struct you already filled and `res` contains a linked list of results.
|
||||||
|
|
||||||
|
### socket()
|
||||||
|
```c
|
||||||
|
int socket(int domain, int type, int protocol);
|
||||||
|
```
|
||||||
|
|
||||||
|
`domain` could be `PF_INET` or `PF_INET6`, `type` instead TCP or UDP and `protocol` to 0. `PF_INET` is very close to `AF_INET`! However, we can avoid to put the stuff manually and use the results from `getaddrinfo()`.
|
||||||
|
|
||||||
|
### bind()
|
||||||
|
Do this if you're going to listen on a port. The port is used by the kernel to match an incoming packet to a socket descriptor.
|
||||||
|
|
||||||
|
```c
|
||||||
|
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
|
||||||
|
```
|
||||||
|
|
||||||
|
### connect()
|
||||||
|
```c
|
||||||
|
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
|
||||||
|
```
|
||||||
|
|
||||||
|
### listen()
|
||||||
|
```c
|
||||||
|
int listen(int sockfd, int backlog);
|
||||||
|
```
|
||||||
|
|
||||||
|
`backlog` is the amount of max clients allowed on the incoming queue. A client will wait in the queue until you accept it.
|
||||||
|
|
||||||
|
### accept()
|
||||||
|
```c
|
||||||
|
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
||||||
|
```
|
||||||
|
|
||||||
|
After you accept a client, the function will return a new socket file descriptor used to communicate.
|
||||||
|
|
||||||
|
### send() and recv()
|
||||||
|
```c
|
||||||
|
int send(int sockfd, const void *msg, int len, int flags);
|
||||||
|
```
|
||||||
|
|
||||||
|
Just put `flags` to 0. It will return the bytes sent.
|
||||||
|
|||||||
38
src/client.c
Normal file
38
src/client.c
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF
|
||||||
|
|
||||||
|
#include "client.h"
|
||||||
|
#include "colors.h"
|
||||||
|
|
||||||
|
int test_client_connection(const char *hostname, const char *port) {
|
||||||
|
struct addrinfo hints;
|
||||||
|
struct addrinfo *res;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof hints);
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
|
||||||
|
int status = getaddrinfo(hostname, port, &hints, &res);
|
||||||
|
if (status != 0) {
|
||||||
|
fprintf(stderr, RED BOLD "[client] getaddrinfo(): %s\n" RESET,
|
||||||
|
gai_strerror(status));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
|
|
||||||
|
status = connect(sockfd, res->ai_addr, res->ai_addrlen);
|
||||||
|
if (status != 0) {
|
||||||
|
fprintf(stderr, RED BOLD "[client] connect(): %s\n" RESET,
|
||||||
|
gai_strerror(status));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[4096];
|
||||||
|
int bytes_read = recv(sockfd, buf, 4096, 0);
|
||||||
|
fprintf(stdout, BLUE "[client] Read %d bytes: %s\n" RESET, bytes_read,
|
||||||
|
buf);
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,17 +1,14 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
fprintf(stdout, BOLD GREEN "Running cws...\n" RESET);
|
fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET);
|
||||||
|
|
||||||
int ret = start_server();
|
int ret = start_server(NULL, "3030");
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, BOLD RED "Unable to start web server\n");
|
fprintf(stderr, BOLD RED "Unable to start web server\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
print_ips("google.com", "80");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/mainc.c
Normal file
14
src/mainc.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "client.h"
|
||||||
|
#include "colors.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
fprintf(stdout, BOLD GREEN "[client] Running cws...\n" RESET);
|
||||||
|
|
||||||
|
int ret = test_client_connection("localhost", "3030");
|
||||||
|
if (ret < 0) {
|
||||||
|
fprintf(stderr, BOLD RED "Unable to start client\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
sources = files('main.c', 'server.c', 'utils.c')
|
server = files('main.c', 'server.c', 'utils.c')
|
||||||
|
client = files('mainc.c', 'client.c')
|
||||||
49
src/server.c
49
src/server.c
@@ -1,34 +1,49 @@
|
|||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
int start_server(void) {
|
int start_server(const char *hostname, const char *service) {
|
||||||
char ipv4[INET_ADDRSTRLEN];
|
|
||||||
struct sockaddr_in sa;
|
|
||||||
|
|
||||||
inet_pton(AF_INET, "192.168.0.1", &(sa.sin_addr));
|
|
||||||
inet_ntop(AF_INET, &(sa.sin_addr), ipv4, INET_ADDRSTRLEN);
|
|
||||||
|
|
||||||
fprintf(stdout, BLUE "IPv4: %s\n" RESET, ipv4);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_local_ip(void) {
|
|
||||||
char service[] = "3030";
|
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *res;
|
struct addrinfo *res;
|
||||||
|
struct sockaddr_storage their_sa; // incoming clients
|
||||||
|
socklen_t theirsa_size = sizeof their_sa;
|
||||||
|
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
|
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
|
||||||
hints.ai_socktype = SOCK_STREAM; // TCP
|
hints.ai_socktype = SOCK_STREAM; // TCP
|
||||||
hints.ai_flags = AI_PASSIVE; // fill in IP for me
|
hints.ai_flags = AI_PASSIVE; // fill in IP for me
|
||||||
|
|
||||||
int status = getaddrinfo(NULL, service, &hints, &res);
|
int status = getaddrinfo(hostname, service, &hints, &res);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
fprintf(stderr, RED "getaddrinfo() error: %s\n" RESET,
|
fprintf(stderr,
|
||||||
|
RED BOLD "[server] getaddrinfo() error: %s\n" RESET,
|
||||||
gai_strerror(status));
|
gai_strerror(status));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// socket file descriptor
|
||||||
|
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
|
|
||||||
|
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
|
||||||
|
if (status != 0) {
|
||||||
|
fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET,
|
||||||
|
gai_strerror(status));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
status = listen(sockfd, BACKLOG);
|
||||||
|
if (status != 0) {
|
||||||
|
fprintf(stderr, RED BOLD "[server] listen(): %s\n" RESET,
|
||||||
|
gai_strerror(status));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *msg = "Hello there!";
|
||||||
|
int msg_len = strlen(msg);
|
||||||
|
int newfd = accept(sockfd, (struct sockaddr *)&their_sa, &theirsa_size);
|
||||||
|
int bytes_sent = send(newfd, msg, msg_len, 0);
|
||||||
|
fprintf(stdout, BLUE "[server] Sent %d bytes\n" RESET, bytes_sent);
|
||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user