Created
August 4, 2017 18:30
-
-
Save sudikrt/26ec55e87c25f9bb31f5479bde964486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <sys/socket.h> | |
| #include <sys/un.h> | |
| #include <unistd.h> | |
| #include <stdlib.h> | |
| /* Function to write the data to the socket */ | |
| int write_socket (int socket_fd, const char* text) | |
| { | |
| int length = strlen (text) + 1; | |
| /* Write the length of the message */ | |
| write (socket_fd, &length, sizeof (length)); | |
| /* Write the string. */ | |
| write (socket_fd, text, length); | |
| } | |
| int main (int argc, char* const argv []) | |
| { | |
| const char* const socket_name = argv [1]; | |
| const char* const message = argv [2]; | |
| int socket_fd; | |
| struct sockaddr_un name; | |
| /* Creates the socket for the connection */ | |
| socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); | |
| name.sun_family = AF_LOCAL; | |
| strcpy (name.sun_path, socket_name); | |
| /* Connects to the server socket */ | |
| connect (socket_fd, (struct sockaddr*) &name, SUN_LEN (&name)); | |
| /* Writes the message to the server socket */ | |
| int i = write_socket (socket_fd, message); | |
| sleep (60); | |
| close (socket_fd); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/socket.h> | |
| #include <sys/un.h> | |
| #include <unistd.h> | |
| #include <pthread.h> | |
| void *server (void *args) | |
| { | |
| int ret = -1; | |
| int length; | |
| char* text; | |
| int client_socket = *((int *)args); | |
| printf ("Client thread start : %d\n", client_socket); | |
| /* Read the length of the message from the client */ | |
| if (read (client_socket, &length, sizeof (length)) == 0) | |
| { | |
| ret = 0; | |
| goto out; | |
| } | |
| /* Allocate a buffer to hold the text. */ | |
| text = (char*) malloc (length); | |
| /* Read the text itself, and print it. */ | |
| read (client_socket, text, length); | |
| printf ("%s\n", text); | |
| sleep (120); | |
| /* Free the buffer. */ | |
| free (text); | |
| out: | |
| printf ("Client thread end : %d\n", client_socket); | |
| close (client_socket); | |
| return NULL; | |
| } | |
| int main (int argc, char* const argv []) | |
| { | |
| const char* const socket_name = argv[1]; | |
| int socket_fd; | |
| struct sockaddr_un name; | |
| int client_sent; | |
| pthread_t server_thread = 0; | |
| /* Initiates the server socket */ | |
| socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); | |
| name.sun_family = AF_LOCAL; | |
| strcpy (name.sun_path, socket_name); | |
| /* Binds the socket */ | |
| bind (socket_fd, (struct sockaddr*) &name, SUN_LEN (&name)); | |
| /* Listens for the client connection */ | |
| listen (socket_fd, 2); | |
| do | |
| { | |
| struct sockaddr_un client_name; | |
| socklen_t client_name_len; | |
| int client_socket_fd; | |
| /* Accept the clients */ | |
| client_socket_fd = accept (socket_fd, (struct sockaddr*) &client_name, &client_name_len); | |
| if (client_socket_fd!=-1) { | |
| printf ("client accept : %d\n ", client_socket_fd); | |
| /* Process the client */ | |
| client_sent = pthread_create(&server_thread, NULL, server, &client_socket_fd); | |
| printf ("Client thread created : %d\n", client_socket_fd); | |
| printf ("Proceeding to listen more !!!!\n"); | |
| } | |
| /* Closes the client socket*/ | |
| //close (client_socket_fd); | |
| }while (1); | |
| /* Closes the server socket */ | |
| close (socket_fd); | |
| /* Unlinks the server socket */ | |
| unlink (socket_name); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment