TCP Client ( can be send as files ...)

 TCP-Client

// client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

#define SERVERIP "127.0.0.1"
#define SERVERPORT 9000
#define BUFSIZE 512

void err_quit(const char *msg) {
    fprintf(stderr, "%s\n", msg);
    exit(1);
}
void err_display(const char *msg) {
    fprintf(stderr, "%s\n", msg);
}

int send_file(SOCKET sock, const char *filename) {
    FILE *file = fopen(filename, "rb");
    if (!file) {
        printf("Error: Cannot open file %s\n", filename);
        return -1;
    }

    //  Send file name (null-terminated string)
    send(sock, filename, (int)strlen(filename) + 1, 0);

    // Get file size
    fseek(file, 0, SEEK_END);
    long filesize = ftell(file);
    fseek(file, 0, SEEK_SET);

    //  Send file size
    send(sock, (char*)&filesize, sizeof(filesize), 0);
    printf("Sending file: %s (%ld bytes)\n", filename, filesize);

    // Send file data in chunks
    char buf[BUFSIZE];
    size_t bytes_read;
    while ((bytes_read = fread(buf, 1, BUFSIZE, file)) > 0) {
        send(sock, buf, (int)bytes_read, 0);
    }

    printf("File sent successfully!\n");
    fclose(file);
    return 0;
}

int main(int argc, char *argv[]) {
    int retval;
    char client_name[50] = "TEE";

    // Initialize Winsock
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
        return 1;

    // Create socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) err_quit("socket()");

    // Connect to server
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    inet_pton(AF_INET, SERVERIP, &serveraddr.sin_addr);
    serveraddr.sin_port = htons(SERVERPORT);
    retval = connect(sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    if (retval == SOCKET_ERROR) err_quit("connect()");

    // Send client name to the server
    send(sock, client_name, (int)strlen(client_name), 0);
    printf("I'M: %s\n", client_name);

    // Communication loop
    char buf[BUFSIZE + 1];
    int len;
    while (1) {
        // Get message input
        printf("\n[Send Message] ");
        if (fgets(buf, BUFSIZE + 1, stdin) == NULL)
            break;

        // Remove '\n'
        len = (int)strlen(buf);
        if (buf[len - 1] == '\n')
            buf[len - 1] = '\0';
        if (strlen(buf) == 0)
            break;

        // If the input is a file command, send "sendfile" first, then send the file
        if (strncmp(buf, "sendfile", 8) == 0) {
            // Extract file name from the input
            char filename[256];
            sscanf(buf + 9, "%255s", filename);  // Take filename from the input

            // Send the command separately
            send(sock, "sendfile", (int)strlen("sendfile"), 0);

            //  Then send the actual file
            send_file(sock, filename);
        } else {
            // Send message
            retval = send(sock, buf, (int)strlen(buf), 0);
            if (retval == SOCKET_ERROR) {
                err_display("send()");
                break;
            }
        }
    }

    // Close socket
    closesocket(sock);
    WSACleanup();
    return 0;
}

Post a Comment

0 Comments