-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftpserver.c
73 lines (67 loc) · 1.5 KB
/
ftpserver.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
#include "mysocket.h"
#include "recv_file.h"
#include "myerror.h"
#include <stdlib.h>
int main()
{
//init and connect to mysql
if(init_connect_mysql() <0 )
{
fprintf(stderr,"connect to mysql error!\n");
return -1;
}
//waiting for transfer requsets from clients
struct in_addr ip;
if(0 == inet_aton("121.199.24.119",&ip))
{
print_error_location();
perror("ip is wrong!\n");
return -1;
}
int listenfd = listen_socket(ip.s_addr,5);
if(-1 == listenfd)
{
print_error_location();
return -1;
}
//print addr
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
bzero(&addr,addrlen);
if(-1 == getsockname(listenfd,(struct sockaddr*)&addr,&addrlen))
{
print_error_location();
return -1;
}
printf("ip addr is :%s,port:%d\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
struct sockaddr_in clientaddr;
socklen_t clientaddrlen = sizeof(clientaddr);
bzero(&clientaddr,clientaddrlen);
//accept a connect requset
while(1)
{
int acceptfd = accept(listenfd,(struct sockaddr*)&clientaddr,&clientaddrlen);
if(-1 == acceptfd)
{
if(errno == EINTR)
{
continue;
}
print_error_location();
perror("accept wrong!\n");
return -1;
}
//create a thread to recv file from client
printf("receive a connect!\n");
pthread_t tid;
PAR_TO_THREAD *p = (PAR_TO_THREAD*)malloc(sizeof(PAR_TO_THREAD));
p->acceptfd = acceptfd;
int err = pthread_create(&tid,NULL,file_recv,(void*)p);
if(0 != err)
{
perror("create thread faild\n");
continue;
}
}
return 0;
}