cli_rcv_send.c

#include <sys/socket.h> /*connect,send,recv,setsockopt等 */
#include <sys/types.h> 
#include <sys/stat.h> 
#include <netinet/in.h> /* sockaddr_in, "man 7 ip" ,htons*/
#include <poll.h> /*poll,pollfd*/
#include <arpa/inet.h> /*inet_addr,inet_aton*/
#include <unistd.h> /*read,write*/
#include <netdb.h> /*gethostbyname*/
#include<stdlib.h>
#include <error.h> /*perror*/
#include <stdio.h>
#include <errno.h> /*errno*/
#include <string.h> /* memset */
#include <string.h>

#define MAXSIZE 1024
#define PORTNUMBER 28000
#define MAX_LINE 1000

int GetFileSize(char* StrFileNaem); /*obtain the size of a file */
void *SendData(void *arg); /*the routine function in pthread_creat() */

int main (const int argc, const char **argv)
{
struct sockaddr_in serv;

int i;

if (argc != 3)
{
printf("Usage: <IpAddress> <filename>\n");
return -1;
}

bzero (&serv, sizeof(serv));
serv.sin_port = htons (PORTNUMBER);
serv.sin_family = AF_INET;
if (inet_pton (AF_INET, argv[1], &serv.sin_addr) <= 0)
{
printf ( "inet_pton error from %s.\n", argv[1] );
return -1;
}

for (i=0; i<=1000; i++)
{
int tempthread_id;

pthread_create (&tempthread_id, NULL, SendData, (void*)&serv);
}

while(1)
{
}

return 0;
}


void *SendData(void *arg)
{
int sockfd;

FILE *PSendFile;
int SizeOfFile;

char SendBuffer[MAX_LINE + 1];
int SendLen;

sockfd = socket (AF_INET, SOCK_STREAM, 0);
connect (sockfd, (struct sockaddr*)arg, sizeof(struct sockaddr_in));

/*get the pointer of file, and determin the size of this file */
if ((PSendFile = fopen ("data_base.txt","r")) == NULL)
{
printf("fail to open!\n");
return ;
}
while (!feof(PSendFile))
{
bzero (SendBuffer, MAX_LINE + 1);
SendLen = fread (SendBuffer, sizeof(char), MAX_LINE, PSendFile);
printf ("send %d bytes.\n", SendLen);
if ((SendLen = send (sockfd, PSendFile, SendLen, 0)) == -1)
{
printf ("send error!\n");
return ;
}
}
if (fclose(PSendFile) != 0)
{
printf("closed error!\n");
return ;
}
/*SizeOfFile = GetFileSize ("data_base.txt");*/



/*sendfile (sockfd, *SendFileName, SizeOfFile);
printf("sizeoffile = %d\n", SizeOfFile);*/
}

/*int GetFileSize(char* StrFileNaem)
{
FILE *tempFile;

int size ;

tempFile = fopen ("data_base.txt", "r");
fseek (tempFile, 0L, SEEK_END);
size = ftell (tempFile);
fclose (tempFile);
return size;
} */

 

posted @ 2013-06-02 01:38  hark.perfe  阅读(156)  评论(0编辑  收藏  举报