How to handle multiple local communication socketss without using AF_INET or soc
ID: 659179 • Letter: H
Question
How to handle multiple local communication socketss without using AF_INET or sockaddr_in, but AF_UNIX and sockaddr_un. So far I can handle 1 socket at a time.
SERVER.C
#include
#include
#include
#include
#include
#include
#include define SOCKET_NAME3 socket3
void doprocessing (int sock);
int main( int argc, char *argv[] )
{
int sockfd, newsockfd, clilen;
char buffer[256];
struct sockaddr_un serv_addr, cli_addr;
int n, pid;
/* First call to socket() function */
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
unlink(SOCKET_NAME3);
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, SOCKET_NAME3);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1)
{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0)
{
perror("ERROR on fork");
exit(1);
}
if (pid == 0)
{
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
exit(0);
}
else
{
close(newsockfd);
}
} /* end of while */
}
void doprocessing (int sock)
{
int n;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0)
{
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s ",buffer);
n = write(sock,"I got your message",18);
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
}
______________________________________________________________________________________
CLIENT.C
#include
#include
#include
#include
#include
#include
#include define SOCKET_NAME3 socket3
int main()
{
int client_sockfd;
int len;
struct sockaddr_un address;
int result;
char ch = 'A';
// Create a socket for the client.
client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
// Name the socket.)
address.sun_family = AF_UNIX;
strcpy(address.sun_path, SOCKET_NAME3);
len = sizeof(address);
// Connect our socket to the server's socket.
result = connect(client_sockfd, (struct sockaddr *)&address, len);
if (result == -1) {
perror("Error: client1");
exit(1);
}
// Write 'A' to the socket via sockfd.
printf("Writing '%c' to the socket. ", 'A');
write(client_sockfd, &ch, 1);
// Read what the server sent back.
read(client_sockfd, &ch, 1);
printf("Read '%c' from the server. ", ch);
close(client_sockfd);
exit(0);
}
_______________________________________________________________________________________
Explanation / Answer
Server Code
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
//char *socket_path = "./socket";
char *socket_path = "hidden";
int main(int argc, char *argv[]) {
struct sockaddr_un addr;
char buf[100];
int fd,cl,rc;
if (argc > 1) socket_path=argv[1];
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket error");
exit(-1);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
unlink(socket_path);
if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("bind error");
exit(-1);
}
if (listen(fd, 5) == -1) {
perror("listen error");
exit(-1);
}
while (1) {
if ( (cl = accept(fd, NULL, NULL)) == -1) {
perror("accept error");
continue;
}
while ( (rc=read(cl,buf,sizeof(buf))) > 0) {
printf("read %u bytes: %.*s ", rc, rc, buf);
}
if (rc == -1) {
perror("read");
exit(-1);
}
else if (rc == 0) {
printf("EOF ");
close(cl);
}
}
return 0;
}
Client Code
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//char *socket_path = "./socket";
char *socket_path = "hidden";
int main(int argc, char *argv[]) {
struct sockaddr_un addr;
char buf[100];
int fd,rc;
if (argc > 1) socket_path=argv[1];
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket error");
exit(-1);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect error");
exit(-1);
}
while( (rc=read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
if (write(fd, buf, rc) != rc) {
if (rc > 0) fprintf(stderr,"partial write");
else {
perror("write error");
exit(-1);
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.