-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.c
72 lines (57 loc) · 1.28 KB
/
server1.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#define LIMIT 8
#define PORT 5000
#define SIZE 80
int main()
{
int sockfd,len,n;
char *buffer;//*c
buffer=(char *)malloc(SIZE);
int ifd, ofd;
char list[LIMIT],filename[SIZE];
ssize_t cin, cout;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET, SOCK_DGRAM,0);
if(sockfd<0)
{
perror("socket");
exit(0);
}
printf("socket success server\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)))<0)
{
perror("bind");
exit(0);
}
printf("bind success\n");
fputs("***server started***\n",stdout);
len=sizeof(cliaddr);
recvfrom(sockfd,filename,SIZE,0,(struct sockaddr *) &cliaddr,&len);
ifd = open (filename, O_RDONLY);
if (ifd == -1)
{
printf ("\nerror open for reading");
return -1;
}
while(1){
cin = read (ifd, &list, LIMIT);
if(cin==0)
break;
sendto(sockfd,&list,sizeof(list),0,(struct sockaddr*)&cliaddr,len);
printf(">");
}
strcpy(list,"stop");
sendto(sockfd,&list,sizeof(list),0,(struct sockaddr*)&cliaddr,len);
return 0;
}