TCP/IP socket file transmission in Linux

Source: Internet
Author: User
Tags htons
////////////////////////////////////
// Server code

///////////////////////////////////
// This file is the server code
# 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> //
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, which represents the Internet address and port of the server.
Struct sockaddr_in
Server_addr;
Bzero (& server_addr, sizeof (server_addr ));
// Set all content in a memory zone 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 TCP socket for the Internet, and use 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 );
}


// Link the socket and socket address Structures
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 for listening
If (
Listen (server_socket, length_of_listen_queue ))
{

Printf ("server listen failed! ");
Exit (1 );
}

While
(1) // The server must be running all the time
{

Struct sockaddr_in
Client_addr;
Socklen_t length = sizeof (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 );



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 );
// Receives information sent from the client to the 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' was
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' was not closed!
/N ");
Exit (1 );

// Close the connection to the client

Close (new_server_socket );
}
// Disable the socket used for listening

Close (server_socket );
Return
0;
}
////////////////////////////////////
// Client code

///////////////////////////////////

// This file is the client code
# 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> //
Exit
# Include <string. h> // For bzero
# Include
<Time. h> // For time_t and time
# Include
<ARPA/inet. h>

# Define hello_world_server_port 7754
# Define
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, which represents the Internet address and port of the client.
Struct sockaddr_in
Client_addr;
Bzero (& client_addr, sizeof (client_addr ));
// Set all content in a memory zone to 0
Client_addr.sin_family = af_inet;
// Internet protocol family
Client_addr.sin_addr.s_addr =
Htons (inaddr_any); // inaddr_any indicates that the local address is automatically obtained.
Client_addr.sin_port =
Htons (0); // 0 indicates that the system will automatically allocate an idle port.

// Create a stream protocol (TCP) socket for the Internet, and use client_socket to represent the client socket
Int client_socket
= Socket (af_inet, sock_stream, 0 );
If (client_socket <0)

{
Printf ("create socket failed! /N ");
Exit (1 );

}
// Link the client's socket with 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, which represents the server's Internet address and 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 program parameter

{
Printf ("Server IP address error! /N ");
Exit (1 );

}
Server_addr.sin_port = htons (hello_world_server_port );
Socklen_t
Server_addr_length = sizeof (server_addr );

// Initiate a connection to the server. After the connection is successful, 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
% S! /N ", argv [1]);
Exit (1 );
}

Char
Buffer [buffer_size];
Bzero (buffer, buffer_size );


// Receives data from the server to the 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 the buffer to the server

Send (client_socket, buffer, buffer_size, 0 );

If (Stream =
Fopen ("data", "W + T") = NULL)
{
Printf ("the file
'Data' was 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]);

// Close the file

Fclose (Stream );

// Close the socket
Close (client_socket );

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.