I have a very simple program to create a local socket shared between 2
processes.
This is the client side, that will open and write the data...
void write_text (int socket_fd, const char* text)
{
int length = strlen (text) + 1;
write (socket_fd, &length, sizeof (length));
write (socket_fd, text, length);
int main (int argc, char* const argv[])Quote:}
{
const char* const socket_name = argv[1];
const char* const message = argv[2];
int socket_fd;
struct sockaddr_un name;
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
connect (socket_fd, (struct sockaddr *) &name, SUN_LEN (&name));
write_text (socket_fd, message);
close (socket_fd);
return 0;
This is the server side.Quote:}
int server (int client_socket)
{
while (1) {
int length;
char* text;
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
printf("length %d\n",length);
length = 15; /* prevent code dump */
text = (char*) malloc (length);
read (client_socket, text, length);
printf ("%s\n", text);
}
int main (int argc, char* const argv[])Quote:}
{
const char* const socket_name = argv[1];
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, (struct sockaddr *) &name, SUN_LEN (&name));
listen (socket_fd, 5);
do {
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
client_socket_fd = accept (socket_fd, (struct sockaddr *)
&client_name, &client_name_len);
client_sent_quit_message = server (client_socket_fd);
close (client_socket_fd);
}
while (!client_sent_quit_message);
close (socket_fd);
unlink (socket_name);
return 0;
On the server side, the length is ALWAYS retured as a negative numberQuote:}
when the client sends the length.
Even if I force the length, nothing is read for the second read.
Really strange. Do you think it is my linux setup?
I use /tmp/socket as the device as in
socket-server /tmp/socket and
socket-client /tmp/socket "this is a test"
TIA