-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.1.c
executable file
·74 lines (62 loc) · 1.59 KB
/
server1.1.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
74
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BACKLOG 10
#define MAX_NAMELEN 50
#define len 2048
struct klien{
char nama[MAX_NAMELEN];
int sock;
};
int main (int argc, char* argv[]) {
if (argc < 2) {
printf("Penggunaan: server port");
exit(-1);
}
struct klien client;
struct sockaddr_storage their_addr;
socklen_t addr_size;
struct addrinfo hints, *res;
int sockfd, new_fd,a;
int status, bytes_sent, bytes_recv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(NULL, argv[1], &hints, &res);
if (status) {
printf("Gagal saat getaddrinfo!, status: %d\n", status);
exit(-1);
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
if (status) {
printf("Gagal saat bind!, status: %d\n", status);
exit (-1);
}
listen(sockfd, BACKLOG);
while(1)
{
new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &addr_size);
client.sock = new_fd;
printf("Client %d konek\n",client.sock);
char msg[len]={};
while(bytes_recv = recv(client.sock, msg, len, 0))
{
bytes_sent = send(client.sock, msg,len,0);
printf("Ini isi sent: ");
for(a=0; a<len; a++)printf("%c",msg[a]);
printf("\n\n");
if (new_fd < 0) {
printf("Gagal saat accept!");
exit (-1);
}
}
printf("Berhasil bung!");
close(socket);
}
}