Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
C++
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:
C++
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:
Posted
Updated 25-Jun-15 21:01pm
v3

1 solution

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:
C++
....
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:
C++
local.sin_addr = inet_addr("192.168.1.10");  //Server IP address supposed 192.168.1.10
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900