C Code Examples for TCP, UDP, and FTP Sockets
TCP Client-Server C Code
TCP Server
This C code sets up a basic TCP server that listens on a specified port, accepts a client connection, receives a message, sends a reply, and then closes the connection, waiting for the next client.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
char *ip = "127.0.0.1";
int port = 5566;
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
int n;
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
perror("[-] Socket error");
exit(1);
}
printf("[+] TCP server socket created.\n");
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (n < 0) {
perror("[-] Bind error");
exit(1);
}
printf("[+] Bind to the port number: %d\n", port);
listen(server_sock, 5);
printf("Listening...\n");
while (1) {
addr_size = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);
printf("[+] Client connected.\n");
bzero(buffer, 1024);
recv(client_sock, buffer, sizeof(buffer), 0);
printf("Client: %s\n", buffer);
bzero(buffer, 1024);
strcpy(buffer, "Hi, this is the server. Have a nice day!");
printf("Server: %s\n", buffer);
send(client_sock, buffer, strlen(buffer), 0);
close(client_sock);
printf("[+] Client disconnected.\n\n");
}
return 0;
}TCP Client
This C code creates a TCP client that connects to the server, sends a message, receives a response, and then closes the connection.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
char *ip = "127.0.0.1";
int port = 5566;
int sock;
struct sockaddr_in addr;
char buffer[1024];
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("[-] Socket error");
exit(1);
}
printf("[+] TCP client socket created.\n");
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock, (struct sockaddr*)&addr, sizeof(addr));
printf("Connected to the server.\n");
bzero(buffer, 1024);
strcpy(buffer, "HELLO, THIS IS THE CLIENT");
printf("Client: %s\n", buffer);
send(sock, buffer, strlen(buffer), 0);
bzero(buffer, 1024);
recv(sock, buffer, sizeof(buffer), 0);
printf("Server: %s\n", buffer);
close(sock);
printf("Disconnected from the server.\n");
return 0;
}UDP Client-Server C Code
UDP Server
This C code demonstrates a simple connectionless UDP server that waits for a datagram from a client, receives it, and sends a response back.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
socklen_t addr_size;
int n;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("[-] Socket error");
exit(1);
}
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(ip);
n = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (n < 0) {
perror("[-] Bind error");
exit(1);
}
bzero(buffer, 1024);
addr_size = sizeof(client_addr);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, &addr_size);
printf("[+] Data received: %s\n", buffer);
bzero(buffer, 1024);
strcpy(buffer, "Welcome to the UDP server.");
sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
printf("[+] Data sent: %s\n", buffer);
return 0;
}UDP Client
This C code is for a UDP client that sends a single datagram to the server and waits to receive a response.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int sockfd;
struct sockaddr_in addr;
char buffer[1024];
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
bzero(buffer, 1024);
strcpy(buffer, "Hello, World!");
sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
printf("[+] Data sent: %s\n", buffer);
bzero(buffer, 1024);
addr_size = sizeof(addr);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
printf("[+] Data received: %s\n", buffer);
return 0;
}Simple FTP C Code for File Transfer
FTP Server
This C code implements a basic FTP-like server that accepts a connection and receives a file from a client, writing the contents to a local file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SIZE 1024
void write_file(int sockfd) {
int n;
FILE *fp;
char *filename = "received_file.txt";
char buffer[SIZE];
fp = fopen(filename, "w");
if (fp == NULL) {
perror("[-] Error in creating file.");
exit(1);
}
while (1) {
n = recv(sockfd, buffer, SIZE, 0);
if (n <= 0) {
break;
}
fprintf(fp, "%s", buffer);
bzero(buffer, SIZE);
}
fclose(fp);
return;
}
int main() {
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd, new_sock;
struct sockaddr_in server_addr, new_addr;
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("[-] Error in socket");
exit(1);
}
printf("[+] Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (e < 0) {
perror("[-] Error in bind");
exit(1);
}
printf("[+] Binding successful.\n");
if (listen(sockfd, 10) != 0) {
perror("[-] Error in listening");
exit(1);
}
printf("[+] Listening....\n");
addr_size = sizeof(new_addr);
new_sock = accept(sockfd, (struct sockaddr*)&new_addr, &addr_size);
write_file(new_sock);
printf("[+] Data written to the file successfully.\n");
return 0;
}FTP Client
This C code is for a simple FTP-like client that reads a local file and sends its contents to the server over a TCP connection.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024
void send_file(FILE *fp, int sockfd) {
char data[SIZE] = {0};
while (fgets(data, SIZE, fp) != NULL) {
if (send(sockfd, data, sizeof(data), 0) == -1) {
perror("[-] Error in sending file.");
exit(1);
}
bzero(data, SIZE);
}
}
int main() {
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd;
struct sockaddr_in server_addr;
FILE *fp;
char *filename = "source_file.txt";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("[-] Error in socket");
exit(1);
}
printf("[+] Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (e == -1) {
perror("[-] Error in connect");
exit(1);
}
printf("[+] Connected to Server.\n");
fp = fopen(filename, "r");
if (fp == NULL) {
perror("[-] Error in reading file.");
exit(1);
}
send_file(fp, sockfd);
printf("[+] File data sent successfully.\n");
printf("[+] Closing the connection.\n");
close(sockfd);
return 0;
}