-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
181 lines (155 loc) · 3.94 KB
/
file.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_LINE 40
#define FILENAME "myfile.txt"
#define ListenNum 20
#define BufLen 1024
using namespace std;
typedef struct {
int id;
float x_coord;
float y_coord;
char name[MAX_LINE+1];
} MY_TYPE_T;
#define MAX_OBJECTS 3
/* Initialize an array of three objects */
MY_TYPE_T objects[MAX_OBJECTS]={
{0,1.5,8.4,"First-object"},
{1,9.2,7.4,"Second-object"},
{2,4.1,5.6,"Final-object"},
};
int _log(char * );
int server();
void _read_log();
int main()
{
server();
}
int server()
{
int sock,sock_new;
int buf_len;
socklen_t sin_size=sizeof(struct sockaddr);
int re,se;
char *buf_r=new char[BufLen];
char *buf_s=new char[BufLen];
struct sockaddr_in my_addr;
struct sockaddr_in ob_addr;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1) {cout<<"socket error"<<endl;exit(0);}
else {cout<<sock<<endl;}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(3490);
my_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(sock,(struct sockaddr *)& my_addr,sizeof(struct sockaddr))<0)
{
cout<<"bind error"<<endl;
exit(0);
}
cout<<"socket port: "<<ntohs(my_addr.sin_port)<<endl;
if(listen(sock,ListenNum)<0)
{
cout<<"listen error"<<endl;
exit(0);
}
while(1)
{
sock_new=accept(sock,(sockaddr *)&ob_addr,&sin_size);
if(sock_new<0)
{
cout<<"accept error"<<endl;
exit(0);
}
else
{
int p=fork();
if(p==0)
{
cout<<"connect to :"<<inet_ntoa(ob_addr.sin_addr)<<" : "<<htons(ob_addr.sin_port)<<endl;
int s=fork();
if(s==0)
{
do
{
memset(buf_r,0,sizeof(buf_r));
re=recv(sock_new,buf_r,BufLen,0);
if(re==-1)
{
cout<<"recv error"<<endl;
exit(0);
}
else if(re==0)
{
cout<<inet_ntoa(ob_addr.sin_addr)<<"connection ended"<<endl;
close(sock_new);
}
else
{
cout<<inet_ntoa(ob_addr.sin_addr)<<" -> "<<buf_r<<endl;
}
}while(re>0);
}
else
{
do
{
memset(buf_s,0,sizeof(buf_s));
cin.getline(buf_s,BufLen);
se=send(sock_new,buf_s,BufLen,0);
if(se==-1)
{
cout<<"send error"<<endl;
exit(0);
}
else
{
cout<<"sended!"<<endl;
}
}while(1);
}
}
}
}
close(sock);
return 0;
}
int _log(char *message)
{
int i;
FILE *fout;
int status;
/* Open the output file*/
fout = fopen(FILENAME, "w");
if(fout==(FILE *)0)
{
exit(-1);
}
/* Emit each of the objects,one per line*/
status = fputs(message,fout);
fclose(fout);
return status;
}
void _read_log()
{
int i;
// read a file
char str[1024];
FILE *fin;
i=0;
fin = fopen("/var/log/boot.log","r");
if(fin==(FILE *)0) exit(-1);
while(false == feof(fin))
{
i++;
fgets(str,1024,fin);
printf("%d lin The boots file content is :%s",i,str);
}
fclose(fin);
}