Results 1 to 3 of 3
Thread: C
Hybrid View
-
16th March 2011 23:33 #1_
Join Date: Apr:2006
Location: _
Posts: 1,128
C
,
source code. Unix Domain Socket. . .
:
Code:#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <syslog.h> #include <string.h> #include <sys/socket.h> #include <sys/un.h> #include <signal.h> #include <pthread.h> #define NTHREADS 5 void *thread_function(void *); int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_un server_address; struct sockaddr_un client_address; void *thread_function(void *dummyPtr){ /* Accept connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); /* Accept data */ char ch; read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); } int main(void) { /* Our process ID and Session ID */ pid_t pid, sid; /* Fork off the parent process */ pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); } /* Change the file mode mask */ umask(0); /* Open any logs here */ /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); /* Daemon-specific initialization goes here */ /* The Big Loop */ while (1) { /* Remove any old socket and create an unnamed socket for the server. */ unlink("/tmp/server_socket"); server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); /* Name the socket. */ server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "/tmp/server_socket"); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /* Create a connection queue and wait for clients. */ listen(server_sockfd, 5); signal(SIGCHLD, SIG_IGN); while(1) { /* Create threads */ pthread_t thread_id[NTHREADS]; int i, j; for(i=0; i < NTHREADS; i++) { pthread_create( &thread_id[i], NULL, thread_function, NULL ); } for(j=0; j < NTHREADS; j++) { pthread_join( thread_id[j], NULL); } close(client_sockfd); } } exit(EXIT_SUCCESS); }
:
?Code:/* Make the necessary includes and set up the variables. */ #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> int main() { int sockfd; int len; struct sockaddr_un address; int result; char ch = 'B'; /* Create a socket for the client. */ sockfd = socket(AF_UNIX, SOCK_STREAM, 0); /* Name the socket, as agreed with the server. */ address.sun_family = AF_UNIX; strcpy(address.sun_path, "/tmp/server_socket"); len = sizeof(address); /* Now connect our socket to the server's socket. */ result = connect(sockfd, (struct sockaddr *)&address, len); if(result == -1) { perror("oops: client1"); exit(1); } /* We can now read/write via sockfd. */ write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from server = %c\n", ch); close(sockfd); exit(0); }Last edited by Red_Leader; 17th March 2011 at 21:37.
-
17th March 2011 12:56 #2
, .
... . : 5 . exit() pthread_exit(). , unreachable .
:
accept client_sockfd , accept ..
-
17th March 2011 21:39 #3_
Join Date: Apr:2006
Location: _
Posts: 1,128
. .
. ?Last edited by Red_Leader; 17th March 2011 at 21:51.




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in