日期:2014-05-16 浏览次数:20960 次
/**
*author:Debris
*time :2014.03
*/
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#define SERVER_PORT 8421
#define BUFFER_SIZE 2*1024
int main(int argc,char **argv[])
{
int sockfd;
char buffer[BUFFER_SIZE];
int len;
FILE *fp;
int n;
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
printf("create socket error\n");
exit(1);
}
if(connect(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0)
{
printf("connect error\n");
exit(1);
}
if((fp = fopen("source.txt","r")) == NULL)
{
printf("file open error\n");
exit(1);
}
//fwrite();
bzero(buffer,BUFFER_SIZE);
n = fread(buffer,1,sizeof(buffer),fp);
if(n == 0)
{
printf("read error\n");
exit(1);
}
while(1)
{
//get running time that you want to test codes
clock_t start, finish;
double usetime;
start = clock();
//your testing codes
len = send(sockfd,buffer,strlen(buffer),0);
finish = clock();
usetime= (double)(finish - start) / CLOCKS_PER_SEC;
printf( "程序用时%f 秒\n ", usetime);
//scanf("%s",&buffer);
//len = send(sockfd,buffer,strlen(buffer),0);
printf("the size of len is:%d\n",len);
if(len > 0)
printf("send success\n");
else
printf("send error\n");
bzero(buffer,BUFFER_SIZE);
scanf("%s",&buffer);
}
fclose(fp);
close(sockfd);
return 0;
}
/**
*author:Debris
*time :2014.03
*/
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#define SERVER_PORT 8421
#define BUFFER_SIZE 1024
int main(int argc,char **argv[])
{
int sockfd;
int connfd;
FILE *fp;
char buffer[BUFFER_SIZE];
socklen_t len;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(SERVER_PORT);