1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include "common.h"
static struct sys_events * sys_events;
static void
event_read_cb (struct sys_events * sys_events, struct sys_event * event );
static struct sys_event_callback read_callback = { event_read_cb, 0 };
int main ( int argc, char ** argv )
{
int socket_index, socket_count = -1;
int * socket_array = NULL;
if (check_not(net_socket_initialize() != 0, "Can't initialize sockets"))
{
return -1;
}
if (check_not(net_types_check_sizes() != 0, "Invalid types size"))
{
return -1;
}
sys_events = sys_events_create(32);
assert(sys_events != NULL, "");
socket_count = net_socket_bind_all(NULL, 1, "0", &socket_array, NET_ADDR_PROTOCOL_TCP);
for (socket_index = 0; socket_index < socket_count; ++socket_index)
{
net_socket_connect_tcp(socket_array[socket_index], "127.0.0.1", 1, "9000");
sys_events_set_callback(sys_events, SYS_EVENT_READ, socket_array[socket_index], &read_callback);
}
while (sys_events_update(sys_events, SYS_EVENTS_INFINITE) == 0){}
net_socket_close_all((unsigned int)(socket_count), socket_array);
sys_events_delete(sys_events);
net_socket_terminate();
return 0;
}
void
event_read_cb( struct sys_events * sys_events, struct sys_event * event )
{
char send_msg[14] = "I'm the client";
unsigned int send_len = sizeof(send_msg);
char rcv_msg[256];
unsigned int rcv_len = 256;
if (event->reason == SYS_EVENT_ERROR)
{
trace_err("SOCKET_ERROR");
return;
}
if (net_socket_recv_tcp(event->ident,rcv_msg,&rcv_len) == NET_SOCKET_OK)
{
rcv_msg[rcv_len] = '\0';
trace_msg(rcv_msg);
}
else
{
trace_err("SOCKET RECV ERROR");
}
if (net_socket_send_tcp(event->ident,send_msg,send_len) != NET_SOCKET_OK)
{
trace_err("SOCKET SEND ERROR");
}
} |
Partager