For what it’s worth, here’s the tcp client code. It seems to be working fine. I’ve tested it with another server.
* Copyright Mark Watson 1999. Open Source Software License.
* Note: derived from NLPserver client example.
* See www.markwatson.com/opensource/opensource.htm for all
* of the natural language server (NLPserver) source code.
*
* Updated by Ray Depew, December 2017
*/
#include <stdio.h>
#include <stdlib.h> // exit()
#include <string.h> // memset()
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> // close()
#include <netdb.h>
char * host_name = "192.168.0.131"; // localhost
// char * host_name = "127.0.0.1"; // localhost
int port = 3230;
void main(int argc, char *argv[])
{
char buf[8192];
char message[256];
int socket_descriptor;
struct sockaddr_in pin;
struct hostent *server_host_name;
char * str = "A default test string";
if (argc < 2) {
printf("Usage: 'test \"Any test string\"\n");
printf("We will send a default test string.\n");
} else {
str = argv[1];
}
if ((server_host_name = gethostbyname(host_name)) == 0) {
perror("Error resolving local host\n");
exit(1);
}
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
pin.sin_port = htons(port);
if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error opening socket\n");
exit(1);
}
if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
perror("Error connecting to socket\n");
exit(1);
}
printf("Sending message %s to server...\n", str);
/* FIXME: Delete after debug */
int message_len = strlen(str);
printf("(%d bytes)\n", message_len);
if (send(socket_descriptor, &str, strlen(str), 0) == -1) {
perror("Error in send");
exit(1);
}
printf("..sent message.. wait for response...\n");
if (recv(socket_descriptor, buf, 8192, 0) == -1) {
perror("Error in receiving response from server\n");
exit(1);
}
printf("\nResponse from server:\n%s\n", buf);
close(socket_descriptor);
}