فهرست منبع

socket programming first commit

priyadharsan 1 ماه پیش
والد
کامیت
6b32c984dc
6فایلهای تغییر یافته به همراه397 افزوده شده و 0 حذف شده
  1. 64 0
      Filetransfer/client.c
  2. 75 0
      Filetransfer/server.c
  3. 63 0
      Non_Blocking_socket/client.c
  4. 90 0
      Non_Blocking_socket/server.c
  5. 44 0
      TCP_CLIENT_SERVER/client.c
  6. 61 0
      TCP_CLIENT_SERVER/server.c

+ 64 - 0
Filetransfer/client.c

@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define PORT 8081
+
+#define SERVER_IP "127.0.0.1"
+
+int main() {
+    int sockfd;
+    struct sockaddr_in server_address;
+
+    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    printf("enter into program\n");
+
+
+    server_address.sin_family = AF_INET;
+    server_address.sin_port = htons(PORT);
+    inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
+
+
+    if (connect(sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
+        perror("Connection failed");
+        close(sockfd);
+        exit(EXIT_FAILURE);
+    }
+
+    char s[]="data.txt";
+    send(sockfd,s,strlen(s),0);
+
+    char buffer[20];
+    int bytes_read;
+
+    FILE* fp = fopen("receiver.txt","w");
+
+        if(fp == NULL)
+            printf("file doesn't open\n");
+        else
+            printf("file open successfully\n");
+
+    while((bytes_read = recv(sockfd,buffer,sizeof(buffer),0)) > 0)
+    {
+        fwrite(buffer,1,bytes_read,fp);
+    }
+
+    if(bytes_read < 0)
+        perror("read failed\n");
+
+    close(sockfd);
+    fclose(fp);
+
+}
+
+
+
+
+

+ 75 - 0
Filetransfer/server.c

@@ -0,0 +1,75 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<unistd.h>
+#include<fcntl.h>
+#include<arpa/inet.h>
+
+#define PORT 8081
+void send_file(int fd,char* filename)
+{
+    int fp = open(filename,O_RDONLY);
+    printf("file:%s",filename);
+
+    if(fp < 0)
+    {
+        printf("file doesn't open\n");
+        return ;
+    }
+    
+    char buffer[1024];
+    int bytes_read;
+
+    while((bytes_read  = read(fp,buffer,sizeof(buffer)))>0)
+    {
+        if((send(fd,buffer,bytes_read,0))<0)
+        {
+            perror("send data failed\n");
+            break;
+        }
+    }
+    close(fp);
+    return;
+}
+int main()
+{
+    int sockfd,new_socket;
+    struct sockaddr_in address;
+
+    if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0)
+        perror("socket creation failed\n");
+
+
+    address.sin_family = AF_INET;
+    address.sin_port = htons(PORT);
+    address.sin_addr.s_addr = INADDR_ANY;
+
+     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");
+
+       int addrlen =sizeof(address);
+     while((new_socket = accept(sockfd, (struct sockaddr *)&address, (socklen_t*)&addrlen))) {
+
+         printf("connected to client\n");
+            
+         char s[20];
+         int read_data;
+         read_data = read(new_socket,s,sizeof(s)-1);
+         s[read_data]='\0';
+
+         printf("which file we want to read\n");
+
+         send_file(new_socket,s);
+
+         printf("file sent to client\n");
+         close(new_socket);
+        
+     }
+     close(sockfd);
+
+
+}

+ 63 - 0
Non_Blocking_socket/client.c

@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define PORT 8081
+
+#define SERVER_IP "127.0.0.1"
+
+int main() {
+    int sockfd;
+    struct sockaddr_in server_address;
+
+    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+        perror("Socket creation failed");
+        exit(EXIT_FAILURE);
+    }
+    printf("enter into program\n");
+
+    int flags = fcntl(sockfd, F_GETFL, 0);
+    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
+
+
+    server_address.sin_family = AF_INET;
+    server_address.sin_port = htons(PORT);
+    inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
+
+    int connect_status;
+label:
+    connect_status = connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address));
+
+
+    if (connect_status == 0) {
+        printf("conncetion succcessfully\n");
+         while(1)
+            {
+                char s[20];
+                printf("enter the data two send\n");
+
+                scanf("%s",s);
+                send(sockfd,s,strlen(s),0);
+            }
+
+    }
+    else
+    {
+
+    while (1) {
+
+        printf("still trying to connect\n");
+
+        sleep(1);
+
+        goto label;
+    }
+    }
+
+    close(sockfd);
+    return 0;
+}

+ 90 - 0
Non_Blocking_socket/server.c

@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include<pthread.h>
+
+#define PORT 8081
+#define BUFFER_SIZE 1024
+
+
+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);
+
+    if (read_size < 0) {
+        perror("Read error");
+    } else if (read_size == 0) {
+        printf("Client disconnected\n");
+    } else {
+        buffer[read_size] = '\0';
+        printf("Received data: %s\n", buffer);
+    }
+    }
+    close(sockfd);
+    return NULL;
+}
+
+
+int main() {
+    int server_fd, new_socket;
+    struct sockaddr_in address;
+    int opt = 1;
+    int addrlen = sizeof(address);
+    char buffer[BUFFER_SIZE] = {0};
+
+
+    pthread_t t1;
+
+    // Creating socket file descriptor
+    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))< 0) {
+        perror("Socket failed");
+        exit(EXIT_FAILURE);
+    }
+
+    // Setting up the address structure
+    address.sin_family = AF_INET;
+    address.sin_addr.s_addr = INADDR_ANY; // Accept connections from any IP
+    address.sin_port = htons(PORT);
+
+    // Binding the socket to the port
+    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
+        perror("Bind failed");
+   exit(EXIT_FAILURE);
+    }
+    if (listen(server_fd, 3) < 0) {
+        perror("Listen");
+        exit(EXIT_FAILURE);
+    }
+
+    printf("Server listening on port %d\n", PORT);
+
+    while(1)
+    {
+    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
+        perror("Accept");
+        exit(EXIT_FAILURE);
+    }
+
+        printf("server connected to client\n");
+
+        //pthread_t t1;
+
+        pthread_create(&t1,NULL,thread_fun,&new_socket);
+
+
+    }
+    pthread_detach(t1);
+
+
+}
+
+

+ 44 - 0
TCP_CLIENT_SERVER/client.c

@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8081
+#define BUFFER_SIZE 1024
+
+int main() {
+    int sock = 0;
+    struct sockaddr_in serv_addr;
+    char *message = "Hello Server\n";
+    char buffer[BUFFER_SIZE] = {0};
+
+    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+        printf("\n Socket creation error \n");
+        return -1;
+    }
+
+    serv_addr.sin_family = AF_INET;
+    serv_addr.sin_port = htons(PORT);
+    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
+        printf("\nInvalid address\n");
+        return -1;
+    }
+
+    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
+        printf("\nConnection Failed \n");
+        return -1;
+    }
+
+send(sock, message, strlen(message), 0);
+    printf("Message sent to server: %s", message);
+
+    int read_size = read(sock, buffer, BUFFER_SIZE);
+    if (read_size > 0) {
+        printf("Received from server: %s", buffer);
+    }
+
+    close(sock);
+    return 0;
+}
+

+ 61 - 0
TCP_CLIENT_SERVER/server.c

@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+
+#define PORT 8081
+#define BUFFER_SIZE 1024
+
+int main() {
+    int server_fd, new_socket;
+    struct sockaddr_in address;
+    int opt = 1;
+    int addrlen = sizeof(address);
+    char buffer[BUFFER_SIZE] = {0};
+
+    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
+        perror("Socket failed");
+        exit(EXIT_FAILURE);
+    }
+
+    address.sin_family = AF_INET;
+    address.sin_addr.s_addr = INADDR_ANY;
+    address.sin_port = htons(PORT);
+
+    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
+        perror("Bind failed");
+        exit(EXIT_FAILURE);
+        }
+
+    if (listen(server_fd, 3) < 0) {
+        perror("Listen");
+  exit(EXIT_FAILURE);
+    }
+
+    printf("Server listening on port %d\n", PORT);
+
+    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
+        perror("Accept");
+        exit(EXIT_FAILURE);
+    }
+
+    while (1) {
+        memset(buffer, 0, BUFFER_SIZE);
+        int read_size = read(new_socket, buffer, BUFFER_SIZE);
+       if (read_size == 0) {
+            printf("Client disconnected\n");
+            break;
+        }
+        printf("Received: %s", buffer);
+
+        send(new_socket, buffer, read_size, 0);
+        printf("Echoed back: %s", buffer);
+    }
+
+    close(new_socket);
+    close(server_fd);
+    return 0;
+}
+
+