Answer with received string.
This commit is contained in:
parent
6fd4569a6d
commit
54216cfaf4
1 changed files with 63 additions and 34 deletions
95
netcalc.c
95
netcalc.c
|
@ -7,15 +7,17 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
int cleanup(int sockfd)
|
#define BUFLEN 67
|
||||||
|
|
||||||
|
int sockfd;
|
||||||
|
|
||||||
|
void cleanup(int signum)
|
||||||
{
|
{
|
||||||
if (close(sockfd) != 0) {
|
if (close(sockfd) != 0) {
|
||||||
perror("listen");
|
perror("close");
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int prepaddr(char *host, char *port, struct addrinfo **ainfo)
|
int prepaddr(char *host, char *port, struct addrinfo **ainfo)
|
||||||
|
@ -56,69 +58,96 @@ void printaddr(struct addrinfo *ainfo)
|
||||||
int prepsocket(struct addrinfo *ainfo)
|
int prepsocket(struct addrinfo *ainfo)
|
||||||
{
|
{
|
||||||
|
|
||||||
int sockfd = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
sockfd = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
||||||
|
|
||||||
if (sockfd == -1) {
|
if (sockfd == -1) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sockfd, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
|
||||||
perror("bind");
|
|
||||||
cleanup(sockfd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
|
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
|
||||||
perror("setsockopt");
|
perror("setsockopt");
|
||||||
cleanup(sockfd);
|
cleanup(0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bind(sockfd, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
||||||
|
perror("bind");
|
||||||
|
cleanup(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(sockfd, 5) != 0) {
|
if (listen(sockfd, 5) != 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
cleanup(sockfd);
|
cleanup(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int server()
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
struct sockaddr_storage c_addr;
|
||||||
|
socklen_t sin_size = sizeof c_addr;
|
||||||
|
int c_fd = accept(sockfd, (struct sockaddr *)&c_addr, &sin_size);
|
||||||
|
|
||||||
|
if (c_fd == -1) {
|
||||||
|
perror("accept");
|
||||||
|
continue; // we do not care
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[BUFLEN];
|
||||||
|
|
||||||
|
ssize_t byte_count = recv(c_fd, buf, sizeof buf, 0);
|
||||||
|
if (byte_count == -1) {
|
||||||
|
perror("recv");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("received %ld bytes containig %s", byte_count, buf);
|
||||||
|
|
||||||
|
//calc(&buf, strlen(buf));
|
||||||
|
|
||||||
|
if (send(c_fd, buf, strlen(buf), 0) == -1) {
|
||||||
|
perror("send");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(c_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc != 3) {
|
char* host = "localhost";
|
||||||
fprintf(stderr,"usage: netcalc hostname port\n");
|
char* port = "5000";
|
||||||
return 1;
|
|
||||||
|
if (argc > 1) {
|
||||||
|
port = argv[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo *ainfo;
|
struct addrinfo *ainfo;
|
||||||
if (prepaddr(argv[1], argv[2], &ainfo) != 0) {
|
if (prepaddr(host, port, &ainfo) != 0) {
|
||||||
fprintf(stderr, "server: failed to obtain adress.");
|
fprintf(stderr, "server: failed to obtain adress.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printaddr(ainfo);
|
printaddr(ainfo);
|
||||||
|
|
||||||
int sockfd;
|
|
||||||
if ((sockfd = prepsocket(ainfo)) == -1) {
|
if ((sockfd = prepsocket(ainfo)) == -1) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
struct sigaction action;
|
||||||
struct sockaddr_storage c_addr;
|
memset(&action, 0, sizeof(struct sigaction));
|
||||||
socklen_t sin_size = sizeof c_addr;
|
action.sa_handler = cleanup;
|
||||||
int c_fd = accept(sockfd, (struct sockaddr *)&c_addr, &sin_size);
|
sigaction(SIGTERM, &action, NULL);
|
||||||
if (c_fd == -1) {
|
|
||||||
perror("accept");
|
|
||||||
continue; // we do not care
|
|
||||||
}
|
|
||||||
if (send(c_fd, "Hello world!\n", 14, 0) == -1) {
|
|
||||||
perror("send");
|
|
||||||
}
|
|
||||||
close(c_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup(sockfd);
|
server();
|
||||||
|
|
||||||
|
cleanup(0);
|
||||||
freeaddrinfo(ainfo); // free the linked list
|
freeaddrinfo(ainfo); // free the linked list
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue