XPC-Target c代码TCP连接 [英] XPC-Target c code TCP connection

查看:98
本文介绍了XPC-Target c代码TCP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的,

我想使用c代码为simulink(xpc-target)创建tcp客户端块,但我的客户端代码无法正常工作。

我用过wireshark(网络协议分析器)并看到发送和接收tcp数据包但连接失败。

这些代码在没有操作系统的情况下运行。

服务器代码是否正确:

 sockaddr_in from; 
int fromlen = sizeof (from);
int 长度;
int read,i,n;
char outbuff [ 2048 ];
char inbuff [ 2048 ];
static int rev = 1 ;
static int rev1 = 1 ;
TCPMSG tcpmsg;
int len = sizeof (sockaddr_in);
int nonblock = 1 ;
if (dothisonce){
local.sin_family = AF_INET; // 地址系列
local.sin_addr = inet_addr(ipAddress); // (dword)0x0F0A0A0A; //通配符IP地址
local.sin_port = htons(gPort); // 在网络顺序中使用/ * 5000的端口* /
printf( socket \ n);
server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if (server == - 1 ){
printf ( 服务器的无效套接字\ n);
}
printf( bind\\\
);
if (bind(服务器,(PCSOCKADDR)& local, sizeof (本地)) != 0 ){
printf( bind failed\\\
);
goto sockOpenFailed;
}
printf( listen\\\
);
if (listen(服务器, 1 )!= 0 ){
printf( listen failed\\\
);
goto sockOpenFailed;
}
ioctlsocket(server,FIONBIO,& nonblock);
printf( XCP服务器设置,等待连接\ n);
sockOpenFailed:
dothisonce = 0 ;
}
printf( accept\\\
);
/ * 如果客户端未处于活动状态,请重新打开连接* /
if (server!= - 1 &&!clientisactive){
if ((client = accept(server,(PSOCKADDR)& from,& fromlen))
== - 1 ){
return ;




客户端代码不正确:

 sockaddr_in来自; 
int fromlen = sizeof (from);
int 长度;
int read,i,n;
char outbuff [ 2048 ];
char inbuff [ 2048 ];
static int rev = 1 ;
static int rev1 = 1 ;
TCPMSG tcpmsg;
int len = sizeof (sockaddr_in);
int nonblock = 1 ;
local.sin_family = AF_INET; // 地址系列
local.sin_addr = inet_addr(ipAddress); // (dword)0x0F0A0A0A; //通配符IP地址
local.sin_port = htons(gPort); // 在网络顺序中使用/ * 5000的端口* /
printf( soket \ n);
server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (server == - 1 ){
printf( 服务器的无效套接字\ n);
}
printf( connect\\\
);
if (connect(server,(PCSOCKADDR)& local, sizeof (local)) != 0 ){
printf( connect failed\\\
);
goto sockOpenFailed;
}
printf( send\\\
);
if (发送(服务器,outbuff, 2048 0 )!= 0 ){
printf( send failed\\\
);
goto sockOpenFailed;
}
printf( 发送的字节数:%ld \ n, outbuff);
sockOpenFailed:

解决方案

这是程序员在第一次使用TCP时所做的所有试验中的常见错误/ IP客户端/服务器。

服务器用于侦听的Internet地址是一个IP_any地址,允许服务器监听任何客户端,无论其具有哪个地址。在accept()函数中,服务器获取客户端的特定地址,并从此开始一个点对点(peer2peer)连接。

但是,在客户端,你必须指定服务器地址! 您无法使用客户端代码中的IP_any地址向网络上的任何IP地址发送连接请求:

 .... 
int nonblock = 1 ;
local.sin_family = AF_INET; // 地址系列
local.sin_addr = inet_addr(ipAddress); // (dword)0x0F0A0A0A; //通配符IP地址
local.sin_port = htons(gPort); // 在网络顺序中使用/ * 5000的端口* /
printf( soket \ n);
server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (server == - 1 ){
printf( 服务器的无效套接字\ n);
}
printf( connect\\\
);
if (connect(server,(PCSOCKADDR)& local, sizeof (local)) != 0 ){
printf( connect failed\\\
);
goto sockOpenFailed;
}
....



假设服务器IP地址是192.168.1.10,请尝试设置:

 local.sin_addr = inet_addr(  192.168.1.10);  //  服务器IP地址假定为192.168.1.10  


Hi dear,
I want create tcp client block for simulink(xpc-target) with c code but my client code don't work correct.
I used wireshark(network protocol analyzer) and saw send&receive tcp packet but connect fail.
These code run without OS.
server code is correct:

sockaddr_in from;
    int fromlen = sizeof(from);
    int length;
    int read, i, n;
    char outbuff[2048];
    char inbuff[2048];
    static int rev = 1;
    static int rev1 = 1;
    TCPMSG tcpmsg;
    int len = sizeof(sockaddr_in);
    int nonblock = 1;
    if (dothisonce) {
        local.sin_family = AF_INET;     //Address family
        local.sin_addr = inet_addr(ipAddress);  //(dword)0x0F0A0A0A; //Wild card IP address
        local.sin_port = htons(gPort);  //port to use       /* 5000 in network order */
            printf("socket\n");
        server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (server == -1) {
            printf("Invalid socket for server\n");
        }
            printf("bind\n");
        if (bind(server, (PCSOCKADDR) & local, sizeof(local)) != 0) {
            printf("bind failed\n");
            goto sockOpenFailed;
        }
            printf("listen\n");
        if (listen(server, 1) != 0) {
            printf("listen failed\n");
            goto sockOpenFailed;
        }
        ioctlsocket(server, FIONBIO, &nonblock);
        printf("XCP Server set up, waiting for connection\n");
      sockOpenFailed:
        dothisonce = 0;
    }
            printf("accept\n");
    /* if client is not active reopen the connection */
    if (server != -1 && !clientisactive) {
        if ((client = accept(server, (PSOCKADDR) & from, &fromlen))
            == -1) {
            return;
.
.
.


client code is incorrect:

sockaddr_in from;
int fromlen = sizeof(from);
int length;
int read, i, n;
char outbuff[2048];
char inbuff[2048];
static int rev = 1;
static int rev1 = 1;
TCPMSG tcpmsg;
int len = sizeof(sockaddr_in);
int nonblock = 1;
    local.sin_family = AF_INET;     //Address family
    local.sin_addr = inet_addr(ipAddress);  //(dword)0x0F0A0A0A; //Wild card IP address
    local.sin_port = htons(gPort);  //port to use       /* 5000 in network order */
        printf("soket\n");
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server == -1) {
        printf("Invalid socket for server\n");
    }
        printf("connect\n");
    if (connect(server, (PCSOCKADDR) & local, sizeof(local)) != 0) {
        printf("connect failed\n");
        goto sockOpenFailed;
    }
        printf("send\n");
    if (send(server, outbuff, 2048, 0) != 0) {
        printf("send failed\n");
        goto sockOpenFailed;
    }
    printf("Bytes Sent: %ld\n", outbuff);
    sockOpenFailed:

解决方案

This is a common error in all trials made by programmers on first approach to TCP/IP client/server.
The internet address used by server for listening is an IP_any address to permit server to listen any client whichever address it has. In the accept() function the server obtains the specific address of the client and from that on starts a point-to-point (peer2peer) connection.
But, on the client side, you must specify the server address! You can't send a connection request to any IP address on the net using an IP_any address as in your client code:

....
int nonblock = 1;
    local.sin_family = AF_INET;     //Address family
    local.sin_addr = inet_addr(ipAddress);  //(dword)0x0F0A0A0A; //Wild card IP address
    local.sin_port = htons(gPort);  //port to use       /* 5000 in network order */
        printf("soket\n");
    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server == -1) {
        printf("Invalid socket for server\n");
    }
        printf("connect\n");
    if (connect(server, (PCSOCKADDR) & local, sizeof(local)) != 0) {
        printf("connect failed\n");
        goto sockOpenFailed;
    }
....


Supposing that the server IP address is 192.168.1.10, try setting it:

local.sin_addr = inet_addr("192.168.1.10");  //Server IP address supposed 192.168.1.10


这篇关于XPC-Target c代码TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆