You are on page 1of 12

/*

* client.c
*
* Simple FTP Client
* ====================
* Connects the server, and sends a command "Get FILENAME" to retrieve
* the file. If the file exists on the server, client retrieves it.
*
* Compile & link : gcc client.c -o client
* Execute : ./client
*
* Ozcan Ovunc <ozcan_ovunc@hotmail.com>
*/

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "a.txt"


#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 65496

int main(int argc , char **argv)


{
int socket_desc;
struct sockaddr_in server;
char request_msg[BUFSIZ],
reply_msg[BUFSIZ];

// Variables for the file being received


int file_size,
file_desc;
char *data;

socket_desc = socket(AF_INET, SOCK_STREAM, 0);


if (socket_desc == -1)
{
perror("Could not create socket");
return 1;
}

server.sin_addr.s_addr = inet_addr(SERVER_IP);
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);

// Connect to server
if (connect(socket_desc, (struct sockaddr *)&server,
sizeof(server)) < 0)
{
perror("Connection failed");
return 1;
}

// Get a file from server


strcpy(request_msg, "Get ");
strcat(request_msg, FILENAME);
write(socket_desc, request_msg, strlen(request_msg));
recv(socket_desc, reply_msg, 2, 0);

// Start receiving file


if (strcmp(reply_msg, "OK") == 0) {

recv(socket_desc, &file_size, sizeof(int), 0);


data = malloc(file_size);
file_desc = open(FILENAME, O_CREAT | O_EXCL | O_WRONLY,
0666);
recv(socket_desc, data, file_size, 0);
write(file_desc, data, file_size);
close(file_desc);
}
else {

fprintf(stderr, "Bad request\n");


}

return 0;
}
/*
* server.c
*
* Simple FTP Server
* ====================
* If the client connects to the server and wants to retrieve a file
* with command "Get FILENAME", server retrieves iff the file exists
* on the server o/w notifies the client with an error message.
*
* Compile & link : gcc server.c -lpthread -o server
* Execute : ./server
*
* Ozcan Ovunc <ozcan_ovunc@hotmail.com>
*/

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>

#define SERVER_PORT 8080

void* ConnectionHandler(void* socket_desc);


char* GetFilenameFromRequest(char* request);
bool SendFileOverSocket(int socket_desc, char* file_name);

int main(int argc, char **argv)


{
int socket_desc,
socket_client,
*new_sock,
c = sizeof(struct sockaddr_in);

struct sockaddr_i server,


client;

// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
perror("Could not create socket");
return 1;
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(SERVER_PORT);

if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) <


0)
{
perror("Bind failed");
return 1;
}

listen(socket_desc , 3);

while (socket_client = accept(socket_desc, (struct sockaddr


*)&client, (socklen_t*)&c))
{
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = socket_client;
pthread_create(&sniffer_thread, NULL, ConnectionHandler,
(void*) new_sock);
pthread_join(sniffer_thread, NULL);
}

if (socket_client<0)
{
perror("Accept failed");
return 1;
}

return 0;
}

void *ConnectionHandler(void *socket_desc)


{
int socket = *(int*)socket_desc;
char server_response[BUFSIZ],
client_request[BUFSIZ],
file_name[BUFSIZ];

recv(socket, client_request, BUFSIZ, 0);


strcpy(file_name, GetFilenameFromRequest(client_request));

// If requested file exists, notify the client and send it


if (access(file_name, F_OK) != -1){

strcpy(server_response, "OK");
write(socket, server_response, strlen(server_response));
SendFileOverSocket(socket, file_name);
}
else {
// Requested file does not exist, notify the client
strcpy(server_response, "NO");
write(socket, server_response, strlen(server_response));
}

free(socket_desc);
return 0;
}

char* GetFilenameFromRequest(char* request){

char *file_name = strchr(request, ' ');


return file_name + 1;
}

bool SendFileOverSocket(int socket_desc, char* file_name){

struct stat obj;


int file_desc,
file_size;

stat(file_name, &obj);
file_desc = open(file_name, O_RDONLY);
file_size = obj.st_size;
send(socket_desc, &file_size, sizeof(int), 0);
sendfile(socket_desc, file_desc, NULL, file_size);

return true;
}

#include <stdio.h>

#include <netdb.h>

#include <netinet/in.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <sys/types.h>
#define MAX 80

#define PORT 8080

#define SA struct sockaddr

// Function designed for chat between client and server.

void func(int sockfd)

char buff[MAX];

int n;

// infinite loop for chat

for (;;) {

bzero(buff, MAX);

// read the message from client and copy it in buffer

read(sockfd, buff, sizeof(buff));

// print buffer which contains the client contents

printf("From client: %s\t To client : ", buff);

bzero(buff, MAX);

n = 0;

// copy server message in the buffer

while ((buff[n++] = getchar()) != '\n')

// and send that buffer to client

write(sockfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.


if (strncmp("exit", buff, 4) == 0) {

printf("Server Exit...\n");

break;

// Driver function

int main()

int sockfd, connfd, len;

struct sockaddr_in servaddr, cli;

// socket create and verification

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == -1) {

printf("socket creation failed...\n");

exit(0);

else

printf("Socket successfully created..\n");

bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification

if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {

printf("socket bind failed...\n");

exit(0);

else

printf("Socket successfully binded..\n");

// Now server is ready to listen and verification

if ((listen(sockfd, 5)) != 0) {

printf("Listen failed...\n");

exit(0);

else

printf("Server listening..\n");

len = sizeof(cli);

// Accept the data packet from client and verification

connfd = accept(sockfd, (SA*)&cli, &len);

if (connfd < 0) {

printf("server acccept failed...\n");

exit(0);

else

printf("server acccept the client...\n");


// Function for chatting between client and server

func(connfd);

// After chatting close the socket

close(sockfd);

#include <netdb.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#define MAX 80

#define PORT 8080

#define SA struct sockaddr

void func(int sockfd)

char buff[MAX];

int n;

for (;;) {

bzero(buff, sizeof(buff));

printf("Enter the string : ");

n = 0;

while ((buff[n++] = getchar()) != '\n')


;

write(sockfd, buff, sizeof(buff));

bzero(buff, sizeof(buff));

read(sockfd, buff, sizeof(buff));

printf("From Server : %s", buff);

if ((strncmp(buff, "exit", 4)) == 0) {

printf("Client Exit...\n");

break;

int main()

int sockfd, connfd;

struct sockaddr_in servaddr, cli;

// socket create and varification

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == -1) {

printf("socket creation failed...\n");

exit(0);

else

printf("Socket successfully created..\n");

bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

servaddr.sin_port = htons(PORT);

// connect the client socket to server socket

if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {

printf("connection with the server failed...\n");

exit(0);

else

printf("connected to the server..\n");

// function for chat

func(sockfd);

// close the socket

close(sockfd);

}
//ping

You might also like