FILE_SERVER.C File transfer Order Server sample
< br>//File_ SERVER.C File transfer Order Server sample
//////////////////////////////////////////////////////////////////////////////////////
This file is the code for the server
#include <netinet/in.h>//For sockaddr_in
#include <sys/types.h>/For socket
#include <sys/socket.h>/For socket
#include <stdio.h>/For printf
#include <stdlib.h>/For exit
#include <string.h>//For Bzero
/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
*/
#define Hello_world_server_port 6666
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
int main (int argc, char **argv)
{
Set a socket address structure server_addr, representing the server Internet address, port
struct sockaddr_in server_addr;
Bzero (&server_addr,sizeof (SERVER_ADDR)); Set the contents of a memory area to 0
server_addr.sin_family = af_inet;
SERVER_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any);
Server_addr.sin_port = htons (Hello_world_server_port);
Create a streaming Protocol (TCP) socket for the Internet, using Server_socket to represent the server socket
int server_socket = socket (pf_inet,sock_stream,0);
if (Server_socket < 0)
{
printf ("Create Socket failed!");
Exit (1);
}
Connect socket and socket address structure
if (Bind (Server_socket, (struct sockaddr*) &server_addr,sizeof (SERVER_ADDR))
{
printf ("Server Bind Port:%d failed!", hello_world_server_port);
Exit (1);
}
Server_socket for listening
if (Listen (Server_socket, Length_of_listen_queue))
{
printf ("Server Listen failed!");
Exit (1);
}
while (1)//server side to always run
{
Defines the socket address structure of the client client_addr
struct sockaddr_in client_addr;
socklen_t length = sizeof (CLIENT_ADDR);
Accept a connection to the socket represented by Server_socket
If there is no connection request, wait until there is a connection request--This is an attribute of the Accept function
The Accept function returns a new socket, the socket (New_server_socket) for communication with the connected client
New_server_socket represents a communication channel between the server and the client
The Accept function fills the client information connected to the client's socket address structure CLIENT_ADDR
int new_server_socket = Accept (Server_socket, (struct sockaddr*) &client_addr,&length);
if (New_server_socket < 0)
{
printf ("Server Accept failed!\n");
Break
}
Char Buffer[buffer_size];
Bzero (buffer, buffer_size);
Length = recv (new_server_socket,buffer,buffer_size,0);//Here first receive the file name that the client sent to get
if (length < 0)
{
printf ("Server recieve Data failed! \ n ");
Break
}
char file_name [file_name_max_size+1];
Bzero (file_name, file_name_max_size+1);
strncpy (file_name, buffer, strlen (buffer) >file_name_max_size? File_name_max_size:strlen (buffer));
int fp = open (file_name, o_rdonly);
if (FP < 0)
FILE * fp = fopen (file_name, "R");
if (NULL = fp)
{
printf ("file:\t%s not found\n", file_name);
}
Else
{
Bzero (buffer, buffer_size);
int file_block_length = 0;
while ((File_block_length = Read (fp,buffer,buffer_size)) >0)
while ((File_block_length = Fread (buffer,sizeof (char), BUFFER_SIZE,FP)) >0)
{
printf ("File_block_length =%d\n", file_block_length);
Send the string in buffer to New_server_socket, which is actually given to the client
if (send (new_server_socket,buffer,file_block_length,0) <0)
{
printf ("Send file:\t%s failed\n", file_name);
Break
}
Bzero (buffer, buffer_size);
//This code is a circular read of the file in a piece of data, in the loop call send, sent to the client, where the emphasis on a bit of TCP each receives up to is 1024 bytes, more will be fragmented, so try not to send more than 1024 bytes each time.
Close (FP);
Fclose (FP);
printf ("file:\t%s Transfer finished\n", file_name);
}
Close the connection to the client
Close (New_server_socket);
}
Turn off the listening socket
Close (Server_socket);
return 0;
}