I'm using sockets in C to write 10 bytes to a socket and then read 10 bytes from that socket. I create a parent process that forks, creating a child that acts as the socket server that writes the 10 bytes. After the 10 bytes are wrote, back in the parent, those 10 bytes get read from the socket. This is what is supposed to happen. My current output just shows the server "waiting". I'm not sure what's causing the hangup between the parent/child but any help or suggestions would be much appreciated. Here's my code below.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <time.h>
#include <sys/wait.h>
int main() {
int counter=-1;
int data_processed;
int sockfd;
int len;
int result;
int x;
int i;
int server_sockfd;
int client_sockfd;
int server_len;
int fork_result;
int stat_val;
char buffer[BUFSIZ + 1];
const char some_data[] = "Cat in Hat";
const char null_msg[] = "\0";
pid_t pid_parent;
pid_t pid_child;
pid_parent=getpid();
srand( time(NULL) );
x = rand() % 7 + 3;
unsigned int client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;
struct sockaddr_un address;
fork_result = fork();
if (fork_result == 0) {
pid_child=getpid();
unlink("server_socket");
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
listen(server_sockfd, 5);
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, &client_len);
for(i = 0; i < x; i++) {
data_processed = write(client_sockfd, some_data,strlen(some_data));
printf("%d - Wrote %d bytes\n",pid_child, data_processed);
}
data_processed = write(client_sockfd, null_msg, 1);
return (x);
}
pid_child=wait(&stat_val);
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
address.sun_family = AF_UNIX;
strcpy(address.sun_path, "server_socket");
len = sizeof(address);
result = connect(sockfd, (struct sockaddr*)&address, len);
while(1) {
counter++;
memset(buffer, '\0', sizeof(buffer));
data_processed = read(sockfd, buffer, 10);
if(buffer[0] == 0) {
break;
}
printf("%d - Read %d bytes: %s\n", pid_parent, data_processed, buffer);
}
close(sockfd);
exit (0);
}