Socket intermediate application on Linux platform - TCP file transfer

Socket intermediate application on Linux platform - TCP file transfer

1, Purpose and task of the experiment

  1. Review the IP configuration knowledge of ubuntu.
  2. This experiment requires to master the TCP communication process.
  3. Understand the basic method of TCP programming, the phenomenon of packet splicing and the realization of file transmission.
    *4. Deploy the program on the development board and transfer the file from the PC host to the development board.

2, Introduction to experimental equipment

1. Software requirements: win7 operating system, VMware workstation, Ubuntu 12 [configure cross compilation environment].
2. Hardware requirements: PC memory is greater than 1G and hard disk space is greater than 20G; smart210 development board.

3, Precautions and requirements

1. Before starting the virtual machine, you should first use the "virtual network editor" to configure the network card to be bridged.
2. Check whether the network cable is connected normally.
3. In the TCP communication process, the data buffer of send and recv methods do not correspond.
4. Please take photos with your mobile phone and put the operation results of the development board into the experimental report.

4, Experiment contents and steps

4.1 laboratory network configuration
The B703 host is equipped with two network cards, which are called A network card and B network card in this paper. Each stand is also equipped with two network sockets, in which the A network card is directly connected to the network socket. At ordinary times, the A network card of all hosts in the whole computer room is connected to the switch, which together with the teacher computer forms an interconnected LAN. The network segment of this LAN is 192.168.200 20. In this way, teachers can broadcast their lessons through the central control software.
The B network card is connected with a network cable, which is placed on the table. The network cable is used to connect the development board. When the development board is connected with the B network card of the PC host with a network cable, the PC and the development board form a tiny LAN. The corresponding network IP address needs to be set for the actual communication of the network.

4.2 configure the network IP address directly connected between the development board and the PC by network cable

First, check the configuration information of win7 host network card. If the IP is configured as 192.168.200.5, this network card is connected to the switch. We ignore it and keep it. Then another network card is connected to the development board.
Configure the IP address of another network card to 192.168.1.5
Check the names of the two network cards and find out the address information of each network card. Obviously, the virtual machine needs to use the 192.168.1.5 network card to connect with the development board.
Next, it's the virtual machine's turn to play, but you can't open the virtual machine at this time. You need to configure the network card of the virtual machine first. Select VMnet0 as the network card, which specifically implements the bridge function. The bridge is the 192.168.1.5 network card. The bridge is wrong. ubuntu cannot be connected with the development board.

Edit the hardware information of the virtual machine to ensure that it is in bridge mode.
After starting the virtual machine, start the network connection of ubuntu, and then configure the IP address.
After saving the configuration information, you see this prompt.

After the above operations, ubuntu is finally connected with the virtual machine, which means that the virtual machine has the network function. The development board and the PC network cable are connected to form a very small LAN, so the PC host and the development board are configured as 192.168.1 XX network segment is enough. In this case, the B network card information of win7 host needs to be configured, and the IP address of the development board needs to be configured through the serial port. Because the default IP configuration of the development board is 192.168.1.230 after startup, there is no need to configure, and the two computers can be connected.

In the case of network connectivity, the development board can use a variety of network protocols to realize file transmission with ubuntu, such as TFTP, HTTP, FTP and NFS.

4.3 compile the file sender program of PC platform

The program that sends the file runs in pc End, in ubuntu12 Used in gcc Direct compilation. The program performs listening. When running, it should be started before the receiving program of the development board.
//TCP/IP network programming page121 fsend c   030. C file transmission sender
//This program performs socket listening and file sending operations
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc,char *argv[])
{
	int serv_sd,clnt_sd;
	FILE * fp;
	char buf[BUF_SIZE];
	int read_cnt;
	struct sockaddr_in serv_adr,clnt_adr;
	socklen_t clnt_adr_sz;
	
	if(argc!=2)
	{
		printf("Usage: %s <port>\n",argv[0]);
		exit(1); 
	}
	//Read in file
	fp=fopen("file_send.c","rb");
	//socket resources for listening
	serv_sd=socket(PF_INET,SOCK_STREAM,0);
	memset(&serv_adr,0,sizeof(serv_adr));
	serv_adr.sin_family=AF_INET;
	serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
	serv_adr.sin_port=htons(atoi(argv[1]));
	//Binding listening port
	bind(serv_sd,(struct sockaddr*)&serv_adr,sizeof(serv_adr));
	//Execute listening
	listen(serv_sd,5);
	printf("listen... \n");
	clnt_adr_sz=sizeof(clnt_adr);
	//Connection establishment
	clnt_sd=accept(serv_sd,(struct sockaddr*)&clnt_adr,&clnt_adr_sz);
	printf("Client connect success... \n");
	//Read data from file and write it to network
	while(1)
	{
		//Read in disk file data
		read_cnt=fread((void*)buf,1,BUF_SIZE,fp);
		printf("from file read data : %d\n",read_cnt); 
		if(read_cnt<BUF_SIZE)
		{//The length of the read data is less than the length of the buffer, indicating that the file has been read to the end
			//Write the buffer data into the socket file to realize data transmission
			write(clnt_sd,buf,read_cnt);
			break; 
		}
		//Send data when the buffer is full
		write(clnt_sd,buf,BUF_SIZE); 
	}
	//Disconnect the network write direction connection,
	shutdown(clnt_sd,SHUT_WR);
	//Read in the last information of the network
	read(clnt_sd,buf,BUF_SIZE);
	printf("Message from client: %s",buf);
	//Disconnect the network read in direction,
	shutdown(clnt_sd,SHUT_RD);
	//Close the file where the data is saved
	fclose(fp);
	//Close data transfer socket file resources
	close(clnt_sd);
	//Turn off listening socket file resources
	close(serv_sd);
	return 0;
}
//End the process after outputting the error message
void error_handling(char * message)
{
	fputs(message,stderr);
	fputc('\n',stderr);
	exit(1); 
}use gcc –o fsend fsend.c Compiler.

4.4 cross compile the file receiving program and run it on the arm development board

The receiving program will run on the development board of smart210, and arm linux GCC is used in Ubuntu 12 to realize the cross compilation of the receiving program.

//TCP/IP network programming page123 frecv c   031. C file transmission and receiving end
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc,char *argv[])
{
	int sd;
	FILE * fp;
	char buf[BUF_SIZE];
	int read_cnt;
	struct sockaddr_in serv_adr; 
	
	if(argc!=3)
	{
		printf("Usage: %s <IP> <port>\n",argv[0]);
		exit(1); 
	}

	fp=fopen("receive.dat","wb");
	sd=socket(PF_INET,SOCK_STREAM,0);
	//Target socket resource to connect 
	memset(&serv_adr,0,sizeof(serv_adr));
	serv_adr.sin_family=AF_INET;
	//serv_adr.sin_addr.s_addr=htonl(argv[1]);
	inet_pton(AF_INET,argv[1],&serv_adr.sin_addr);
	//serv_adr.sin_addr.s_addr=htonl(argv[1]);
	serv_adr.sin_port=htons(atoi(argv[2]));
	//Connect to target host
	connect(sd,(struct sockaddr*)&serv_adr,sizeof(serv_adr));
	//Read data from socket resource
	while((read_cnt=read(sd,buf,BUF_SIZE))!=0)
		//Write data to file
		fwrite((void *)buf,1,read_cnt,fp);
	puts("Received file data.");
	write(sd,"Thank you",10); 
	//Close disk file
	fclose(fp);
	//Close socket resource
	close(sd);
	return 0; 
}

4.5 file transfer program supporting file name

In the above program, the receiving end uses a fixed file name to receive file data and modify the program[Sender and receiver]Make the receiving end save with the original name. Send the file name string to the receiver before sending the file 
//032.c file transmission receiver supports file name reception
main(int argc,char *argv[])
{
	int sockfd,sendbytes;
	char buf[MAXDATASIZE];
	char file_name[FILE_NAME_LEN];
	struct hostent *host;
	struct sockaddr_in serv_addr;
	int fd = 0;
	int totalbytes = 0;
	int readbytes = 0;
	if(argc < 2){
		fprintf(stderr,"Please enter the server's hostname!\n");
		exit(1);
	}
	if((host=gethostbyname(argv[1]))==NULL){
		perror("gethostbyname");
		exit(1);
	}
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
		perror("socket");
		exit(1);
	}
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(SERVPORT);
	serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
	bzero(&(serv_addr.sin_zero),8);
	if(connect(sockfd,(struct sockaddr *)&serv_addr,\
		sizeof(struct sockaddr))==-1){
		perror("connect");
		exit(1);
	}
        printf("please input the file you want to open:\n");
//Empty file name string buffer
memset(file_name,0,strlen(file_name));
//Enter file name characters
        scanf("%s",file_name);
        printf("filename is %s\n",file_name);
        fd=open(file_name, O_RDONLY, 0666);
        if(fd < 0){
        	perror("open file error.");
        	exit(1);
        }	
        //Send the file name string to the receiver
send(sockfd, file_name, strlen(file_name),0));
        printf("press s to send file\n");
        getchar();
        while((readbytes = read(fd, buf,MAXDATASIZE)) )
        {
            if((sendbytes=send(sockfd, buf, readbytes,0))==-1){
            	perror("send");
            	exit(1);
            }
//Accumulate the number of bytes that have been sent
            totalbytes += sendbytes;
        }
        printf("sent %d bytes ..\n", totalbytes);
//Add code here --- close socket resource
        close(sockfd);
}

Tags: Linux network socket switch LAN

Posted by mightyworld.com on Sat, 07 May 2022 03:29:58 +0300