Simple TCP echo server and client connected to it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define TTCP_VERSION "0.0.1"
#define SERVER_PORT 9050
#define BUF_LEN 1024
int close_flg = 0;
int read_len = read(w->fd, buffer,
BUF_LEN-1);
buffer[read_len] = 0;
printf("Server received %d bytes data: %s\n", read_len, buffer);
if(read_len > 0) {
char *d = strdup(buffer);
d[strcspn(d, "\r\n")] = 0;
if(!strcmp(d,"help") || !strcmp(d,"?")) {
char *b = "TCP Client commands:\n"
"close -- close this client\n"
"close_all -- close all clients of this TCP server\n"
"destroy -- destroy this TCP server\n";
if(write(w->fd, b, strlen(b)) == -1);
}
else if(!strcmp(d,"close")) {
printf("The connection to client %d was closed\n", w->fd);
close_flg = 1;
}
else if(!strcmp(d,"destroy")) {
printf("The TCP server %d was destroyed\n", w->fd);
}
else if(!strcmp(d,"close_all")) {
if(fd) {
printf("Close all TCP server %d clients\n", fd);
}
else {
printf("Can't find server of %d client\n", w->fd);
}
}
else {
if(write(w->fd, buffer, read_len) == -1);
}
}
else {
printf("The client %d has disconnected\n", w->fd);
close_flg = 1;
}
if(close_flg) {
}
}
printf("Client %d connected\n", fd);
}
size_t data_len, void *user_data) {
switch(event) {
{
int port_created;
}
printf("Client mode example is under construction yet,\n"
"trying system telnet application ...\n");
if(system(buffer) == -1);
}
}
break;
default:
break;
}
}
int main(
int argc,
char** argv) {
const char *app_argv[] = { "", "app_type"};
return (EXIT_SUCCESS);
}