mirror of
https://github.com/ada-dmitry/TMP_C712.git
synced 2026-09-24 01:00:19 +00:00
repack rep
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
#define DATA "\
|
||||
<!DOCTYPE html>
|
||||
<html lang = "en">
|
||||
<head>
|
||||
<meta charset = "UTF-8">
|
||||
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
|
||||
<title> Document</ title>
|
||||
</ head>
|
||||
<body>
|
||||
|
||||
</ body> < / html >
|
||||
"
|
||||
@@ -0,0 +1,146 @@
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define FIN "exit"
|
||||
#define FINS "exit_s"
|
||||
#define FINC "exit_c"
|
||||
#define BUF 1024
|
||||
#define DATA "Answer"
|
||||
#define GET "GET"
|
||||
#define HTTPOK "HTTP/1.1 200 OK\n\n"
|
||||
#define INDEX "index.html"
|
||||
// #include "data.h"
|
||||
|
||||
void session(int msgsock)
|
||||
{
|
||||
int sock;
|
||||
socklen_t length;
|
||||
struct sockaddr_in server;
|
||||
char ibuf[BUF];
|
||||
char obuf[BUF];
|
||||
int rval, sval;
|
||||
int fd, offt;
|
||||
do
|
||||
{
|
||||
memset(ibuf, 0, BUF);
|
||||
if ((rval = read(msgsock, ibuf, 1024)) == -1)
|
||||
perror("reading stream message");
|
||||
dprintf(1, "-[%d]\n", rval);
|
||||
if (rval == 0)
|
||||
printf("Connection finished\n");
|
||||
else
|
||||
{
|
||||
dprintf(1, "[%s]\n", ibuf);
|
||||
memset(obuf, 0, BUF);
|
||||
if (!strncmp(ibuf, GET, 3))
|
||||
{
|
||||
#ifdef INDEX
|
||||
strcpy(obuf, HTTPOK);
|
||||
offt = strlen(obuf);
|
||||
fd = open(INDEX, O_RDONLY);
|
||||
read(fd, obuf + offt, BUF - offt);
|
||||
close(fd);
|
||||
#else
|
||||
strcpy(obuf, DATA1);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(obuf, DATA);
|
||||
}
|
||||
sval = send(msgsock, obuf, strlen(obuf), 0);
|
||||
dprintf(1, "Sent [%d]\n", sval);
|
||||
}
|
||||
} while ((rval > 0) && (strncmp(ibuf, GET, 3)));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
socklen_t length;
|
||||
struct sockaddr_in server;
|
||||
int msgsock;
|
||||
pid_t pid;
|
||||
const int enable = 1;
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == -1)
|
||||
{
|
||||
perror("opening stream socket");
|
||||
exit(1);
|
||||
}
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
|
||||
exit(2);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (argc == 2)
|
||||
server.sin_port = htons(atoi(argv[1]));
|
||||
else
|
||||
server.sin_port = 0;
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&server, sizeof server) == -1)
|
||||
{
|
||||
perror("binding stream socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
length = sizeof server;
|
||||
if (getsockname(sock, (struct sockaddr *)&server, &length) == -1)
|
||||
{
|
||||
perror("getting socket name");
|
||||
exit(1);
|
||||
}
|
||||
printf("Socket port %d\n", ntohs(server.sin_port));
|
||||
|
||||
listen(sock, 5);
|
||||
pid_t cpid;
|
||||
do
|
||||
{
|
||||
if ((msgsock = accept(sock, (struct sockaddr *)NULL, (socklen_t *)NULL)) == -1)
|
||||
perror("accept");
|
||||
else
|
||||
{
|
||||
cpid = fork();
|
||||
if (cpid == 0)
|
||||
{
|
||||
session(msgsock);
|
||||
}
|
||||
}
|
||||
close(msgsock);
|
||||
} while (1);
|
||||
/*
|
||||
do {
|
||||
if ((msgsock = accept(sock,(struct sockaddr *) NULL,(socklen_t *) NULL)) == -1)
|
||||
perror("accept");
|
||||
else
|
||||
do {
|
||||
memset(ibuf, 0, BUF);
|
||||
if ((rval = read(msgsock, ibuf, 1024)) == -1) perror("reading stream message");
|
||||
dprintf(1, "-[%d]\n", rval);
|
||||
if (rval == 0) printf("Connection finished\n");
|
||||
else{
|
||||
dprintf(1, "[%s]\n", ibuf);
|
||||
memset(obuf, 0, BUF);
|
||||
if (!strncmp(ibuf, GET, 3)){
|
||||
strcpy(obuf, DATA1);
|
||||
}else{
|
||||
strcpy(obuf, DATA);
|
||||
}
|
||||
sval = send(msgsock, obuf, strlen(obuf), 0);
|
||||
dprintf(1, "Sent [%d]\n", sval);
|
||||
}
|
||||
} while ((rval > 0) && (strncmp(ibuf, GET, 3)));
|
||||
close(msgsock);
|
||||
} while (strcmp(ibuf, FIN))
|
||||
*/
|
||||
exit(0);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
if (argc > 1)
|
||||
dir = opendir(argv[1]);
|
||||
else
|
||||
dir = opendir("/");
|
||||
if (!dir)
|
||||
{
|
||||
perror("diropen");
|
||||
return 1;
|
||||
};
|
||||
while ((entry = readdir(dir)) != NULL)
|
||||
{
|
||||
printf("%d - %s [%d] %d\n",
|
||||
entry->d_ino, entry->d_name, entry->d_type,
|
||||
entry->d_reclen);
|
||||
};
|
||||
closedir(dir);
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
struct dirent **namelist;
|
||||
int n;
|
||||
char dir[255] = ".";
|
||||
|
||||
if (argc > 1)
|
||||
strcpy(dir, argv[1]);
|
||||
n = scandir(dir, &namelist, 0, alphasort);
|
||||
if (n < 0)
|
||||
perror("scandir");
|
||||
else
|
||||
{
|
||||
while (n--)
|
||||
{
|
||||
printf("%s\n", namelist[n]->d_name);
|
||||
free(namelist[n]);
|
||||
}
|
||||
free(namelist);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_PATH "tpf_unix_sock.server"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int server_sock, rc;
|
||||
socklen_t len;
|
||||
int bytes_rec = 0;
|
||||
struct sockaddr_un server_sockaddr, peer_sock;
|
||||
char buf[256];
|
||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
memset(buf, 0, 256);
|
||||
|
||||
server_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (server_sock == -1)
|
||||
{
|
||||
perror("socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
server_sockaddr.sun_family = AF_UNIX;
|
||||
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
||||
len = sizeof(server_sockaddr);
|
||||
unlink(SOCK_PATH);
|
||||
rc = bind(server_sock, (struct sockaddr *)&server_sockaddr, len);
|
||||
if (rc == -1)
|
||||
{
|
||||
perror("bind");
|
||||
close(server_sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("waiting to recvfrom...\n");
|
||||
bytes_rec = recvfrom(server_sock, buf, 256, 0, (struct sockaddr *)&peer_sock, &len);
|
||||
if (bytes_rec == -1)
|
||||
{
|
||||
perror("recv");
|
||||
close(server_sock);
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("DATA RECEIVED = %s\n", buf);
|
||||
}
|
||||
|
||||
close(server_sock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DATA "exit"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
socklen_t length;
|
||||
struct sockaddr_in server;
|
||||
int msgsock;
|
||||
char buf[1024];
|
||||
int rval;
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == -1)
|
||||
{
|
||||
perror("opening stream socket");
|
||||
exit(1);
|
||||
}
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = htoml(INADDR_ANY);
|
||||
if(argc == 2){
|
||||
server.sin_port = htons(atoi(argv[1]));
|
||||
}
|
||||
else{
|
||||
server.sin_port = 0;
|
||||
}
|
||||
if(bind(sock, (struct sockaddr *) &server, sizeof server) == -1){
|
||||
perror("binding stream socket");
|
||||
exit(1);
|
||||
}
|
||||
length = sizeof server;
|
||||
if(getsockname(sock,(struct sockaddr *) &server, &length) == -1){
|
||||
perror("getting socket name");
|
||||
exit(1);
|
||||
}
|
||||
printf("socket port %d\n", ntohs(server.sin_port));
|
||||
listen(sock, 5);
|
||||
do{
|
||||
if ((msgsock = accept(sock, (struct sockaddr *)NULL, (socklen_t *)NULL)) == -1)
|
||||
{
|
||||
perror("accept");
|
||||
}
|
||||
else
|
||||
do
|
||||
{
|
||||
memset(buf, 0, sizeof buf);
|
||||
if ((rval = read(msgsock, buf, 1024)) == -1)
|
||||
perror("reading stream message");
|
||||
dprintf(1, "-[%d]\n", rval);
|
||||
if (rval == 0)
|
||||
{
|
||||
printf("connecting finished\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
dprintf(1, "[%s]\n", buf);
|
||||
}
|
||||
} while ((rval > 0) && (strcmp(buf, FIN)));
|
||||
close(msgsock);
|
||||
|
||||
}while(strcmp(buf,FIN));
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
//./client_app stud*** "port"
|
||||
|
||||
#define DATA "Hello, World!"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
struct sockaddr_in server;
|
||||
struct hostent *hp;
|
||||
char buf[1024];
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
printf("c_inet hostname port [data]\n");
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
||||
{
|
||||
perror("opening stream socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
hp = malloc(sizeof(struct hostent));
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
hp = gethostbyname(argv[1]);
|
||||
if (hp == (struct hostent *)0)
|
||||
{
|
||||
fprintf(stderr, "%s: unknown host\n", argv[1]);
|
||||
exit(2);
|
||||
}
|
||||
memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
||||
server.sin_port = htons(atoi(argv[2]));
|
||||
if (connect(sock, (struct sockaddr *)&server, sizeof server) == -1)
|
||||
{
|
||||
perror("connecting stream socket");
|
||||
exit(1);
|
||||
}
|
||||
getchar();
|
||||
memset(buf, 0, 1024);
|
||||
if (argc > 3)
|
||||
{
|
||||
printf("[%s]\n", argv[3]);
|
||||
strcpy(buf, argv[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(buf, DATA);
|
||||
}
|
||||
if (write(sock, buf, strlen(buf)) == -1)
|
||||
perror("writing on stream socket");
|
||||
close(sock);
|
||||
exit(0);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,70 @@
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FIN "exit"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
socklen_t length;
|
||||
struct sockaddr_in server;
|
||||
int msgsock;
|
||||
char buf[1024];
|
||||
int rval;
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == -1)
|
||||
{
|
||||
perror("opening stream socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (argc == 2)
|
||||
server.sin_port = htons(atoi(argv[1]));
|
||||
else
|
||||
server.sin_port = 0;
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&server, sizeof server) == -1)
|
||||
{
|
||||
perror("binding stream socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
length = sizeof server;
|
||||
if (getsockname(sock, (struct sockaddr *)&server, &length) == -1)
|
||||
{
|
||||
perror("getting socket name");
|
||||
exit(1);
|
||||
}
|
||||
printf("Socket port %d\n", ntohs(server.sin_port));
|
||||
|
||||
listen(sock, 5);
|
||||
do
|
||||
{
|
||||
if ((msgsock = accept(sock, (struct sockaddr *)NULL, (socklen_t *)NULL)) == -1)
|
||||
perror("accept");
|
||||
else
|
||||
do
|
||||
{
|
||||
memset(buf, 0, sizeof buf);
|
||||
if ((rval = read(msgsock, buf, 1024)) == -1)
|
||||
perror("reading stream message");
|
||||
dprintf(1, "-[%d]\n", rval);
|
||||
if (rval == 0)
|
||||
printf("Connection finished\n");
|
||||
else
|
||||
{
|
||||
dprintf(1, "[%s]\n", buf);
|
||||
}
|
||||
// if (!strcmp(buf,FIN)) {break;}
|
||||
} while ((rval > 0) && (strcmp(buf, FIN)));
|
||||
close(msgsock);
|
||||
} while (strcmp(buf, FIN));
|
||||
exit(0);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
+52
-20
@@ -9,8 +9,19 @@
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||
#define SerrExit(s,msg) do { close(s); perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||
#define errExit(msg) \
|
||||
do \
|
||||
{ \
|
||||
perror(msg); \
|
||||
_exit(EXIT_FAILURE); \
|
||||
} while (0)
|
||||
#define SerrExit(s, msg) \
|
||||
do \
|
||||
{ \
|
||||
close(s); \
|
||||
perror(msg); \
|
||||
_exit(EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
#define SERVER_PATH "/tmp/unix_sock.server"
|
||||
#define CLIENT_PATH "unix_sock.client"
|
||||
@@ -20,7 +31,8 @@
|
||||
#define FINC "exit_c"
|
||||
// #define INFO
|
||||
|
||||
int rcvBuffer(int sock_fd){
|
||||
int rcvBuffer(int sock_fd)
|
||||
{
|
||||
int rcvBufferSize;
|
||||
socklen_t sockOptSize = sizeof(rcvBufferSize);
|
||||
getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufferSize, &sockOptSize);
|
||||
@@ -30,15 +42,18 @@ int rcvBuffer(int sock_fd){
|
||||
return rcvBufferSize;
|
||||
}
|
||||
|
||||
int recvCount(const void *s, int size){
|
||||
int recvCount(const void *s, int size)
|
||||
{
|
||||
register int count;
|
||||
register int mark;
|
||||
register char *p;
|
||||
p = (char *)s;
|
||||
count = 0;
|
||||
mark = 0;
|
||||
do {
|
||||
if (*p && !(*(p+1))) count++;
|
||||
do
|
||||
{
|
||||
if (*p && !(*(p + 1)))
|
||||
count++;
|
||||
#ifdef INFO
|
||||
printf("[%c][%X]\n", *p, *p);
|
||||
#endif
|
||||
@@ -48,7 +63,8 @@ int recvCount(const void *s, int size){
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int client_sock, rc, len;
|
||||
struct sockaddr_un server_sockaddr;
|
||||
@@ -64,55 +80,66 @@ int main(int argc,char *argv[]){
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (client_sock == -1) {
|
||||
if (client_sock == -1)
|
||||
{
|
||||
perror("socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
client_sockaddr.sun_family = AF_UNIX;
|
||||
strcpy(client_sockaddr.sun_path, cpath);
|
||||
|
||||
unlink(cpath);
|
||||
rc = bind(client_sock, (struct sockaddr *)&client_sockaddr, len);
|
||||
if (rc == -1) SerrExit(client_sock,"bind");
|
||||
if (rc == -1)
|
||||
SerrExit(client_sock, "bind");
|
||||
|
||||
server_sockaddr.sun_family = AF_UNIX;
|
||||
strcpy(server_sockaddr.sun_path, SERVER_PATH);
|
||||
rc = connect(client_sock, (struct sockaddr *)&server_sockaddr, len);
|
||||
if (rc == -1) SerrExit(client_sock,"connect");
|
||||
if (rc == -1)
|
||||
SerrExit(client_sock, "connect");
|
||||
|
||||
ibs = rcvBuffer(client_sock);
|
||||
if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||
if ((ibuf = (char **)malloc(ibs)) == NULL)
|
||||
errExit("malloc");
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
memset(obuf, 0, obs);
|
||||
printf("> ");
|
||||
scanf("%s", obuf);
|
||||
|
||||
printf("Sending data...\n");
|
||||
rc = send(client_sock, obuf, strlen(obuf) + 1, 0);
|
||||
if (rc == -1) {
|
||||
if (rc == -1)
|
||||
{
|
||||
perror("send");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Data sent!\n");
|
||||
}
|
||||
|
||||
printf("Waiting to recieve data...\n");
|
||||
memset(ibuf, 0, ibs);
|
||||
rc = recv(client_sock, ibuf, ibs, 0);
|
||||
if (rc == -1) {
|
||||
if (rc == -1)
|
||||
{
|
||||
perror("recv");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
int c = recvCount(ibuf, ibs);
|
||||
#ifdef INFO
|
||||
char *p = (char *)ibuf;
|
||||
printf("count: %d resieved\n", c);
|
||||
for (int i = 0; i < 32; i++){
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
printf("[%c][%X]", *p, *p);
|
||||
p++;
|
||||
}
|
||||
@@ -122,9 +149,14 @@ int main(int argc,char *argv[]){
|
||||
char p[obs];
|
||||
char *tp = (char *)ibuf;
|
||||
// if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||
for (int i = 0; i < c; i++){
|
||||
for (int i = 0; i < c; i++)
|
||||
{
|
||||
memset(p, 0, obs);
|
||||
while (*tp) {strncat(p,tp,1);tp++;}
|
||||
while (*tp)
|
||||
{
|
||||
strncat(p, tp, 1);
|
||||
tp++;
|
||||
}
|
||||
tp++;
|
||||
// printf("DATA RECEIVED [%ld] = %s\n", strlen(ibuf[i]),ibuf[i]);
|
||||
printf("DATA RECEIVED [%ld] = %s\n", strlen(p), p);
|
||||
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
+52
-22
@@ -11,7 +11,12 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||
#define errExit(msg) \
|
||||
do \
|
||||
{ \
|
||||
perror(msg); \
|
||||
_exit(EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
#define SOCK_PATH "/tmp/unix_sock.server"
|
||||
#define DATA "Hello from server"
|
||||
@@ -19,12 +24,15 @@
|
||||
#define FINS "exit_s"
|
||||
#define FINC "exit_c"
|
||||
|
||||
void child_handler(int sig) {
|
||||
void child_handler(int sig)
|
||||
{
|
||||
pid_t pid;
|
||||
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
||||
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
|
||||
;
|
||||
}
|
||||
|
||||
void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockaddr_un client_sockaddr){
|
||||
void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockaddr_un client_sockaddr)
|
||||
{
|
||||
int rc;
|
||||
char inbuf[BUF];
|
||||
char outbuf[BUF];
|
||||
@@ -38,20 +46,27 @@ void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockadd
|
||||
|
||||
len = sizeof(client_sockaddr);
|
||||
rc = getpeername(client_sock, (struct sockaddr *)&client_sockaddr, &len);
|
||||
if (rc == -1){
|
||||
if (rc == -1)
|
||||
{
|
||||
close(client_sock);
|
||||
errExit("getpeer");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[%d] Client socket filepath: %s\n", pid, client_sockaddr.sun_path);
|
||||
}
|
||||
do {
|
||||
do
|
||||
{
|
||||
printf("waiting for %s...\n", client_sockaddr.sun_path);
|
||||
memset(inbuf, 0, bs);
|
||||
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
||||
if (bytes_rec == -1){
|
||||
if (bytes_rec == -1)
|
||||
{
|
||||
close(client_sock);
|
||||
errExit("recv");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[%s]: %s\n", client_sockaddr.sun_path, inbuf);
|
||||
}
|
||||
|
||||
@@ -59,18 +74,26 @@ void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockadd
|
||||
strcpy(outbuf, DATA);
|
||||
printf("Sending data...\n");
|
||||
rc = send(client_sock, outbuf, strlen(outbuf) + 1, 0);
|
||||
if (rc == -1) {
|
||||
if (rc == -1)
|
||||
{
|
||||
close(client_sock);
|
||||
errExit("send");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Data sent!\n");
|
||||
}
|
||||
if (strcmp(inbuf,FINS) == 0 ) {kill(ppid,SIGTERM);break;}
|
||||
if (strcmp(inbuf, FINS) == 0)
|
||||
{
|
||||
kill(ppid, SIGKILL);
|
||||
break;
|
||||
}
|
||||
} while (strcmp(inbuf, FINC));
|
||||
memset(outbuf, 0, bs);
|
||||
sprintf(outbuf, "Exit %d\n", pid);
|
||||
rc = send(client_sock, outbuf, strlen(outbuf) + 1, 0);
|
||||
if (rc == -1) {
|
||||
if (rc == -1)
|
||||
{
|
||||
close(client_sock);
|
||||
errExit("send");
|
||||
}
|
||||
@@ -78,7 +101,8 @@ void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockadd
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(void){
|
||||
int main(void)
|
||||
{
|
||||
|
||||
int server_sock, client_sock, rc;
|
||||
socklen_t len;
|
||||
@@ -98,43 +122,49 @@ int main(void){
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (server_sock == -1) errExit("socket");
|
||||
if (server_sock == -1)
|
||||
errExit("socket");
|
||||
|
||||
server_sockaddr.sun_family = AF_UNIX;
|
||||
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
||||
|
||||
unlink(SOCK_PATH);
|
||||
rc = bind(server_sock, (struct sockaddr *)&server_sockaddr, len);
|
||||
if (rc == -1){
|
||||
if (rc == -1)
|
||||
{
|
||||
close(server_sock);
|
||||
errExit("bind");
|
||||
}
|
||||
|
||||
rc = listen(server_sock, backlog);
|
||||
if (rc == -1){
|
||||
if (rc == -1)
|
||||
{
|
||||
close(server_sock);
|
||||
errExit("listen");
|
||||
}
|
||||
|
||||
printf("socket listening...\n");
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
client_sock = accept(server_sock, (struct sockaddr *)&client_sockaddr, &len);
|
||||
if (client_sock == -1){
|
||||
if (client_sock == -1)
|
||||
{
|
||||
close(server_sock);
|
||||
close(client_sock);
|
||||
errExit("accept");
|
||||
}
|
||||
if ((pid = fork()) == -1 ) {
|
||||
if ((pid = fork()) == -1)
|
||||
{
|
||||
close(server_sock);
|
||||
close(client_sock);
|
||||
errExit("fork");
|
||||
}
|
||||
if (pid == 0) session(client_sock,server_sockaddr,client_sockaddr);
|
||||
if (pid == 0)
|
||||
session(client_sock, server_sockaddr, client_sockaddr);
|
||||
close(client_sock);
|
||||
} while (1);
|
||||
|
||||
|
||||
close(server_sock);
|
||||
unlink(SOCK_PATH);
|
||||
|
||||
|
||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user