Bonjour a tous,
Je voudrai intégrer dans mon code une fonction pour récupérer des données du serveur.
Merci par avance
Voici le code
client
serveur#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "L_LOG.h"
#include "L_MOD.h"
#include "L_SIO.h"
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
int KeepRunning = XRWS_TRUE;
/// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
fd_set fd_set_read,fd_set_write;
int fd_max = 0;
/**
* @brief ascessor to the configuration parameters file path.
* @return the configuration parameters file path.
*/
char * getCfgParameterPath ()
{
return CfgPath;
}
/**
* @brief This function is executed at exit, it permit to stop the main loop
* @param[in] sig the signal recieve to stop appication.
*/
void do_at_exit(int sig)
{
DEBUG("do at exit");
KeepRunning = 0;
}
/**
* @brief Map the interuption signal with do_at_exit function
*/
void handle_SIGINT()
{
struct sigaction orig_action;
struct sigaction action;
action.sa_flags = 0;
action.sa_handler = do_at_exit;
if(-1 == sigaction(SIGINT, &action, &orig_action))
{
ERROR("Error in fu. sigaction");
}
}
static XRWS_STATUS T_SNIFFER_API_handle_data(char * p_data, int p_nr_of_bytes, int* p_nr_of_bytes_parsed){
INFO("connection call back");
return XRWS_OK;
}
static void build_select_fd_set(){
int i;
L_SIO_port_t *l_port = NULL;
L_SIO_TCP_config_t *l_cfg = NULL;
fd_max = 0;
FD_ZERO(&fd_set_read);
FD_ZERO(&fd_set_write);
if(L_SIO_get_port_nr()!=0){
for(i=0;i<L_SIO_get_port_nr(); i++){
l_port = L_SIO_get_port(i);
if(l_port->type == L_SIO_PORT_TYPE_TCP){
l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
FD_SET(l_port->fd, &fd_set_read);
if(l_cfg->writable==XRWS_FALSE){
FD_SET(l_port->fd, &fd_set_write);
}
fd_max = MAX(fd_max,l_port->fd);
}
}
}else{
INFO("Server connection was broken");
KeepRunning = XRWS_FALSE;
}
}
/**
* @brief Main function
*
* @param[in] argc argument count.
* @param[in] argv arguments
* @return exit code
*/
int main(int argc, char **argv)
{
sigset_t orig_sigmask;
int rc = 0;
int i,fd_connection;
struct timeval l_timeout; /* Timeout for select */
L_SIO_TCP_config_t l_connection_cfg;
L_SIO_serial_read_CB l_CB[]={(L_SIO_serial_read_CB)T_SNIF FER_API_handle_data};
L_SIO_port_t *l_port = NULL;
L_SIO_TCP_config_t *l_cfg = NULL;
unsigned char msg[5]={2,1,2,12,1};
//****** INITIALIZATION ******
handle_SIGINT();
if(L_MOD_RUN_PARAM_retrieve(ar gc
,argv
,CfgPath)!=0)
{
ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
return L_MOD_EXIT_ERROR;
}
// init signals
if(L_MOD_SIG_init(&orig_sigmas k)!=0)
{
ERROR_EC("Error in fu. L_MOD_SIG_init");
return L_MOD_EXIT_ERROR;
}
DEBUG("\tSIG init OK");
// config for server
l_connection_cfg.type = L_SIO_SOCKET_TYPE_SOCKET;
l_connection_cfg.address.sin_f amily = AF_INET;
l_connection_cfg.address.sin_a ddr.s_addr = INADDR_ANY;
l_connection_cfg.address.sin_p ort = htons(2010);
// open server
if(L_SIO_TCP_open(&l_connectio n_cfg, l_CB, 1, &fd_connection)!=XRWS_OK){
ERROR("Error in function L_SIO_TCP_open");
return XRWS_ERROR;
}
build_select_fd_set();
DEBUG("Module was initialized correctly");
//****** MAIN LOOP ******
while(KeepRunning==XRWS_TRUE)
{
l_timeout.tv_sec = 5;
l_timeout.tv_usec = 0;
rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
DEBUG("Select (%d)",rc);
if(rc<0){
ERROR("Error in select function: '%s'",strerror(errno));
}else if(rc == 0){
ERROR("Select time out");
}else{
for(i=0;i<L_SIO_get_port_nr(); i++){
l_port = L_SIO_get_port(i);
if(l_port->type == L_SIO_PORT_TYPE_TCP){
l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
// check if there is something to read
if(FD_ISSET(l_port->fd,&fd_set_read) && rc > 0){
rc--; // this permits to avoid a false fd_isset on the socket returned by accept function
// Client is notify that there is something to read
DEBUG("connection has something to read");
if(L_SIO_read(l_port->fd,NULL)!=XRWS_OK){
ERROR("Error in fu L_SIO_read");
return XRWS_ERROR;
}
}
if(l_port->fd != -1){ //this protect from a previously closed fd
// check if socket is ready to write
if(FD_ISSET(l_port->fd,&fd_set_write) && rc > 0){
rc--; // this permits to avoid a false fd_isset on the socket returned by accept function
DEBUG("connection is ready to write");
l_cfg->writable = XRWS_TRUE;
if(L_SIO_write(l_port->fd,msg,5)!=XRWS_OK){
ERROR("Error in fu. L_SIO_write");
return XRWS_ERROR;
}
}
}
}
}
}
build_select_fd_set(); // re-build the fd set because select() function updated it
}
//close server connection
for (i=0;i<L_SIO_get_port_nr();i++ ){
l_port = L_SIO_get_port(i);
if(L_SIO_close(l_port->fd)!=XRWS_OK){
ERROR("Error in fu L_SIO_close on fd: %d",l_port->fd);
return XRWS_ERROR;
}
}
DEBUG("Exit properly");
return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "L_LOG.h"
#include "L_MOD.h"
#include "L_SIO.h"
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
int KeepRunning = 1;
/// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
fd_set fd_set_read,fd_set_write;
int fd_listen = -1;
//int fd_connection[L_SIO_MAX_NR_OF_PORTS] = {-1};
//int fd_connection_nr = 0;
//int fd_writable[L_SIO_MAX_NR_OF_PORTS] = {XRWS_FALSE};
int fd_max = 0;
/**
* @brief ascessor to the configuration parameters file path.
* @return the configuration parameters file path.
*/
char * getCfgParameterPath ()
{
return CfgPath;
}
/**
* @brief This function is executed at exit, it permit to stop the main loop
* @param[in] sig the signal recieve to stop appication.
*/
void do_at_exit(int sig)
{
DEBUG("do at exit");
KeepRunning = 0;
}
/**
* @brief Map the interuption signal with do_at_exit function
*/
void handle_SIGINT()
{
struct sigaction orig_action;
struct sigaction action;
action.sa_flags = 0;
action.sa_handler = do_at_exit;
if(-1 == sigaction(SIGINT, &action, &orig_action))
{
ERROR("Error in fu. sigaction");
}
}
static XRWS_STATUS SIM_SNIFFER_API_handle_data(in t p_fd,char* p_data, int p_nr_of_bytes, int* p_nr_of_bytes_parsed){
INFO("connection call back");
unsigned char msg[5]={2,1,2,12,1};
if(L_SIO_write(p_fd,msg,5)!=XR WS_OK){
ERROR("Error in fu. L_SIO_write");
return XRWS_ERROR;
}
return XRWS_OK;
}
static void build_select_fd_set(){
int i;
L_SIO_port_t *l_port;
L_SIO_TCP_config_t *l_cfg;
FD_ZERO(&fd_set_read);
FD_ZERO(&fd_set_write);
FD_SET(fd_listen, &fd_set_read);
fd_max = fd_listen;
for(i=0;i<L_SIO_get_port_nr(); i++){
l_port = L_SIO_get_port(i);
if(l_port->type == L_SIO_PORT_TYPE_TCP){
l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
FD_SET(l_port->fd, &fd_set_read);
if(l_cfg->writable==XRWS_FALSE){
FD_SET(l_port->fd, &fd_set_write);
}
fd_max = MAX(fd_max,l_port->fd);
}
}
}
/**
* @brief Main function
*
* @param[in] argc argument count.
* @param[in] argv arguments
* @return exit code
*/
int main(int argc, char **argv)
{
sigset_t orig_sigmask;
int rc = 0;
int i,l_read_byte_nr,l_client_fd;
struct timeval l_timeout; /* Timeout for select */
L_SIO_TCP_config_t l_cfg_server = {0};
L_SIO_serial_read_CB l_CB[]={(L_SIO_serial_read_CB)SIM_SN IFFER_API_handle_data};
L_SIO_port_t *l_port = NULL;
L_SIO_TCP_config_t *l_cfg = NULL;
//****** INITIALIZATION ******
handle_SIGINT();
if(L_MOD_RUN_PARAM_retrieve(ar gc
,argv
,CfgPath)!=0)
{
ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
return L_MOD_EXIT_ERROR;
}
// init signals
if(L_MOD_SIG_init(&orig_sigmas k)!=0)
{
ERROR_EC("Error in fu. L_MOD_SIG_init");
return L_MOD_EXIT_ERROR;
}
DEBUG("\tSIG init OK");
// config for server
l_cfg_server.type = L_SIO_SOCKET_TYPE_LISTENER;
l_cfg_server.address.sin_famil y = AF_INET;
l_cfg_server.address.sin_addr. s_addr = INADDR_ANY;
l_cfg_server.address.sin_port = htons(2010);
// open server
if(L_SIO_TCP_open_listener_soc ket(&l_cfg_server, &fd_listen)!=XRWS_OK){
ERROR("Error in function L_SIO_TCP_open");
return XRWS_ERROR;
}
DEBUG("Module was initialized correctly");
//****** MAIN LOOP ******
while(KeepRunning==1)
{
l_timeout.tv_sec = 5;
l_timeout.tv_usec = 0;
build_select_fd_set();
rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
DEBUG("Select (%d)",rc);
if(rc<0){
ERROR("Error in select function: '%s'",strerror(errno));
}else if(rc == 0){
ERROR("Select time out");
}else{
if(FD_ISSET(fd_listen,&fd_set_ read)){
DEBUG("Listening Server socket is set");
if(L_SIO_TCP_accept(&l_client_ fd,l_CB,1,fd_listen)!=XRWS_OK) {
ERROR("Error in function L_SIO_TCP_accept");
return XRWS_ERROR;
}
printf("New client on fd: %d\n",l_client_fd);
fflush(stdout);
DEBUG("client connection was accepted on fd: %d",l_client_fd);
rc --;
}
if(rc > 0){
for(i=0;i<L_SIO_get_port_nr(); i++){
l_port = L_SIO_get_port(i);
if(l_port->type == L_SIO_PORT_TYPE_TCP){
l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
if(FD_ISSET(l_port->fd,&fd_set_read) && rc > 0){
// Client is notify that there is something to read
DEBUG("Client connection (%d) has something to read", i);
if(L_SIO_read(l_port->fd,&l_read_byte_nr)!=XRWS_OK) {
ERROR("Error in fu L_SIO_read");
return XRWS_ERROR;
}
rc --;
}
if(l_port->fd != -1){ //this protect from a previously closed fd
if(FD_ISSET(l_port->fd,&fd_set_write) && rc > 0){
DEBUG("Client connection (%d) is ready to write", i);
l_cfg->writable = XRWS_TRUE;
rc --;
}
}
if(rc <= 0){
break; // stop the loop because there's nothing to read anymore
}
}
}
}
}
}
//close server connection
for (i=0;i<L_SIO_get_port_nr();i++ ){
if(L_SIO_close(L_SIO_get_port( i)->fd)!=XRWS_OK){
ERROR("Error in fu L_SIO_close on fd: %d",L_SIO_get_port(i)->fd);
return XRWS_ERROR;
}
}
//close server
if(close(fd_listen)!=XRWS_OK){
ERROR("Error in fu L_SIO_close on fd: %d",fd_listen);
return XRWS_ERROR;
}
DEBUG("Exit properly");
return 0;
}
-----