RTLAB异步通信---客户端代码

来源:互联网 发布:胸大 知乎 编辑:程序博客网 时间:2024/05/21 22:22

本分析RTLAB软件自带的异步通信案例中的源代码。
更多内容可参考异步通信流程

该案例在…/Examples/IO/Generic/asyncip中,打开模型后,主要内容如下图所示:这里写图片描述

该模型首先将模型内部运行数据,通过端口发送到第三方程序app,app接收到数据后,再将其返回,模型接收到数据后,再将数据显示出来。

下面的代码展示了app中的接收与发送是如何实现的。

注:该程序其实只是常规的socket编程流程,具体可参考socket编程基本流程
这里写图片描述

#include <sys/types.h>#include <stdio.h>#include <conio.h>#include <winsock.h>#include <time.h>#pragma comment(lib, "winmm.lib")#pragma comment(lib, "wsock32.lib")#pragma comment(lib, "advapi32.lib")/*----------------------------------------------------------------- * Displays the packet (array of double) received and sends it back * to the originator. *-----------------------------------------------------------------*/void main(void){    struct          sockaddr_in server_ad;    struct          sockaddr_in client_ad;    int             sd,sd2;    int             port;    int             length;    int             n,iter=0, i;    long            ioctlVal,nb_char;    char            data[255];      // 数据大小    struct hostent *hote;    char            buf[200];    int             TCP_PROTO;    WSADATA         wsaData;    WSAStartup(0x0101, &wsaData);    length = sizeof(client_ad);//结构体大小    printf("Select a port number for the IP server: ");    scanf("%d", &port);    printf("Select protocol (0=UDP, 1=TCP): ");    scanf("%d", &TCP_PROTO);    /* Ouverture d'un socket afin de recevoir des demandes de connection *///打开一个套接字接收连接请求     /* ---------------------------------------------------------    ---------*/     if(TCP_PROTO)      {     //创建一个socket    if((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)      {        printf("ERROR: Could not create socket\n");        exit(1);      }      }    else      {    if((sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)      {        printf("ERROR: Could not create socket\n");        exit(1);      }      }    /* Indentification du socket */    /* ------------------------------------------------------------------*/         memset((char *)&server_ad,0,sizeof(server_ad));  /* effacer la structure      */    server_ad.sin_family = AF_INET;                  /* selectionner la famille Internet     */    server_ad.sin_addr.s_addr = INADDR_ANY;          /* selectionner l'IP adresse local      */    server_ad.sin_port = htons((u_short)port);       /* selectionner le port de communication*///绑定socket与sever    if (bind(sd, (struct sockaddr *)&server_ad, sizeof(server_ad)) < 0)      {    printf("bind() error\n");    exit(1);      }    gethostname(buf,200);    hote = gethostbyname(buf);    printf("======================================================\n");    printf("SERVER:   Address  : %s\n", inet_ntoa(*(struct in_addr*)hote->h_addr));    printf("          Name     : %s\n", hote->h_name);    printf("          Port     : %d\n", port);    printf("======================================================\n\n");    if(TCP_PROTO)//是否是TCP模式      {    if (listen(sd, 5) < 0) //监听      {         printf("ERROR: Could not listen for connections\n");         exit(1);       }     printf("Listening for incoming connections... \n");     /* Bloque dans l'attente d'un demande de connection client */     if ((sd2 = accept(sd, (struct sockaddr *)&client_ad, &length)) < 0)       {         printf("ERROR: Could not accept connection\n");         exit(1);       }     printf("Client %s connected.\n", inet_ntoa( client_ad.sin_addr));      }    //设置端口模式    ioctlVal = 1;    if(!TCP_PROTO)      ioctlsBoucle Principale: Affichage de message de clientsFIONBIO, &ioctlVal);//    else      ioctlsocket(sd2, FIONBIO, &ioctlVal);    /* 主循环:客户信息显示*/    /* ------------------------------------------------------------------*/         while(1)      {    if(!TCP_PROTO)      ioctlsocket(sd, FIONREAD, &nb_char);    else      ioctlsocket(sd2, FIONREAD, &nb_char);    if(nb_char > 0)      {        // Lecture du message        memset(&data,0,sizeof(data));        if(!TCP_PROTO)          n = recvfrom(sd, (char*)&data, sizeof(data), 0, (struct sockaddr *)&client_ad, &length);        else          n = recv(sd2, (char*)&data, sizeof(data), 0);//TCP接收数据        if(!TCP_PROTO)          sendto(sd,(char*)&data,n,0,(struct sockaddr *)&client_ad, length);        else          send(sd2,(char*)&data,n,0);        printf ("Received (%d bytes): ", n);        for (i=0; i<(n); i++) { printf ("%2X ", data[i]); }        printf ("\n");了        iter = 1;      }    else      {        if((iter++  % 30) == 0)          printf("...\n");        Sleep(500);      }      }    /* 关闭服务器 */    closesocket(sd);    if(TCP_PROTO)      closesocket(sd2);}
阅读全文
0 0