This article describes the C language implementation of Linux socket file transfer. Share to everyone for your reference. Specifically as follows:
SERVER.C is as follows:
Server code/////////////////////////////////////This file is the code for the server #include <netinet/ In.h>//For sockaddr_in #include <sys/types.h>//For sockets #include <sys/socket.h>/for socket #inc Lude <stdio.h>//For printf #include <stdlib.h>//For exit #include <string.h>//For bzero #
Include <time.h>//for time_t and time #define Hello_world_server_port 7754 #define LENGTH_OF_LISTEN_QUEUE 20 #define BUFFER_SIZE 1024 int main (int argc, char **argv) {//Set a socket address structure server_addr, representing the server Internet address, port struct SOCKADD
R_in server_addr; Bzero (&server_addr,sizeof (SERVER_ADDR));
Setting the contents of a section of 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);
time_t now;
FILE *stream;
Create a streaming Protocol (TCP) socket for the Internet, using Server_socket to represent the server socket int server_socket = socket (af_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 is used to listen for if (Listen (Server_socket, length_of_listen_queue)) {printf ("Server Listen failed!"); exit (1);} W Hile (1)//server end to run {struct sockaddr_in client_addr; socklen_t length = sizeof (CLIENT_ADDR); int new_server_socket = ACC
EPT (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); strcpy (buffer, "hello,world! from the server!")
"); strcat (buffer, "\ n");
C Language String connection send (new_server_socket,buffer,buffer_size,0);
Bzero (buffer,buffer_size);
Receive the information sent by the client to buffer length = recv (new_server_socket,buffer,buffer_size,0);
if (length < 0) {printf ("Server recieve Data failed!\n"), exit (1);} printf ("\n%s", buffer); if (stream = fopen ("Data1", "R==null) {printf ("The file ' data1 ' is not opened!
\ n ");
Exit (1); else printf ("The file ' filename ' was opened!
\ n ");
Bzero (buffer,buffer_size);
int lengsize = 0; while ((Lengsize = Fread (buffer,1,1024,stream)) > 0) {printf ("lengsize =%d\n", lengsize); if (send New_server_socket,
buffer,lengsize,0) <0) {printf (' Send File is failed\n '); break;} bzero (buffer, buffer_size); if (fclose (stream)) printf ("The file ' data ' is not closed!
\ n ");
Exit (1);
Closes the connection to the client close (New_server_socket);
//Turn off the listening socket Close (server_socket);
return 0;
}
The
Client.c is as follows:
Client code/////////////////////////////////////This file is the client's code #include <netinet/ In.h>//For sockaddr_in #include <sys/types.h>//For sockets #include <sys/socket.h>/for socket #inc Lude <stdio.h>//For printf #include <stdlib.h>//For exit #include <string.h>//For bzero # Include <time.h>//for time_t and time #include <arpa/inet.h> #define Hello_world_server_port 7754 #de
Fine buffer_size 1024 int main (int argc, char **argv) {if (argc!= 2) {printf ("Usage:./%s serveripaddress\n", argv[0]);
Exit (1);
}//time_t now;
FILE *stream;
Set a socket address structure client_addr, on behalf of the client Internet address, port struct sockaddr_in client_addr; Bzero (&client_addr,sizeof (CLIENT_ADDR)); Setting the contents of a section of memory area to 0 client_addr.sin_family = af_inet; Internet Protocol Family client_addr.sin_addr.s_addr = htons (inaddr_any);//inaddr_any indicates automatic acquisition of native address Client_addr.sin_port = htons (0) ; 0 means that the system automatically assigns an idle port//To create a streaming protocol (TCP) socket for the Internet, using the CLIENT_SOCKET represents the client socket int client_socket = socket (af_inet,sock_stream,0); if (Client_socket < 0) {printf ("Create socket failed!\n"); exit (1);}//Connect the client's socket to the client's socket address structure if bind (client _socket, (struct sockaddr*) &client_addr,sizeof (client_addr)) {printf ("Client Bind Port failed!\n"); exit (1);}//
Set a socket address structure server_addr, representing the server's Internet address, port struct sockaddr_in server_addr;
Bzero (&server_addr,sizeof (SERVER_ADDR));
server_addr.sin_family = af_inet; if (Inet_aton (argv[1],&server_addr.sin_addr) = = 0)//The IP address of the server comes from the parameter of the program {printf ("Server IP addressing error!\n"); exit (1)
;
} Server_addr.sin_port = Htons (Hello_world_server_port);
socklen_t server_addr_length = sizeof (SERVER_ADDR); Initiating a connection to the server, Client_socket represents a socket connection between the client and the server if (connect (client_socket, struct sockaddr*) &server_addr,
Server_addr_length) < 0) {printf ("Can not Connect to%s!\n", argv[1]); exit (1);} char buffer[buffer_size];
Bzero (buffer,buffer_size); Receive data from server to buffer int length = recv (Client_socket,buffer,buffer_size,0);
if (length < 0) {printf ("recieve Data from Server%s failed!\n", argv[1]); exit (1);} printf ("\n%s\n", buffer);
Bzero (buffer,buffer_size);
Bzero (buffer,buffer_size); strcpy (Buffer, "Hello, world!
From client\n ");
Send data in buffer to the server send (client_socket,buffer,buffer_size,0); if (stream = fopen ("Data", "W+t")) ==null) {printf ("The file ' data ' is not opened!
\ n ");
else Bzero (buffer,buffer_size);
length = 0; while (length = recv (client_socket,buffer,buffer_size,0)) {if (length < 0) {printf ("recieve Data from Server%s Failed
!\n ", argv[1]);
Break
int write_length = fwrite (buffer,sizeof (char), length,stream);
if (write_length<length) {printf (' File is write failed\n '); break;} bzero (Buffer,buffer_size);
printf ("Recieve File from server[%s] finished\n", argv[1]);
Closes the file fclose (stream);
Closes the socket close (client_socket);
return 0;
}
I hope this article will help you with your C language program.