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.
+111
-79
@@ -9,8 +9,19 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
#define errExit(msg) \
|
||||||
#define SerrExit(s,msg) do { close(s); perror(msg); _exit(EXIT_FAILURE); } while (0)
|
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 SERVER_PATH "/tmp/unix_sock.server"
|
||||||
#define CLIENT_PATH "unix_sock.client"
|
#define CLIENT_PATH "unix_sock.client"
|
||||||
@@ -18,125 +29,146 @@
|
|||||||
#define BUF 128
|
#define BUF 128
|
||||||
#define FINS "exit_s"
|
#define FINS "exit_s"
|
||||||
#define FINC "exit_c"
|
#define FINC "exit_c"
|
||||||
//#define INFO
|
// #define INFO
|
||||||
|
|
||||||
int rcvBuffer(int sock_fd){
|
int rcvBuffer(int sock_fd)
|
||||||
int rcvBufferSize;
|
{
|
||||||
socklen_t sockOptSize = sizeof(rcvBufferSize);
|
int rcvBufferSize;
|
||||||
getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufferSize, &sockOptSize);
|
socklen_t sockOptSize = sizeof(rcvBufferSize);
|
||||||
|
getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufferSize, &sockOptSize);
|
||||||
#ifdef INFO
|
#ifdef INFO
|
||||||
printf("initial socket receive buf %d\n", rcvBufferSize);
|
printf("initial socket receive buf %d\n", rcvBufferSize);
|
||||||
#endif
|
#endif
|
||||||
return rcvBufferSize;
|
return rcvBufferSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
int recvCount(const void *s, int size){
|
int recvCount(const void *s, int size)
|
||||||
|
{
|
||||||
register int count;
|
register int count;
|
||||||
register int mark;
|
register int mark;
|
||||||
register char *p;
|
register char *p;
|
||||||
p=(char *)s;
|
p = (char *)s;
|
||||||
count = 0;
|
count = 0;
|
||||||
mark = 0;
|
mark = 0;
|
||||||
do {
|
do
|
||||||
if (*p && !(*(p+1))) count++;
|
{
|
||||||
|
if (*p && !(*(p + 1)))
|
||||||
|
count++;
|
||||||
#ifdef INFO
|
#ifdef INFO
|
||||||
printf("[%c][%X]\n",*p,*p);
|
printf("[%c][%X]\n", *p, *p);
|
||||||
#endif
|
#endif
|
||||||
p++;
|
p++;
|
||||||
mark++;
|
mark++;
|
||||||
} while (!((*p == '\0') && (*(p+1)=='\0')) && (mark < size));
|
} while (!((*p == '\0') && (*(p + 1) == '\0')) && (mark < size));
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,char *argv[]){
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
int client_sock, rc, len;
|
int client_sock, rc, len;
|
||||||
struct sockaddr_un server_sockaddr;
|
struct sockaddr_un server_sockaddr;
|
||||||
struct sockaddr_un client_sockaddr;
|
struct sockaddr_un client_sockaddr;
|
||||||
char obuf[BUF];
|
char obuf[BUF];
|
||||||
char **ibuf;
|
char **ibuf;
|
||||||
size_t obs=BUF,ibs;
|
size_t obs = BUF, ibs;
|
||||||
char cpath[22];
|
char cpath[22];
|
||||||
|
|
||||||
sprintf(cpath,"%d-%s",getpid(),CLIENT_PATH);
|
sprintf(cpath, "%d-%s", getpid(), CLIENT_PATH);
|
||||||
len = sizeof(client_sockaddr);
|
len = sizeof(client_sockaddr);
|
||||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (client_sock == -1) {
|
if (client_sock == -1)
|
||||||
perror("socket");
|
{
|
||||||
exit(1);
|
perror("socket");
|
||||||
}
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
client_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(client_sockaddr.sun_path, cpath);
|
||||||
|
|
||||||
client_sockaddr.sun_family = AF_UNIX;
|
unlink(cpath);
|
||||||
strcpy(client_sockaddr.sun_path, cpath);
|
rc = bind(client_sock, (struct sockaddr *)&client_sockaddr, len);
|
||||||
|
if (rc == -1)
|
||||||
|
SerrExit(client_sock, "bind");
|
||||||
|
|
||||||
unlink(cpath);
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
rc = bind(client_sock, (struct sockaddr *) &client_sockaddr, len);
|
strcpy(server_sockaddr.sun_path, SERVER_PATH);
|
||||||
if (rc == -1) SerrExit(client_sock,"bind");
|
rc = connect(client_sock, (struct sockaddr *)&server_sockaddr, len);
|
||||||
|
if (rc == -1)
|
||||||
|
SerrExit(client_sock, "connect");
|
||||||
|
|
||||||
server_sockaddr.sun_family = AF_UNIX;
|
ibs = rcvBuffer(client_sock);
|
||||||
strcpy(server_sockaddr.sun_path, SERVER_PATH);
|
if ((ibuf = (char **)malloc(ibs)) == NULL)
|
||||||
rc = connect(client_sock, (struct sockaddr *) &server_sockaddr, len);
|
errExit("malloc");
|
||||||
if (rc == -1) SerrExit(client_sock,"connect");
|
|
||||||
|
|
||||||
ibs=rcvBuffer(client_sock);
|
do
|
||||||
if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
{
|
||||||
|
|
||||||
do {
|
|
||||||
memset(obuf, 0, obs);
|
memset(obuf, 0, obs);
|
||||||
printf("> ");
|
printf("> ");
|
||||||
scanf("%s",obuf);
|
scanf("%s", obuf);
|
||||||
|
|
||||||
printf("Sending data...\n");
|
printf("Sending data...\n");
|
||||||
rc = send(client_sock, obuf, strlen(obuf)+1, 0);
|
rc = send(client_sock, obuf, strlen(obuf) + 1, 0);
|
||||||
if (rc == -1) {
|
if (rc == -1)
|
||||||
|
{
|
||||||
perror("send");
|
perror("send");
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
}
|
||||||
printf("Data sent!\n");
|
else
|
||||||
|
{
|
||||||
|
printf("Data sent!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Waiting to recieve data...\n");
|
printf("Waiting to recieve data...\n");
|
||||||
memset(ibuf, 0, ibs);
|
memset(ibuf, 0, ibs);
|
||||||
rc = recv(client_sock, ibuf, ibs,0);
|
rc = recv(client_sock, ibuf, ibs, 0);
|
||||||
if (rc == -1) {
|
if (rc == -1)
|
||||||
|
{
|
||||||
perror("recv");
|
perror("recv");
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
}
|
||||||
int c = recvCount(ibuf,ibs);
|
else
|
||||||
|
{
|
||||||
|
int c = recvCount(ibuf, ibs);
|
||||||
#ifdef INFO
|
#ifdef INFO
|
||||||
char *p = (char *)ibuf;
|
char *p = (char *)ibuf;
|
||||||
printf("count: %d resieved\n",c);
|
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++;
|
printf("[%c][%X]", *p, *p);
|
||||||
}
|
p++;
|
||||||
printf("\n");
|
}
|
||||||
|
printf("\n");
|
||||||
#endif
|
#endif
|
||||||
#ifndef INFO
|
#ifndef INFO
|
||||||
char p[obs];
|
char p[obs];
|
||||||
char *tp = (char *)ibuf;
|
char *tp = (char *)ibuf;
|
||||||
// if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
// 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++;}
|
memset(p, 0, obs);
|
||||||
tp++;
|
while (*tp)
|
||||||
// printf("DATA RECEIVED [%ld] = %s\n", strlen(ibuf[i]),ibuf[i]);
|
{
|
||||||
printf("DATA RECEIVED [%ld] = %s\n", strlen(p),p);
|
strncat(p, tp, 1);
|
||||||
// printf("DATA RECEIVED [%ld] = %s\n", strlen(p[i]),p[i]);
|
tp++;
|
||||||
}
|
}
|
||||||
|
tp++;
|
||||||
|
// printf("DATA RECEIVED [%ld] = %s\n", strlen(ibuf[i]),ibuf[i]);
|
||||||
|
printf("DATA RECEIVED [%ld] = %s\n", strlen(p), p);
|
||||||
|
// printf("DATA RECEIVED [%ld] = %s\n", strlen(p[i]),p[i]);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (strcmp(obuf,FINC) && strcmp(obuf,FINS));
|
} while (strcmp(obuf, FINC) && strcmp(obuf, FINS));
|
||||||
|
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
unlink(cpath);
|
unlink(cpath);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
+121
-91
@@ -11,66 +11,89 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/types.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 SOCK_PATH "/tmp/unix_sock.server"
|
||||||
#define DATA "Hello from server"
|
#define DATA "Hello from server"
|
||||||
#define BUF 128
|
#define BUF 128
|
||||||
#define FINS "exit_s"
|
#define FINS "exit_s"
|
||||||
#define FINC "exit_c"
|
#define FINC "exit_c"
|
||||||
|
|
||||||
void child_handler(int sig) {
|
void child_handler(int sig)
|
||||||
pid_t pid;
|
{
|
||||||
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
pid_t pid;
|
||||||
|
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];
|
int rc;
|
||||||
char outbuf[BUF];
|
char inbuf[BUF];
|
||||||
int bytes_rec = 0;
|
char outbuf[BUF];
|
||||||
size_t bs = BUF;
|
int bytes_rec = 0;
|
||||||
socklen_t len;
|
size_t bs = BUF;
|
||||||
pid_t ppid, pid;
|
socklen_t len;
|
||||||
|
pid_t ppid, pid;
|
||||||
|
|
||||||
ppid=getppid();
|
ppid = getppid();
|
||||||
pid=getpid();
|
pid = getpid();
|
||||||
|
|
||||||
len = sizeof(client_sockaddr);
|
len = sizeof(client_sockaddr);
|
||||||
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
|
rc = getpeername(client_sock, (struct sockaddr *)&client_sockaddr, &len);
|
||||||
if (rc == -1){
|
if (rc == -1)
|
||||||
|
{
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
errExit("getpeer");
|
errExit("getpeer");
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("[%d] Client socket filepath: %s\n", pid, client_sockaddr.sun_path);
|
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);
|
printf("waiting for %s...\n", client_sockaddr.sun_path);
|
||||||
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
memset(inbuf, 0, bs);
|
||||||
if (bytes_rec == -1){
|
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
||||||
close(client_sock);
|
if (bytes_rec == -1)
|
||||||
errExit("recv");
|
{
|
||||||
} else {
|
close(client_sock);
|
||||||
printf("[%s]: %s\n", client_sockaddr.sun_path, inbuf);
|
errExit("recv");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("[%s]: %s\n", client_sockaddr.sun_path, inbuf);
|
||||||
|
}
|
||||||
|
|
||||||
memset(outbuf, 0, bs);
|
memset(outbuf, 0, bs);
|
||||||
strcpy(outbuf, DATA);
|
strcpy(outbuf, DATA);
|
||||||
printf("Sending data...\n");
|
printf("Sending data...\n");
|
||||||
rc = send(client_sock, outbuf, strlen(outbuf)+1, 0);
|
rc = send(client_sock, outbuf, strlen(outbuf) + 1, 0);
|
||||||
if (rc == -1) {
|
if (rc == -1)
|
||||||
close(client_sock);
|
{
|
||||||
errExit("send");
|
close(client_sock);
|
||||||
} else {
|
errExit("send");
|
||||||
printf("Data sent!\n");
|
}
|
||||||
}
|
else
|
||||||
if (strcmp(inbuf,FINS) == 0 ) {kill(ppid,SIGTERM);break;}
|
{
|
||||||
} while (strcmp(inbuf,FINC));
|
printf("Data sent!\n");
|
||||||
|
}
|
||||||
|
if (strcmp(inbuf, FINS) == 0)
|
||||||
|
{
|
||||||
|
kill(ppid, SIGKILL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (strcmp(inbuf, FINC));
|
||||||
memset(outbuf, 0, bs);
|
memset(outbuf, 0, bs);
|
||||||
sprintf(outbuf,"Exit %d\n",pid);
|
sprintf(outbuf, "Exit %d\n", pid);
|
||||||
rc = send(client_sock, outbuf, strlen(outbuf)+1, 0);
|
rc = send(client_sock, outbuf, strlen(outbuf) + 1, 0);
|
||||||
if (rc == -1) {
|
if (rc == -1)
|
||||||
|
{
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
errExit("send");
|
errExit("send");
|
||||||
}
|
}
|
||||||
@@ -78,65 +101,72 @@ void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockadd
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
int server_sock, client_sock, rc;
|
int server_sock, client_sock, rc;
|
||||||
socklen_t len;
|
socklen_t len;
|
||||||
struct sockaddr_un server_sockaddr;
|
struct sockaddr_un server_sockaddr;
|
||||||
struct sockaddr_un client_sockaddr;
|
struct sockaddr_un client_sockaddr;
|
||||||
int backlog = 10;
|
int backlog = 10;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
struct sigaction new_act, old_act;
|
struct sigaction new_act, old_act;
|
||||||
sigemptyset (&new_act.sa_mask);
|
sigemptyset(&new_act.sa_mask);
|
||||||
new_act.sa_flags = SA_RESTART;
|
new_act.sa_flags = SA_RESTART;
|
||||||
new_act.sa_handler = child_handler;
|
new_act.sa_handler = child_handler;
|
||||||
sigaction (SIGCHLD, &new_act, &old_act);
|
sigaction(SIGCHLD, &new_act, &old_act);
|
||||||
|
|
||||||
len = sizeof(server_sockaddr);
|
len = sizeof(server_sockaddr);
|
||||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
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;
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
||||||
|
|
||||||
unlink(SOCK_PATH);
|
unlink(SOCK_PATH);
|
||||||
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
|
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){
|
|
||||||
close(server_sock);
|
|
||||||
errExit("listen");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("socket listening...\n");
|
|
||||||
|
|
||||||
do {
|
|
||||||
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
|
|
||||||
if (client_sock == -1){
|
|
||||||
close(server_sock);
|
close(server_sock);
|
||||||
close(client_sock);
|
errExit("bind");
|
||||||
errExit("accept");
|
|
||||||
}
|
}
|
||||||
if ((pid = fork()) == -1 ) {
|
|
||||||
|
rc = listen(server_sock, backlog);
|
||||||
|
if (rc == -1)
|
||||||
|
{
|
||||||
close(server_sock);
|
close(server_sock);
|
||||||
close(client_sock);
|
errExit("listen");
|
||||||
errExit("fork");
|
|
||||||
}
|
}
|
||||||
if (pid == 0) session(client_sock,server_sockaddr,client_sockaddr);
|
|
||||||
close(client_sock);
|
|
||||||
} while (1);
|
|
||||||
|
|
||||||
|
printf("socket listening...\n");
|
||||||
|
|
||||||
close(server_sock);
|
do
|
||||||
unlink(SOCK_PATH);
|
{
|
||||||
|
client_sock = accept(server_sock, (struct sockaddr *)&client_sockaddr, &len);
|
||||||
|
if (client_sock == -1)
|
||||||
|
{
|
||||||
|
close(server_sock);
|
||||||
|
close(client_sock);
|
||||||
|
errExit("accept");
|
||||||
|
}
|
||||||
|
if ((pid = fork()) == -1)
|
||||||
|
{
|
||||||
|
close(server_sock);
|
||||||
|
close(client_sock);
|
||||||
|
errExit("fork");
|
||||||
|
}
|
||||||
|
if (pid == 0)
|
||||||
|
session(client_sock, server_sockaddr, client_sockaddr);
|
||||||
|
close(client_sock);
|
||||||
|
} while (1);
|
||||||
|
|
||||||
return 0;
|
close(server_sock);
|
||||||
|
unlink(SOCK_PATH);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user