-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
93 lines (85 loc) · 2.1 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h> /* perror */
#include<stdlib.h> /* exit */
#include<sys/types.h> /* WNOHANG */
#include<sys/wait.h> /* waitpid */
#include<string.h> /* memset */
#include<time.h>
#include "common.h" /* socket layer wrapper */
#define true 1
#define false 0
#define MYPORT 9876 /* listen port */
#define BACKLOG 10 /* length of listen recv quene */
#define MAXDATASIZE 1000
int main()
{
int sockfd, new_fd; /* listen port£¬data port */
struct sockaddr_in sa; /* server address */
struct sockaddr_in their_addr; /* client address */
int sin_size;
char buf[MAXDATASIZE];
int nread;
time_t t_start,t_end;
if ((sockfd = Socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
else
{
printf("serverfd create succeed!\n");
}
sa.sin_family = AF_INET;
sa.sin_port = Htons(MYPORT);
sa.sin_addr.s_addr = INADDR_ANY;
memset(&(sa.sin_zero),0, 8);
if (Bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
{
perror("bind");
exit(1);
}
else
{
printf("bind succeed!\n");
}
if (Listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
else
{
printf("begin listening......\n");
}
sin_size = sizeof(struct sockaddr_in);
new_fd = Accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept");
}
else
{
printf("connect succeed!\n");
}
printf("Got connection from %s\n", Inet_ntoa(their_addr.sin_addr));
FILE *fp;
if((fp=fopen(Inputpath,"r"))==NULL)
{
perror("fopen");
return false;
}
printf("begin timing......\n");
t_start=time(NULL);
printf("recving......\n");
do{
nread=fread(buf,1,MAXDATASIZE,fp);
send(new_fd,buf,nread,0);
}while(nread>0);
fclose(fp);
printf("receving succeed!\n");
printf("timing end!\n");
t_end=time(NULL);
printf("time of transmission:%.0fs\n",difftime(t_end,t_start));
close(new_fd);
close(sockfd);
return true;
}