Linux network programming practice one (familiar with linux programming)

foreword

        Practice 1 is just familiar with programming in the linux environment, and has not started to engage in server and client.

        Then I use the ubuntu virtual machine for practice. Of course, it is not necessary to use a virtual machine. You can also download VScode and configure a linux environment. Many students are like this.

Topic 1:

        1. Write the Hello World program in the Linux environment. It is required to give the source code, compilation command, and running result.

source code:

#include<stdio.h>
int main(int argc,char *argv[]){
	printf("Hello World!\n");
	return 0;
}

Compile command:

You need to install vim and gcc on ubuntu first

sudo apt install vim

sudo apt install gcc

First open the terminal on the ubuntu desktop

Enter ls to view the files in the current directory

Then use the vim command to create the C language file hello.c: vim hello.c

Compile with the gcc command: gcc hello.c

Execute the file after finding that the out suffix file has been generated: ./a.out

(./ refers to the current directory)

operation result:

              

Topic 2:

        Create, read, write, and close files by programming under Linux. It is required to give the source code, compilation command, and running result.

source code:

#include<stdio.h>
int main(int argc,char *argv[]){
	FILE *fp1=fopen("test.txt","w+");//file creat and write
	if(fp1==NULL){
		printf("file open error!\n");
		return 0;
	} else printf("creat and read file success!\n");
	fprintf(fp1,"this is a test file!\n");//input the data
	fclose(fp1);//close the file
	//and now begin file's read
	FILE *fp2=fopen("test.txt","r");
	if(fp2==NULL){
		printf("file open error!\n");
		return 0;
	}
	char test[10]={0};
	while(~fscanf(fp2,"%s",test)){
		printf("%s ",test);
	}
	printf("\n");
	fclose(fp2);
	return 0;
}

Compile command:

Create a C language file through vim instructions: vim file.c

To compile: gcc file.c

Run the program: ./a.out

operation result:

(A newline statement is added in the middle)

epilogue

        This article is just some basics, and the server and client will be used later in Practice 2. Information interaction between the server and the client, as well as the upload and download of text files will be performed. I'll post it later when I have time.

Guess you like

Origin blog.csdn.net/xiexieyuchen/article/details/129758789