server:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "trans_util.h"
#include "msrt_api.h"
#define SEND_BUF_SIZE (256 * 1024)
static unsigned int g_connect_id = 0;
static unsigned int g_old_connect_id = 0;
static FILE *g_input_fp = NULL;
static FILE *g_output_fp = NULL;
static void ct_server_status_nfy(
msrt_fd instance,
unsigned int connect_id,
msrt_status_e status,
void *user_data)
{
printf("call.. ct_server_status_nfy\n");
msrt_addr_t remote_addr;
int rc;
if (MSRT_STATUS_CONNECTED == status)
{
rc = msrt_get_remote_address(instance, connect_id, &remote_addr);
if (0 != rc)
{
printf("Failed to get_remote_address_by_msrt[%d]!\n",
g_connect_id);
}
g_connect_id = connect_id;
printf("server[%u, %s:%d] connected!\n",
g_connect_id,
remote_addr.ip,
remote_addr.port);
}
else if (MSRT_STATUS_DISCONNECTED == status)
{
printf("server[%u] disconnected!\n",
g_connect_id);
}
return;
}
static void ct_server_data_nfy(
msrt_fd instance,
unsigned int connect_id,
char *data,
int data_size,
void *user_data)
{
printf("call.. ct_server_data_nfy\n");
printf("server[%u] recv %d data!\n",connect_id,data_size);
printf("your input is :");
for (int i = 0; i < 20; ++i) {
printf("%c", data[i]);
}
printf("\n");
//fwrite(data, sizeof(unsigned char), data_size, g_output_fp);
//fflush(g_output_fp);
return;
}
int main() {
printf("my_server\n");
char input_file_name[1024] = {0};
char output_file_name[1024] = {0};
msrt_ct_server_param_t param;
msrt_fd instance = MSRT_INVALID_FD;
int rc = MSRTC_OK;
int read_size = 0;
char *buf = NULL;
buf = (char *)malloc(sizeof(char) * SEND_BUF_SIZE);
if (NULL == buf)
{
printf("Failed to malloc buf!\n");
return -1;
}
int param_size = sizeof(msrt_ct_server_param_t);
memset(¶m, 0, param_size);//初始化所有参数为0
strcpy(param.local_ip, "127.0.0.1");
param.local_listen_port = 5555;
param.nb_local_connect_ports = 2;
param.local_connect_ports[0] = 5556;
param.local_connect_ports[1] = 5557;
param.connect_config.status_nfy_func = ct_server_status_nfy;
param.connect_config.status_user_data = NULL;
param.connect_config.data_nfy_func = ct_server_data_nfy;
param.connect_config.data_user_data = NULL;
rc = msrt_create_ct_server_instance(¶m, &instance);
printf("rc: %d\n", rc);
if (MSRTC_OK != rc)
{
printf("Failed to msrt_create_client_instance!\n");
free(buf);
return -1;
}
//打开文件,发送数据到客户端
strcpy(input_file_name, "server_in.txt");
g_input_fp = fopen(input_file_name, "rb");
int start_time = (int)time_now() / 1000;
printf("start_time: %d \n", start_time);
int curr_time = 0;
int loop_time = 180;//超时时间,单位s
while (1){
sleep(1);
curr_time = (int)time_now() / 1000;
if((curr_time - start_time) > loop_time){
printf("server timeout exit!\n");
break;
}
if(g_connect_id == 0){
continue;
}
//printf("g_connect_id:%d \n", g_connect_id);
// if (0 == feof(g_input_fp))
// {
// read_size = (int)fread(buf, sizeof(char), SEND_BUF_SIZE, g_input_fp);
//
// if (read_size > 0)
// {
// msrt_send_data(instance, g_connect_id, buf, read_size);
// }
// }
//服务器控制台接收数据
//msrt_recv_data(instance, g_connect_id, buf, 20, &read_size, -1);
}
fclose(g_input_fp);
//断开连接
msrt_destroy_instance(instance);
free(buf);
return 0;
}
client:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "trans_util.h"
#include "msrt_api.h"
#define SEND_BUF_SIZE (256 * 1024)
#define RECV_BUF_SIZE (256 * 1024)
static unsigned int g_connect_id = 0;
static unsigned int g_old_connect_id = 0;
static FILE *g_input_fp = NULL;
static FILE *g_output_fp = NULL;
static void ct_client_status_nfy(
msrt_fd instance,
unsigned int connect_id,
msrt_status_e status,
void *user_data)
{
msrt_addr_t remote_addr;
int rc;
if (MSRT_STATUS_CONNECTED == status)
{
rc = msrt_get_remote_address(instance, connect_id, &remote_addr);
if (0 != rc)
{
printf("Failed to get_remote_address_by_msrt[%d]!\n",
g_connect_id);
}
g_connect_id = connect_id;
printf("client[%u, %s:%d] connected!\n",
g_connect_id,
remote_addr.ip,
remote_addr.port);
}
else if (MSRT_STATUS_DISCONNECTED == status)
{
printf("client[%u] disconnected!\n",
g_connect_id);
}
return;
}
static void ct_client_data_nfy(
msrt_fd instance,
unsigned int connect_id,
char *data,
int data_size,
void *user_data)
{
printf("client[%u] recv %d data async!\n",
connect_id,
data_size);
fwrite(data, sizeof(unsigned char), data_size, g_output_fp);
fflush(g_output_fp);
return;
}
int main() {
printf("my_client\n");
char input_file_name[1024] = {0};
char output_file_name[1024] = {0};
msrt_ct_client_param_t param;
msrt_fd instance = MSRT_INVALID_FD;
int rc = MSRTC_OK;
int read_size = 0;
char *buf = NULL;
buf = (char *)malloc(sizeof(char) * RECV_BUF_SIZE);
if (NULL == buf)
{
printf("Failed to malloc buf!\n");
return -1;
}
int param_size = sizeof(msrt_ct_client_param_t);
memset(¶m, 0, param_size);//初始化所有参数为0
strcpy(param.remote_ip, "127.0.0.1");
param.remote_port = 5555;
param.connect_config.status_nfy_func = ct_client_status_nfy;
param.connect_config.status_user_data = NULL;
//param.connect_config.data_nfy_func = ct_client_data_nfy;
//param.connect_config.data_user_data = NULL;
rc = msrt_create_ct_client_instance(¶m, &instance);
printf("rc: %d\n", rc);
if (MSRTC_OK != rc)
{
printf("Failed to msrt_create_client_instance!\n");
free(buf);
return -1;
}
//打开文件,接收服务器数据
strcpy(output_file_name, "client_out.txt");
g_output_fp = fopen(output_file_name, "wb+");
int start_time = (int)time_now() / 1000;
printf("start_time: %d \n", start_time);
int curr_time = 0;
int loop_time = 180;//超时时间,单位s
while (1){
sleep(1);
curr_time = (int)time_now() / 1000;
if((curr_time - start_time) > loop_time){
printf("server timeout exit!\n");
break;
}
if(g_connect_id == 0){
continue;
}
//printf("g_connect_id:%d \n", g_connect_id);
// msrt_recv_data(instance, g_connect_id, buf, RECV_BUF_SIZE, &read_size, -1);
// printf("client[%u] recv %d data sync!\n", g_connect_id, read_size);
//
// fwrite(buf, sizeof(unsigned char), read_size, g_output_fp);
// fflush(g_output_fp);
//控制台输入
char chat[20];
printf("place input:");
scanf("%s", chat);
buf = chat;
msrt_send_data(instance, g_connect_id, buf, 20);
}
fclose(g_input_fp);
//断开连接
msrt_destroy_instance(instance);
free(buf);
return 0;
}