Repository navigation
Expand file tree
/
Copy pathScheduledServer.cpp
More file actions
127 lines (118 loc) · 2.45 KB
/
Copy pathScheduledServer.cpp
File metadata and controls
127 lines (118 loc) · 2.45 KB
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
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wait.h>
#include <signal.h>
#include <fcntl.h>
// concurrent server
void transact_with_client(int sock);
int main()
{
//socket creation
int serv_fd = socket(AF_INET,SOCK_STREAM,0);
if (serv_fd == -1)
{
perror("socket creation error");
exit(1);
}
//initialize the sockaddr_in structure
struct sockaddr_in sock_addr_serv;// sockaddr_in for IPV4
sock_addr_serv.sin_family = AF_INET;
sock_addr_serv.sin_port = 9888;
sock_addr_serv.sin_addr.s_addr = inet_addr("127.0.0.1");
//bind the socket with ip address and port
if (bind(serv_fd,(struct sockaddr *)&sock_addr_serv,sizeof(sock_addr_serv))==-1)
{
perror("bind error");
exit(1);
}
// listening (waiting) for client request
if (listen(serv_fd,5)==-1)
{
perror("listen error");
exit(1);
}
cout << "server waiting for client to connect" << endl;
//accepting client request
struct sockaddr_in sock_addr_cli;
socklen_t cli_len = sizeof(sock_addr_cli);
int client_fd=accept(serv_fd,(struct sockaddr *)&sock_addr_cli,&cli_len);
if (client_fd==-1)
{
perror(" accept error");
exit(1);
}
while (1)
{
transact_with_client(client_fd);
//close(client_fd);
}
//close socket
/*if (close(serv_fd)==-1)
{
perror("socket close error");
exit(1);
}*/
}
void transact_with_client(int sock)
{
char ch[100];
int fd;
int n =read(sock,ch,sizeof(ch));
while(n!=0)
{
send(sock,"received",8,0);
if (strcmp(ch,"filename")==0)
{
char fn[30];
n=read(sock,fn,sizeof(fn));
cout << "fn " << fn << endl;
send(sock,"received",8,0);
fd = open(fn,O_CREAT|O_WRONLY,0660);
bzero(fn,sizeof(fn));
}
else if (strcmp(ch,"end")==0)
{
close(fd);
}
else
{
write(fd,ch,n);
bzero(ch,sizeof(ch));
}
n =read(sock,ch,sizeof(ch));
}
/*ch[n]='\0';
int fd;
puts(ch);
cout << n << endl;
send(sock,"received",8,0);
if (strcmp(ch,"filename")==0)
{
cout << "wihtin if " << endl;
char fn[30];
n=read(sock,fn,sizeof(fn));
send(sock,"received",8,0);
fn[n]='\0';
cout << fn << endl;
fd = open(fn,O_CREAT|O_WRONLY,0660);
}
else
{
cout << " ESESSDEE; " << endl;
n=read(sock,buf,sizeof(buf));
send(sock,"received",8,0);
cout << buf << endl;
write(fd,buf,n);
bzero(buf,sizeof(buf));
}
bzero(ch,sizeof(ch));*/
//close(fd);
}