-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
110 lines (84 loc) · 2.4 KB
/
server.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
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
// gcc -Wall -o server server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char* argv[])
{
int sockfd, newsockfd;
char buffer [1024];
socklen_t clilen;
struct sockaddr_in6 serv_addr, cli_addr;
char client_addr_ipv6[100];
if(argc <2){
printf("Usage: <FILE> <PORT> <SNUMMER>\n");
return EXIT_FAILURE;
}
int portno = atoi(argv[1]);
int snum = atoi(argv[2]);
printf("IPv6 TCP Server started.\n");
// Socket erstellen
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if(sockfd == -1){
perror("socket");
return EXIT_FAILURE;
}
// Empfänger-Addresse vorbereiten
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_port = htons(portno);
// Socket an Port binden
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1){
perror("bind");
close(sockfd);
return EXIT_FAILURE;
}
// Auf eingehende Verbindungen hören
if(listen(sockfd,5) == -1){
perror("listen");
close(sockfd);
return EXIT_FAILURE;
}
printf("Waiting for connection...\n");
// Verbindung akzeptieren
newsockfd = accept(sockfd, (struct sockaddr *) & cli_addr, & clilen);
if(newsockfd == -1){
perror("accept");
close(sockfd);
return EXIT_FAILURE;
}
inet_ntop(AF_INET6, &(cli_addr.sin6_addr),client_addr_ipv6,100);
printf("New connection from: %s\n", client_addr_ipv6);
printf("Server and Client are connected.\n");
while(1)
{
// Lesen Daten von Server
bzero(buffer,1024);
if(recv(newsockfd,buffer,1024,0) == -1){
perror("recv");
close(newsockfd);
exit(1);
}
printf("Client[s%d]: %s", snum,buffer);
// Antwort senden
bzero(buffer,1024);
fgets(buffer,1024,stdin);
if(send(newsockfd,buffer,1024,0) == -1) {
perror("send");
close(newsockfd);
exit(1);
}
// Verbindung schließen
int i = strncmp("Bye!",buffer,3);
if(i == 0) break;
}
// Socket schließen
close(newsockfd);
close(sockfd);
return 0;
}