Hi, I have a problem to send a simple message to an existing TCP server, using Julia Sockets module.
I used one of the “C” examples on the web to test if I can make a connection and send/receive messages to/from the server. This works fine. This C code (called client_c.c) is below:
#include<stdio.h> //printf
#include<string.h> //strlen
#include <unistd.h>
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int sock;
int proto = 6; // TCP protocol
struct sockaddr_in server;
char message[100] , server_reply[1024];
//Create socket
sock = socket(PF_INET , SOCK_STREAM , IPPROTO_TCP);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("198.214.229.46");
server.sin_family = AF_INET;
server.sin_port = htons( 22401 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
memcpy(message,"iraf", 4);
if ( send(sock,message,100,0) < 0 )
{
puts("Failed to send message");
return 1;
}
if ( recv(sock, server_reply, 1024, 0) < 0 )
{
puts("Failed to receive message");
}
printf("%s\n",server_reply);
close(sock);
return 0;
}
After compiled to an executable called client_c and upon running it results in the following:
shell> ../../client_c
Socket created
Connected
2 6 Thu Feb 10 2022( 41) 18:15:35.1 0 0 0
LST/HA +20:42:42.34 - 0: 0: 0.37 180.0 50.8 1.5792
MEAN +20:41:28.96 -20:11:12.6 2000.0 0.000 0.00
APP +20:42:42.99 -20: 6:36.9 2022.1
REFRA +20:42:42.71 -19:59:55.8 2022.1
RAW +20:42:42.71 -19:59:55.8 2022.1
107 F 36974
The last 7 rows are the messages from this TCP server.
Now, I tried to do the same thing in Julia by the following:
(Please note that I am pretty new to this).
# Import Sockets module
using Sockets
# Establish socket connection to the TCP server
tcs = connect(TCPSockets(;delay=false), IPv4("192.214.229.46"), 22401)
# Create 100 byte Char array with command "iraf"
a = Array{Char,1}(undef,100)
a[1:4] = collect("iraf")
# Write the message to the socket
write(tcs,String(a))
# Read message back from the server
read(tcs)
close(tcs)
Please note that the TCP server expects a message from a client to be 100 byte long string and that is why I created char array “a” of length 100, put the message, and converting it to String when writing it to the socket.
When running this in REPL, write(tcs,String(a)) return 100 which is the length of the message, but it hangs at read(tcs) line. Upon re running it up to write(tcs,String(a)), I saw tcs having no bytes. I am supposing that this is why read() hangs, but not sure whether the way I am connecting to the server in Julia is identical to that in client_c.c (?).
Also note that this TCP server is quite old and is a complete black box…
I really wish experts can give a pointer to a possible resolution.
I appreciate any help.