linux下C语言用socket网络编程怎么计算传输速度? 15
我自己写的服务器代码,要怎么修改才能实现显示传输速度?#defineMAX1024structtcp_thread_arg{intsockfd;//嵌套字连接符struc...
我自己写的服务器代码,要怎么修改才能实现显示传输速度?
#define MAX 1024
struct tcp_thread_arg {
int sockfd; //嵌套字连接符
struct sockaddr_in sockaddr; //ip地址
};
void *thread(void *arg){
while(1){
int flag = 0;
int recv_len; //接受字节长度
int write_len; //写入文件字节长度
int accept_id = ((struct tcp_thread_arg *)arg)->sockfd; //嵌套字描述符
struct sockaddr_in addr = ((struct tcp_thread_arg *)arg)->sockaddr; //地址信息
FILE *fp;
struct tm* ptr;
time_t lt;
char buf[MAX];
if((fp=fopen("data","w"))==NULL){
printf("file open error\n");
exit(1);
}
else{
memset(&buf,0,sizeof(buf));
}
while (recv_len = recv(accept_id, buf, MAX, 0)) {
flag++;
if (flag==1){
printf("正在接收来自%s的文件....\n",inet_ntoa(addr.sin_addr));
}
if(recv_len < 0) {
printf("Recieve Data From Server Failed!\n");
exit(1);
}
write_len = fwrite(buf, sizeof(char), recv_len, fp);
if (write_len > recv_len) {
printf("Write file failed\n");
exit(1);
}
memset(&buf,0,MAX);
}
if(flag>0){
printf("%s的文件传送完毕\n",inet_ntoa(addr.sin_addr));
}
lt = time(NULL);
ptr = gmtime(<);
rename("data",asctime(ptr));//重命名
fclose(fp);
close(accept_id);
return 0;
}
}
int main(int argc,char *argv[]){
int sockfd;
int accept_id;
int cli_addr_len;
struct sockaddr_in ser_addr;
struct sockaddr_in cli_addr;
FILE *fp;
char buf[MAX];
pthread_t pid;
struct tcp_thread_arg thread_arg;
if(argc!=2){
printf("usage :%s portnum filename\n", argv[0]);
exit(0);
}
if ((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
printf("creat socket failed\n");
exit(1);
}
memset(&ser_addr,0,sizeof(ser_addr));
ser_addr.sin_family = AF_INET; ser_addr.sin_port = htons(atoi(argv[1]));
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&ser_addr,sizeof(ser_addr))){
printf("bind socket failed\n");
exit(1);
}
if(listen(sockfd,5)){
printf("listen socket failed\n");
return 0;
}
while(1){
cli_addr_len = sizeof(cli_addr);
accept_id = accept(sockfd,(struct sockaddr *)&cli_addr,&cli_addr_len);
if(accept_id<0){
if(errno==EINTR){
continue;
}
else {
printf("accept client failed\n");
close(sockfd);
return 1;
}
}
else{
printf("客户端%s连接成功\n",inet_ntoa(cli_addr.sin_addr));
}
strcpy(buf,"请输入要传输的文件的完整路径:");
strcat(buf,"\n");
send(accept_id,buf,MAX,0);
thread_arg.sockfd = accept_id;
thread_arg.sockaddr = cli_addr;
if(pthread_create(&pid,NULL,thread,(void *)&thread_arg)){
printf("creat pthread failed\n");
close(accept_id);
close(sockfd);
return 1;
}
}
close(sockfd);
return 0;
} 展开
#define MAX 1024
struct tcp_thread_arg {
int sockfd; //嵌套字连接符
struct sockaddr_in sockaddr; //ip地址
};
void *thread(void *arg){
while(1){
int flag = 0;
int recv_len; //接受字节长度
int write_len; //写入文件字节长度
int accept_id = ((struct tcp_thread_arg *)arg)->sockfd; //嵌套字描述符
struct sockaddr_in addr = ((struct tcp_thread_arg *)arg)->sockaddr; //地址信息
FILE *fp;
struct tm* ptr;
time_t lt;
char buf[MAX];
if((fp=fopen("data","w"))==NULL){
printf("file open error\n");
exit(1);
}
else{
memset(&buf,0,sizeof(buf));
}
while (recv_len = recv(accept_id, buf, MAX, 0)) {
flag++;
if (flag==1){
printf("正在接收来自%s的文件....\n",inet_ntoa(addr.sin_addr));
}
if(recv_len < 0) {
printf("Recieve Data From Server Failed!\n");
exit(1);
}
write_len = fwrite(buf, sizeof(char), recv_len, fp);
if (write_len > recv_len) {
printf("Write file failed\n");
exit(1);
}
memset(&buf,0,MAX);
}
if(flag>0){
printf("%s的文件传送完毕\n",inet_ntoa(addr.sin_addr));
}
lt = time(NULL);
ptr = gmtime(<);
rename("data",asctime(ptr));//重命名
fclose(fp);
close(accept_id);
return 0;
}
}
int main(int argc,char *argv[]){
int sockfd;
int accept_id;
int cli_addr_len;
struct sockaddr_in ser_addr;
struct sockaddr_in cli_addr;
FILE *fp;
char buf[MAX];
pthread_t pid;
struct tcp_thread_arg thread_arg;
if(argc!=2){
printf("usage :%s portnum filename\n", argv[0]);
exit(0);
}
if ((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
printf("creat socket failed\n");
exit(1);
}
memset(&ser_addr,0,sizeof(ser_addr));
ser_addr.sin_family = AF_INET; ser_addr.sin_port = htons(atoi(argv[1]));
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&ser_addr,sizeof(ser_addr))){
printf("bind socket failed\n");
exit(1);
}
if(listen(sockfd,5)){
printf("listen socket failed\n");
return 0;
}
while(1){
cli_addr_len = sizeof(cli_addr);
accept_id = accept(sockfd,(struct sockaddr *)&cli_addr,&cli_addr_len);
if(accept_id<0){
if(errno==EINTR){
continue;
}
else {
printf("accept client failed\n");
close(sockfd);
return 1;
}
}
else{
printf("客户端%s连接成功\n",inet_ntoa(cli_addr.sin_addr));
}
strcpy(buf,"请输入要传输的文件的完整路径:");
strcat(buf,"\n");
send(accept_id,buf,MAX,0);
thread_arg.sockfd = accept_id;
thread_arg.sockaddr = cli_addr;
if(pthread_create(&pid,NULL,thread,(void *)&thread_arg)){
printf("creat pthread failed\n");
close(accept_id);
close(sockfd);
return 1;
}
}
close(sockfd);
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询