Omron Forums Support Posted February 17, 2015 Posted February 17, 2015 Here's an example of how to make Power PMAC a TCP/IP socket server (through a background C program) that echoes back whatever strings it receives from clients (this is a synchronous example; threading can be used to handle multiple clients and asynchronous communication): #include #include #include #include #include #include #include #include // Global Gp Shared memory pointer #include "../../Include/pp_proj.h" #define MAXPENDING 5 /* Max connection requests */ #define BUFFSIZE 32 void Die(char *mess) { perror(mess); exit(1); } // Prototypes void HandleClient(int sock); int main(int argc, char *argv[]) { { int serversock, clientsock; struct sockaddr_in server, client; InitLibrary(); if (argc != 2) { fprintf(stderr, "USAGE: server \n"); // Can pass any port here; 1024 works usually exit(1); } /* Create the TCP socket */ if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { Die("Failed to create socket"); } // Construct the server sockaddr_in structure memset(&server, 0, sizeof(server)); /* Clear struct */ server.sin_family = AF_INET; /* Internet/IP */ server.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */ server.sin_port = htons(atoi(argv[1])); /* server port */ // Bind the server socket if (bind(serversock, (struct sockaddr *) &server, sizeof(server)) < 0) { Die("Failed to bind the server socket"); } // Listen on the server socket if (listen(serversock, MAXPENDING) < 0) { Die("Failed to listen on server socket"); } // Run until cancelled while (1) { unsigned int clientlen = sizeof(client); // Wait for client connection if ((clientsock = accept(serversock, (struct sockaddr *) &client, &clientlen)) < 0) { Die("Failed to accept client connection"); } fprintf(stdout, "Client connected: %s\n", inet_ntoa(client.sin_addr)); HandleClient(clientsock); } } CloseLibrary(); return 0; } // HandleClient operates once the server accepts connection with the client void HandleClient(int sock) { char buffer[bUFFSIZE]; int received = -1; /* Receive message */ if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { Die("Failed to receive initial bytes from client"); } /* Send bytes and check for more incoming data in loop */ while (received > 0) { /* Send back received data */ if (send(sock, buffer, received, 0) != received) { Die("Failed to send bytes to client"); } /* Check for more data */ if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { Die("Failed to receive additional bytes from client"); } } close(sock); } When you run the .out file after compiling this program, pass it the port number to which you want the program to listen.
piefum Posted February 18, 2015 Posted February 18, 2015 Here's an example of how to make Power PMAC a TCP/IP socket server (through a background C program) that echoes back whatever strings it receives from clients When you run the .out file after compiling this program, pass it the port number to which you want the program to listen. Hi Charles, thanks for the great tip. As you might remember, I tried to do a similar thing, using the ZeroMQ C libraries (see http://forums.deltatau.com/showthread.php?tid=910). At the moment I cannot compile under the IDE (it cannot link the libraries), so I compile it directly inside the PMAC. Is this problem solved in the IDE 2.0? Unfortunately right now I am delivering a product so I don't have time to setup a new machine to test the IDE2.0. Thanks gigi
Omron Forums Support Posted February 18, 2015 Author Posted February 18, 2015 There were a few compiler issues that have been fixed in the new (patched) version of the IDE. You may want to consider trying it again within the IDE in the future. Note that there will be a "patch" released that will fix a few things in version 2.0.
Recommended Posts