⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.c

📁 Linux网络编程第一个实验,使用TCP协议实现了服务器和客户端
💻 C
字号:
/********Filename:client.c*****Author:Will*****E-Mail:hihicd@hotmail.com*****Date:2008-05-26*****Version:Beta 0.0*****Remark:HNU***/#include <stdio.h>#include <string.h> #include <stdlib.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#define PORTNUM         9000          // Port number//#define HOSTNAME        "127.0.0.1"   //Host IPint main(int argc,char *argv[]){   FILE *fp;  char *ch = NULL;  int n,size, ServerSock ;				// Socket bound to the server  char fileName[256];  char buffer[900*1024];				 //Send buffer  struct sockaddr_in destination_sin;        // Server socket address  struct hostent *phostent;           		// Points to the HOSTENT structure of the server  char HOSTNAME[18];      if (argc < 2)  {	fprintf (stderr, "Usage Error! \nUsage : ./client  FileName  down/up(download or upload)   HostIP   \nPlease Try Again.\n");	exit (1);  }    strcpy (HOSTNAME, argv[3]);       // Create a TCP/IP socket that is bound to the server.  if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) < 0)  {    fprintf (stderr, "Allocating socket failed.\n");    exit(1);  }  // Fill out the server socket's address information.  destination_sin.sin_family = AF_INET;  // Assign the socket IP address.  destination_sin.sin_addr.s_addr=inet_addr(HOSTNAME);  // Convert to network ordering.  destination_sin.sin_port = htons (PORTNUM);        // Establish a connection to the server socket.  if (connect (ServerSock,                (struct sockaddr *) &destination_sin,                sizeof (destination_sin)) <0)   {    fprintf (stderr,"Connecting to the server failed.\n");    close(ServerSock);    exit(1);  }	  ch = strrchr (argv[1], '/'); // ch point to the last '/'	  if ((ch != NULL) && (strlen(ch) > 0))//if the argument 1 contains the full path and the last char is not '/'	  {		ch++;		strcpy (fileName, ch); //copy the file name to  fileName	  }	  else //if the argument 1 is the file name	  {		strcpy (fileName, argv[1]); // get the file name	  } if(strcmp(argv[2],"up")==0)//upload file  {	  //try to open the file	   if( (fp  = fopen(argv[1], "r" )) == NULL )	  {		fprintf( stderr, "The file was not opened\n" );		exit (1);	  }	   printf ("Uploading file %s to %s ...\n", fileName, HOSTNAME);	  // sending request	  if (send (ServerSock, "up", 3 , 0) <0)	  {		  fprintf(stderr, "Sending request error \n");		  exit (1);	  }		printf("Sending request...\n");	   // sending filename	  if (send (ServerSock, fileName, strlen(fileName) + 1, 0) <0)	  {		  fprintf(stderr, "Sending file name error \n");		  exit (1);	  }	  	printf("Sending file name...\n");	  //sending once a char	  while((size = fread(buffer,sizeof(char),sizeof(buffer), fp))>0)	  {		printf(".");  		if (send (ServerSock, buffer, size, 0) < 0)  		{  		  fprintf (stderr,"Sending data to the server failed.\n");  		}	  }	 	 printf("Sending data...\n");	  // Disable sending on ServerSock.	  close(ServerSock);	  	  printf ("Uploading finished\n");  } else if(strcmp(argv[2],"down")==0)//download file  {	  fp = fopen (fileName, "w+"); //create file	  if (fp == NULL)	  {	    printf("Create File Error! \nfilename : %s \n",fileName);	    exit (1);	  }		// sending request	  if (send (ServerSock, "dn", 3 , 0) <0)	  {		  fprintf(stderr, "Sending request error \n");		  exit (1);	  }		printf("Sending request...\n"); 	// sending filename	  if (send (ServerSock, fileName, strlen(fileName) + 1, 0) <0)	  {		  fprintf(stderr, "Sending file name error \n");		  exit (1);	  }	  	printf("Sending file name...\n"); 	while ((n = read(ServerSock, buffer, 1024)) >= 0)	  {	    if (n > 0)	    { 	      fwrite (buffer, sizeof(char), n, fp);	    }	    else 	    {	       fclose (fp);	       break;	    }	}	  if (n < 0) 	  {	    fclose (fp);	    printf("Read data from network error!\n");	    exit (1);	  }	  	printf("Recieving data...\n");	  // Disable sending on ServerSock.	  close(ServerSock);	  	  printf ("Downloading finished\n"); }  exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -