priyadharsan 1 місяць тому
батько
коміт
6035ab4801

+ 38 - 0
Concurrent_Server_Multithreading/client.c

@@ -0,0 +1,38 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+#include<unistd.h>
+#include<arpa/inet.h>
+
+#define port 8081
+
+int main()
+{
+    int sockfd;
+    struct sockaddr_in client;
+
+    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
+        perror("socket failed\n");
+
+    client.sin_family = AF_INET;
+    client.sin_addr.s_addr = inet_addr("127.0.0.1");
+    client.sin_port = htons(port);
+
+    if(connect(sockfd,(struct sockaddr*)&client,sizeof(client))<0)
+        perror("connection failed\n");
+    else
+        printf("connected successfully\n");
+
+    while(1)
+    {
+    char s[20];
+
+    printf("enter the data to send\n");
+    scanf("%s",s);
+
+    send(sockfd,(char*)s,strlen(s),0);
+    }
+
+}
+
+

+ 84 - 0
Concurrent_Server_Multithreading/server.c

@@ -0,0 +1,84 @@
+#include<stdio.h>
+#include<arpa/inet.h>
+#include<stdlib.h>
+#include<unistd.h>
+#include<string.h>
+#include<pthread.h>
+#define port 8081
+
+pthread_t t1;
+int i=0;
+
+void* thread_fun(void* n) {
+    char buffer[100];
+    int read_size = 0;
+    int sockfd = *(int*)n;  
+
+    printf("Inside thread function\n");
+
+    while(1)
+    {
+    read_size = read(sockfd, buffer, sizeof(buffer) - 1); // Leave space for null terminator
+
+    if (read_size < 0) {
+        perror("Read error");
+    } else if (read_size == 0) {
+        printf("Client disconnected\n");
+        return NULL;
+    } else {
+        buffer[read_size] = '\0';
+        printf("Received data: %s\n", buffer);
+    }
+    }
+
+    close(sockfd);
+
+    return NULL;
+}
+
+
+int main()
+{
+    int sockfd,*newfd;
+    struct sockaddr_in address;
+    int addrlen = sizeof(address);
+
+    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
+        perror("socket failed\n");
+    
+    address.sin_family = AF_INET;
+    address.sin_addr.s_addr = INADDR_ANY;
+    address.sin_port = htons(port);
+
+    if((bind(sockfd,(struct sockaddr*)&address,sizeof(address)))<0)
+            perror("bind failed\n");
+
+       if((listen(sockfd,3))<0)
+            perror("server listening failed\n");
+       else
+           printf("server listening\n");
+       
+       while(1)
+       {
+           newfd = malloc(sizeof(int));
+
+           *newfd=accept(sockfd,(struct sockaddr*)&address,(socklen_t*)&addrlen);
+
+           if(*newfd<0)
+           {
+              perror("accept failed\n");
+           }
+           else
+               printf("client connected\n");
+
+          
+           
+           if(pthread_create(&t1,NULL,thread_fun,newfd)==0)
+               printf("new thread created\n"); 
+       }
+
+       pthread_detach(t1);
+    
+
+}
+

+ 39 - 0
UDP/client1.c

@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8080
+#define BUFFER_SIZE 1024
+
+int main() {
+    int sockfd;
+    char buffer[BUFFER_SIZE];
+    struct sockaddr_in server_addr;
+
+    
+    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+
+      memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_port = htons(PORT);
+    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Change to server IP if needed
+
+   
+    printf("Enter message to send to server: ");
+    scanf("%[^\n]", buffer); 
+
+    sendto(sockfd, buffer, strlen(buffer), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr));
+
+    char buf[20];
+    recvfrom(sockfd,buf, sizeof(buf), 0, NULL, NULL);
+    printf("Received message length from server: %s\n",buf); 
+
+    close(sockfd);
+    return 0;
+}
+

+ 50 - 0
UDP/server1.c

@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8080
+
+int main() {
+    int sockfd;
+    char buffer[50];
+    char sbuf[50];
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_len;
+
+    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+
+    memset(&server_addr, 0, sizeof(server_addr));
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address
+    server_addr.sin_port = htons(PORT);
+
+    if (bind(sockfd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
+        perror("Bind failed");
+        close(sockfd);
+        exit(EXIT_FAILURE);
+    }
+
+    printf("Server listening on port %d\n", PORT);
+
+    while (1) {
+
+        client_len = sizeof(client_addr);
+        int n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_len);
+        buffer[n] = '\0'; // Null-terminate the received string
+
+        printf("Received message: %s\n", buffer);
+
+        int length = n;
+        
+        sprintf(sbuf,"%d",n);
+        sendto(sockfd, sbuf, sizeof(sbuf), 0, (const struct sockaddr *)&client_addr, client_len);
+    }
+    close(sockfd);
+    return 0;
+}
+