فهرست منبع

socket programm

vishnuprasath 1 هفته پیش
والد
کامیت
03e8408f1a

+ 75 - 0
file_transfer/client_file_transfer.c

@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <fcntl.h>
+
+#define PORT 12345
+#define CHUNK_SIZE 1024
+
+void handle_error(const char *msg) {
+        perror(msg);
+            exit(1);
+}
+
+int main()
+{
+    int client_fd, file_fd;
+    struct sockaddr_in server_addr;
+    char buffer[CHUNK_SIZE];
+    char filename[256];
+    ssize_t bytes_received, bytes_written;
+    client_fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (client_fd < 0)
+    {
+        handle_error("Socket creation failed");
+    }
+    memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_port = htons(PORT);
+    if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0)
+    {
+        handle_error("Invalid address");
+    }
+    if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+    {
+        handle_error("Connection failed");
+    }
+    // Ask for the filename from the user
+    printf("Enter the filename you want to request: ");
+    fgets(filename, sizeof(filename), stdin);
+    filename[strcspn(filename, "\n")] = 0; // Remove newline character
+    // Send the filename to the server
+    if (send(client_fd, filename, strlen(filename), 0) < 0)
+    {
+        handle_error("Failed to send filename");
+    }
+   // Open a file to write the received data
+   strcpy(filename, "HelloWorld.txt");
+   file_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+   if (file_fd < 0)
+   {
+       handle_error("File creation failed");
+   }
+   // Receive the file in chunks and write to disk
+   while ((bytes_received = recv(client_fd, buffer, CHUNK_SIZE, 0)) > 0)
+   {
+        bytes_written = write(file_fd, buffer, bytes_received);
+        if (bytes_written < 0)
+        {
+            perror("Write to file failed");
+            break;
+        }
+   }
+  if (bytes_received < 0)
+  {
+       perror("Receive failed");
+  } else {
+  printf("File received successfully\n");
+     }
+close(file_fd);
+close(client_fd);
+return 0;
+}

+ 90 - 0
file_transfer/server_file_transfer.c

@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <fcntl.h>
+
+#define PORT 12345
+#define CHUNK_SIZE 1024
+
+void handle_error(const char *msg) {
+        perror(msg);
+            exit(1);
+}
+
+int main()
+{
+    int server_fd, client_fd, file_fd;
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_len;
+    char buffer[CHUNK_SIZE];
+    char filename[256];
+    ssize_t bytes_read, bytes_sent;
+    server_fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (server_fd < 0)
+    {
+        handle_error("Socket creation failed");
+    }
+    memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;
+    server_addr.sin_port = htons(PORT);
+    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+    {
+         handle_error("Bind failed");
+    }
+   if (listen(server_fd, 5) < 0)
+    {
+         handle_error("Listen failed");
+    }
+    printf("Server listening on port %d...\n", PORT);
+    while (1)
+    {
+        client_len = sizeof(client_addr);
+        client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
+        if (client_fd < 0)
+        {
+           perror("Accept failed");
+           continue;
+        }
+        printf("Client connected\n");
+       // Receive the filename request from the client
+       memset(filename, 0, sizeof(filename));
+       if (recv(client_fd, filename, sizeof(filename), 0) <= 0)
+       {
+            perror("Failed to receive filename");
+            close(client_fd);
+            continue;
+        }
+       // Open the file requested by the client
+        file_fd = open(filename, O_RDONLY);
+        if (file_fd < 0)
+        {
+              perror("File not found");
+              close(client_fd);
+              continue;
+         }
+        // Send the file in chunks
+          while ((bytes_read = read(file_fd, buffer, CHUNK_SIZE)) > 0) {
+          bytes_sent = send(client_fd, buffer, bytes_read, 0);
+          if (bytes_sent < 0)
+          {
+             perror("Send failed");
+            close(file_fd);
+            close(client_fd);
+            break;
+          }
+        }
+       if (bytes_read < 0) {
+        perror("File read failed");
+      } else {
+      printf("File sent successfully\n");
+      }
+      close(file_fd);
+      close(client_fd);
+    }
+close(server_fd);
+return 0;
+}

+ 60 - 0
milti_thread/client.c

@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#define SERVER_ADDR "127.0.0.1"
+#define PORT 8083
+#define BUFFER_SIZE 1024
+
+int main()
+{
+int client_socket;
+struct sockaddr_in server_addr={0};
+char buffer[BUFFER_SIZE];
+// Create socket
+if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+{
+    perror("Socket creation failed");
+    exit(EXIT_FAILURE);
+}
+// Define server address
+server_addr.sin_family = AF_INET;
+server_addr.sin_port = htons(PORT);
+server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
+// Convert IP address to binary form
+//if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0)
+//{
+  //  perror("Invalid address");
+    //exit(EXIT_FAILURE);
+//}
+// Connect to the server
+if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+{
+    perror("Connection failed");
+    close(client_socket);
+    exit(EXIT_FAILURE);
+}
+
+printf("Connected to server\n");
+//for(int i=0;i<6;i++)
+  //  {
+    // Send a message to the server
+    const char *message = "testing!";
+    send(client_socket, message, strlen(message), 0);
+        printf("Message sent to server: %s\n",message );
+    //}
+//Receive server's response
+int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
+if (n < 0)
+{
+    perror("Receive failed");
+    close(client_socket);
+    exit(EXIT_FAILURE);
+}
+buffer[n] = '\0';  // Null-terminate the received string
+printf("Received from server: %s\n", buffer);
+// Close the socket
+close(client_socket);
+return 0;
+}

+ 112 - 0
milti_thread/server_tcp_mthread.c

@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <arpa/inet.h>
+
+#define PORT 8081
+#define BUFFER_SIZE 1024
+
+// structure to store client data
+typedef struct {
+    int client_socket;
+    struct sockaddr_in client_addr;
+} client_data_t;
+//function to handle thread
+void *handle_client(void *arg)
+{
+    client_data_t *data = (client_data_t *)arg;
+    int client_socket = data->client_socket;
+    struct sockaddr_in client_addr = data->client_addr;
+    char buffer[BUFFER_SIZE]={0};
+    int n;
+    printf("client connected\n");
+    while(1)
+    {
+        n = recv(client_socket, buffer, sizeof(buffer), 0);
+        if (n <= 0)
+        {
+            printf("Client disconnected\n");
+            break;
+        }
+         printf("Received from client: %s\n", buffer);
+         send(client_socket, buffer, strlen(buffer), 0);
+    }
+    close(client_socket);
+    free(data); // Free the memory allocated for client data
+    pthread_exit(NULL);
+
+}
+void main()
+{
+    int server_socket, client_socket;
+    struct sockaddr_in server_addr={0}, client_addr;
+    socklen_t client_len = sizeof(client_addr);
+    pthread_t thread_id;
+    // create server socket
+    if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    // server address setup
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;  // Listen on all interfaces
+    server_addr.sin_port = htons(PORT);
+    //bind the server socket to port_no and ip
+    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+    {
+        perror("Bind failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    //listen for connection
+    if (listen(server_socket, 5) < 0)
+    {
+        perror("Listen failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("server listening\n");
+
+    while(1)
+    {
+        // accept incomming client
+        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
+        if (client_socket < 0)
+        {
+            perror("Client accept failed");
+            continue;  // Continue to accept new clients
+        }
+        //allocate memory for client structure 
+        client_data_t *data = malloc(sizeof(client_data_t));
+        data->client_socket = client_socket;
+        data->client_addr = client_addr;
+        //create new thread to handle every new client 
+        if (pthread_create(&thread_id, NULL, handle_client, data) != 0)
+        {
+            perror("Thread creation failed");
+            close(client_socket);
+            free(data);
+        } else
+        {
+            //Detach the thread to allow it to clean up automatically after finishing
+            pthread_detach(thread_id);
+        }
+    }
+     close(server_socket);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 84 - 0
non_blocking_client/client_non_blocking.c

@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/select.h>
+
+#define SERVER_HOST "127.0.0.1"
+#define SERVER_PORT 12345
+
+int main()
+{
+    int sockfd;
+    struct sockaddr_in server_addr;
+    fd_set write_fds;
+    struct timeval timeout;
+    int flags, result;
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd == -1)
+    {
+        perror("socket");
+        return 1;
+    }
+    memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_port = htons(SERVER_PORT);
+    if (inet_pton(AF_INET, SERVER_HOST, &server_addr.sin_addr) <= 0)
+    {
+        perror("inet_pton");
+        close(sockfd);
+        return 1;
+    }
+    flags = fcntl(sockfd, F_GETFL, 0);
+    if (flags == -1)
+    {
+        perror("fcntl");
+        close(sockfd);
+        return 1;
+    }
+    if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
+    {
+        perror("fcntl");
+        close(sockfd);
+        return 1;
+    }
+    result = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
+    if (result < 0 && errno != EINPROGRESS)
+    {
+        perror("connect");
+        close(sockfd);
+        return 1;
+    }
+    printf("Trying to connect...\n");
+    while (1)
+    {
+        FD_ZERO(&write_fds);
+       // FD_SET(sockfd, &write_fds);
+        timeout.tv_sec = 0;
+        timeout.tv_usec = 100000;
+        result = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
+        if (result == -1)
+        {
+            perror("select");
+            close(sockfd);
+            return 1;
+        }
+        if (FD_ISSET(sockfd, &write_fds))
+        {
+            printf("Successfully connected to %s:%d\n", SERVER_HOST, SERVER_PORT);
+            break;
+        }
+        else
+        {
+            printf("Still attempting to connect...\n");
+            sleep(1);                                                                                                             }
+    }
+    close(sockfd);
+    return 0;
+}
+

+ 68 - 0
non_blocking_client/server.c

@@ -0,0 +1,68 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 12345
+#define BUFFER_SIZE 1024
+
+int main() 
+{
+    int server_socket, client_socket;
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_addr_len;
+    char buffer[BUFFER_SIZE];
+    if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    // Define server address
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;
+    server_addr.sin_port = htons(PORT); 
+    // Bind the socket to the address and port
+    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 
+    {
+        perror("Bind failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    // Listen for incoming connections
+    if (listen(server_socket, 5) < 0)
+    {
+         perror("Listen failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Server listening on port %d...\n", PORT);
+    // Accept incoming client connection
+    client_addr_len = sizeof(client_addr);
+    if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) < 0)
+    {
+        perror("Client connection failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Client connected. Waiting for message...\n");
+    // Receive message from the client
+    //int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
+    //if (n < 0)
+   // {
+     //   perror("Receive failed");
+       // close(client_socket);
+       // close(server_socket);
+       // exit(EXIT_FAILURE);
+     //}
+    //buffer[n] = '\0';  // Null-terminate the received string
+    //printf("Received from client: %s\n", buffer);
+    // Send response to the client
+    //const char *response = "Hello, Client! Your message was received.";
+    //send(client_socket, response, strlen(response), 0);
+   // printf("Response sent to client.\n");
+    // Close the sockets
+    close(client_socket);
+    close(server_socket);
+    return 0;
+    } 

+ 77 - 0
select/client_tcp_select.c

@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/select.h>
+
+#define BUFFER_SIZE 256
+
+void error(const char *msg) {
+        perror(msg);
+            exit(1);
+}
+int main()
+{
+    int client_fd;
+    struct sockaddr_in server;
+    fd_set readfds;
+    char buffer[BUFFER_SIZE];
+    // Create a TCP socket
+    client_fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (client_fd < 0)
+    {
+        error("Error opening socket.");
+    }
+    server.sin_family = AF_INET;
+    server.sin_addr.s_addr = inet_addr("127.0.0.1"); // Change this to server's IP address
+    server.sin_port = htons(5000);
+     // Connect to the server
+    if (connect(client_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
+    {
+            error("Connection error");
+    }
+    printf("Connected to the server.\n");
+    while (1)
+    {
+        // Clear the socket set and add client socket to it
+        FD_ZERO(&readfds);
+        FD_SET(client_fd, &readfds);
+        FD_SET(STDIN_FILENO, &readfds); // Add standard input to readfds
+        // Wait for an activity on one of the sockets
+        int activity = select(client_fd + 1, &readfds, NULL, NULL, NULL);
+        if (activity < 0)
+        {
+            error("Select error");
+        }
+        //If data is received from the server
+        if (FD_ISSET(client_fd, &readfds))
+        {
+            int valread = read(client_fd, buffer, sizeof(buffer));
+            if (valread > 0)
+            {
+                buffer[valread] = '\0'; // Null-terminate the received string
+                printf("Broadcast from server: %s\n", buffer);
+            }
+        }
+        // If input is available from the console
+        if (FD_ISSET(STDIN_FILENO, &readfds))
+        {
+            fgets(buffer, BUFFER_SIZE, stdin);
+            buffer[strcspn(buffer, "\n")] = 0; // Remove trailing newline character
+            send(client_fd, buffer, strlen(buffer), 0);
+        }
+    }
+        close(client_fd);
+        return 0;
+}
+
+
+
+
+
+
+

+ 135 - 0
select/server_tcp_select.c

@@ -0,0 +1,135 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/select.h>
+
+#define PORT 5000
+#define MAX_CLIENTS 10
+#define BUFFER_SIZE 256
+void error(const char *msg) {
+        perror(msg);
+            exit(1);
+}
+void main()
+{
+    int server_fd;
+    int client_fd[MAX_CLIENTS] = {0}; // Array to store client socket descriptors
+    struct sockaddr_in server, client;
+    fd_set readfds, copyfds;
+    int max_fd, new_socket, activity, i, valread, sd;
+    socklen_t addr_len = sizeof(client);
+    char buffer[BUFFER_SIZE];
+    // Create a TCP socket
+    server_fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (server_fd < 0)
+        {
+            error("Error opening socket.");
+        }
+     // Prepare the sockaddr_in structure
+    server.sin_family = AF_INET;
+    server.sin_addr.s_addr = INADDR_ANY;
+    server.sin_port = htons(PORT);
+    // Allow reuse of addresses
+    int opt = 1;
+    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+    // Bind the socket to the port
+    if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
+    {
+        error("Binding failed");
+    }
+    // Listen for incoming connections
+    if (listen(server_fd, 5) < 0)
+    {
+        error("Listening failed");
+    }
+    printf("Listening on port %d...\n", PORT);
+    while(1)
+    {
+        FD_ZERO(&readfds);
+        FD_SET(server_fd, &readfds);
+        max_fd = server_fd;
+        // Add child sockets to the set
+        for (i = 0; i < MAX_CLIENTS; i++)
+        {
+            sd = client_fd[i];
+            if (sd > 0)
+            {
+                FD_SET(sd, &readfds);
+            }
+            if (sd > max_fd)
+            {
+                max_fd = sd;
+            }
+        }
+         // Wait for an activity on one of the sockets
+        activity = select(max_fd + 1, &readfds, NULL, NULL, NULL);
+        if (activity < 0)
+        {
+            error("Select error");
+        }
+        // If something happened on the server socket, then it's an incoming connection
+         if (FD_ISSET(server_fd, &readfds))
+        {
+            new_socket = accept(server_fd, (struct sockaddr *)&client, &addr_len);
+            if (new_socket < 0)
+            {
+                error("Accept error");
+            }
+            printf("New connection from %s on socket %d\n", inet_ntoa(client.sin_addr), new_socket);
+            // Add new socket to array of sockets
+            for (i = 0; i < MAX_CLIENTS; i++)
+            {
+                if (client_fd[i] == 0)
+                {
+                    client_fd[i] = new_socket;
+                    break;
+                }
+            }
+        }
+          // Check all clients for incoming messages
+          for (i = 0; i < MAX_CLIENTS; i++)
+          {
+                sd = client_fd[i];
+                if (FD_ISSET(sd, &readfds))
+                {
+                // Check if it was for closing, and also read the incoming message
+                valread = read(sd, buffer, sizeof(buffer));
+                if (valread > 0)
+                {
+                    buffer[valread] = '\0'; // Null-terminate the received string
+                    printf("Message from client: %s\n", buffer);
+                    // Broadcast the message to all other clients
+                    for (int j = 0; j < MAX_CLIENTS; j++)
+                    {
+                        if (client_fd[j] != 0 && client_fd[j] != sd)
+                        {
+                            send(client_fd[j], buffer, strlen(buffer), 0);
+                        }
+                    }
+                }
+                }
+            }
+        }
+    close(server_fd);
+   // return 0;
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 60 - 0
tcp_client_server/client.c

@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#define SERVER_ADDR "127.0.0.1"
+#define PORT 8083
+#define BUFFER_SIZE 1024
+
+int main()
+{
+int client_socket;
+struct sockaddr_in server_addr={0};
+char buffer[BUFFER_SIZE];
+// Create socket
+if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+{
+    perror("Socket creation failed");
+    exit(EXIT_FAILURE);
+}
+// Define server address
+server_addr.sin_family = AF_INET;
+server_addr.sin_port = htons(PORT);
+server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
+// Convert IP address to binary form
+//if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0)
+//{
+  //  perror("Invalid address");
+    //exit(EXIT_FAILURE);
+//}
+// Connect to the server
+if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+{
+    perror("Connection failed");
+    close(client_socket);
+    exit(EXIT_FAILURE);
+}
+
+printf("Connected to server\n");
+//for(int i=0;i<6;i++)
+  //  {
+    // Send a message to the server
+    const char *message = "testing!";
+    send(client_socket, message, strlen(message), 0);
+        printf("Message sent to server: %s\n",message );
+    //}
+//Receive server's response
+int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
+if (n < 0)
+{
+    perror("Receive failed");
+    close(client_socket);
+    exit(EXIT_FAILURE);
+}
+buffer[n] = '\0';  // Null-terminate the received string
+printf("Received from server: %s\n", buffer);
+// Close the socket
+close(client_socket);
+return 0;
+}

+ 68 - 0
tcp_client_server/server.c

@@ -0,0 +1,68 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 12345
+#define BUFFER_SIZE 1024
+
+int main() 
+{
+    int server_socket, client_socket;
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_addr_len;
+    char buffer[BUFFER_SIZE];
+    if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    // Define server address
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;
+    server_addr.sin_port = htons(PORT); 
+    // Bind the socket to the address and port
+    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 
+    {
+        perror("Bind failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    // Listen for incoming connections
+    if (listen(server_socket, 5) < 0)
+    {
+         perror("Listen failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Server listening on port %d...\n", PORT);
+    // Accept incoming client connection
+    client_addr_len = sizeof(client_addr);
+    if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) < 0)
+    {
+        perror("Client connection failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Client connected. Waiting for message...\n");
+    // Receive message from the client
+    //int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
+    //if (n < 0)
+   // {
+     //   perror("Receive failed");
+       // close(client_socket);
+       // close(server_socket);
+       // exit(EXIT_FAILURE);
+     //}
+    //buffer[n] = '\0';  // Null-terminate the received string
+    //printf("Received from client: %s\n", buffer);
+    // Send response to the client
+    //const char *response = "Hello, Client! Your message was received.";
+    //send(client_socket, response, strlen(response), 0);
+   // printf("Response sent to client.\n");
+    // Close the sockets
+    close(client_socket);
+    close(server_socket);
+    return 0;
+    } 

+ 116 - 0
timeout_send_rec/client_non_blocking_SR.c

@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <sys/select.h>
+
+#define SERVER_HOST "127.0.0.1"
+#define SERVER_PORT 12345
+#define SEND_TIMEOUT_SEC 5
+#define RECV_TIMEOUT_SEC 5
+
+int main()
+{
+    int sockfd;
+    struct sockaddr_in server_addr;
+    struct timeval send_timeout, recv_timeout, timeout;
+    fd_set read_fds, write_fds;
+    int result;
+    char send_buffer[] = "Hello, Server!";
+    char recv_buffer[1024];
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0)
+    {
+        perror("Socket creation failed");
+        return 1;
+    }
+    memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_port = htons(SERVER_PORT);
+    if (inet_pton(AF_INET, SERVER_HOST, &server_addr.sin_addr) <= 0)
+    {
+        perror("Invalid address");
+        close(sockfd);
+        return 1;
+    }
+    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
+    {
+        perror("Connection failed");
+        close(sockfd);
+        return 1;
+    }
+    // Set timeouts for sending and receiving
+    send_timeout.tv_sec = SEND_TIMEOUT_SEC;
+    send_timeout.tv_usec = 0;
+    recv_timeout.tv_sec = RECV_TIMEOUT_SEC;
+    recv_timeout.tv_usec = 0;
+    // Set socket option to configure timeout for send and receive
+    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recv_timeout, sizeof(recv_timeout));
+    setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_timeout, sizeof(send_timeout));
+    // Send data with timeout
+    FD_ZERO(&write_fds);
+    FD_SET(sockfd, &write_fds);
+    timeout = send_timeout;
+    //result = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
+   // if (result < 0)
+   // {
+     //   perror("Select error");
+       // close(sockfd);
+        //return 1;
+    //}
+    //else if (result == 0)
+   // {
+     //   printf("Send operation timed out\n");
+      //  close(sockfd);
+       // return 1;
+    //}
+    //else if (FD_ISSET(sockfd, &write_fds))
+   // {
+        if (send(sockfd, send_buffer, strlen(send_buffer), 0) == -1)
+        {
+            perror("Send failed");
+            close(sockfd);
+            return 1;
+        }
+        printf("Message sent to server: %s\n", send_buffer);
+   // }
+    // Receive data with timeout
+    FD_ZERO(&read_fds);
+    FD_SET(sockfd, &read_fds);
+    timeout = recv_timeout;
+   // result = select(sockfd + 1, &read_fds, NULL, NULL, &timeout);
+   // if (result < 0)
+   // {
+     //     perror("Select error");
+       //   close(sockfd);
+        //  return 1;
+    // } else if (result == 0)
+     //{
+       //    printf("Receive operation timed out\n");
+        //   close(sockfd);
+         //  return 1;
+    // }
+   // else if (FD_ISSET(sockfd, &read_fds))
+    // {
+         int bytes_received = recv(sockfd, recv_buffer, sizeof(recv_buffer) - 1, 0);
+         if (bytes_received < 0)
+         {
+            perror("Receive failed");
+         }
+         else if (bytes_received == 0)
+         {
+             printf("Server closed the connection\n");
+         }
+         else
+         {
+            recv_buffer[bytes_received] = '\0';
+            printf("Received from server: %s\n", recv_buffer);
+         }
+   // }
+close(sockfd);
+return 0;
+}

+ 68 - 0
timeout_send_rec/server.c

@@ -0,0 +1,68 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 12345
+#define BUFFER_SIZE 1024
+
+int main() 
+{
+    int server_socket, client_socket;
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_addr_len;
+    char buffer[BUFFER_SIZE];
+    if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    // Define server address
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;
+    server_addr.sin_port = htons(PORT); 
+    // Bind the socket to the address and port
+    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 
+    {
+        perror("Bind failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    // Listen for incoming connections
+    if (listen(server_socket, 5) < 0)
+    {
+         perror("Listen failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Server listening on port %d...\n", PORT);
+    // Accept incoming client connection
+    client_addr_len = sizeof(client_addr);
+    if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) < 0)
+    {
+        perror("Client connection failed");
+        close(server_socket);
+        exit(EXIT_FAILURE);
+    }
+    printf("Client connected. Waiting for message...\n");
+    // Receive message from the client
+    //int n = recv(client_socket, buffer, BUFFER_SIZE, 0);
+    //if (n < 0)
+   // {
+     //   perror("Receive failed");
+       // close(client_socket);
+       // close(server_socket);
+       // exit(EXIT_FAILURE);
+     //}
+    //buffer[n] = '\0';  // Null-terminate the received string
+    //printf("Received from client: %s\n", buffer);
+    // Send response to the client
+    //const char *response = "Hello, Client! Your message was received.";
+    //send(client_socket, response, strlen(response), 0);
+   // printf("Response sent to client.\n");
+    // Close the sockets
+    close(client_socket);
+    close(server_socket);
+    return 0;
+    } 

+ 54 - 0
udp/client_udp.c

@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8080
+#define BUFFER_SIZE 1024
+
+void main()
+{
+    int sock_value;
+    char buffer[BUFFER_SIZE];
+    struct sockaddr_in server_addr={0};
+    socklen_t server_len = sizeof(server_addr);
+
+    //Create UDP socket
+    if ((sock_value = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
+    {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_port = htons(PORT);
+    // Convert IP address from text to binary form
+    if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0)
+    {
+        perror("Invalid address / Address not supported");
+        close(sock_value);
+        exit(EXIT_FAILURE);
+     }
+    printf("Enter a message to send to the server: ");
+    fgets(buffer, sizeof(buffer), stdin);
+
+    // Remove trailing newline character from fgets
+     buffer[strcspn(buffer, "\n")] = '\0';
+    //
+      // Send message to the server
+      sendto(sock_value, (const char *)buffer, strlen(buffer), MSG_CONFIRM, (const struct sockaddr *)&server_addr, server_len);
+      printf("Message sent to server: %s\n", buffer);
+    //receive
+    int n = recvfrom(sock_value, (char *)buffer, BUFFER_SIZE, MSG_WAITALL, (struct sockaddr *)&server_addr, &server_len);
+    buffer[n] = '\0';  // Null-terminate the received data
+     printf("Server responded with message length: %s\n", buffer);
+    //
+     //Close the socket
+    close(sock_value);
+
+}
+
+
+
+
+

+ 51 - 0
udp/server_udp.c

@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8080
+#define BUFFER_SIZE 1024
+
+void main()
+{
+    int socket_value;
+    char message[BUFFER_SIZE];
+    struct sockaddr_in server_addr={0};
+    struct sockaddr_in client_addr;
+    socklen_t client_len = sizeof(client_addr);
+    // socket creation
+    if((socket_value=socket(AF_INET,SOCK_DGRAM,0)) <0)
+    {
+        perror("server socket creation error");
+        exit(EXIT_FAILURE);
+    }
+    //server addr speciifications
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY;
+    server_addr.sin_port = htons(PORT);
+    // bind the socket to the port in server
+    if(bind(socket_value,(struct sockaddr*)&server_addr,sizeof(server_addr))<0)
+    {
+        perror("bind fail");
+        close(socket_value);
+        exit(EXIT_FAILURE);
+    }
+    printf("server is listening on port\n");
+    // receive message from client..
+    while(1)
+    {
+        int len=recvfrom(socket_value, (char *)message, BUFFER_SIZE, MSG_WAITALL, (struct sockaddr *)&client_addr, &client_len); 
+    message[len]='\0';
+    printf("%s/n",message);
+     // Send the length of the received message back to the client
+     int message_length = strlen(message);
+     snprintf(message, sizeof(message), "%d", message_length);  // Convert length to string
+     sendto(socket_value, (const char *)message, strlen(message), MSG_CONFIRM, (const struct sockaddr *)&client_addr, client_len);
+     printf("Sent message length to client: %d\n", message_length);
+    }
+    close(socket_value);
+
+
+}
+