mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
added sock20
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#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>
|
||||
#include <time.h>
|
||||
|
||||
#define SERVER_PATH "/tmp/unix_sock.server"
|
||||
#define CLIENT_PATH "unix_sock.client"
|
||||
#define DATA "Hello from client"
|
||||
#define BUF 128
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
|
||||
int client_sock, rc, len;
|
||||
struct sockaddr_un server_sockaddr;
|
||||
struct sockaddr_un client_sockaddr;
|
||||
char buf[BUF];
|
||||
size_t bs=BUF;
|
||||
char cpath[22];
|
||||
|
||||
srand(time(NULL));
|
||||
sprintf(cpath,"%03d-%s",rand()%1000,CLIENT_PATH);
|
||||
len = sizeof(client_sockaddr);
|
||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
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){
|
||||
perror("bind");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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){
|
||||
perror("connect");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (argc > 1)
|
||||
strcpy(buf, argv[1]);
|
||||
else
|
||||
strcpy(buf, DATA);
|
||||
|
||||
printf("Sending data...\n");
|
||||
rc = send(client_sock, buf, strlen(buf), 0);
|
||||
if (rc == -1) {
|
||||
perror("send");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
printf("Data sent!\n");
|
||||
}
|
||||
|
||||
printf("Waiting to recieve data...\n");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
rc = recv(client_sock, buf, bs,0);
|
||||
if (rc == -1) {
|
||||
perror("recv");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
printf("DATA RECEIVED = %s\n", buf);
|
||||
}
|
||||
|
||||
close(client_sock);
|
||||
unlink(cpath);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
#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>
|
||||
#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 SERVER_PATH "/tmp/unix_sock.server"
|
||||
#define CLIENT_PATH "unix_sock.client"
|
||||
#define DATA "Hello from client"
|
||||
#define BUF 128
|
||||
#define FINS "exit_s"
|
||||
#define FINC "exit_c"
|
||||
//#define INFO
|
||||
|
||||
int rcvBuffer(int sock_fd){
|
||||
int rcvBufferSize;
|
||||
socklen_t sockOptSize = sizeof(rcvBufferSize);
|
||||
getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufferSize, &sockOptSize);
|
||||
#ifdef INFO
|
||||
printf("initial socket receive buf %d\n", rcvBufferSize);
|
||||
#endif
|
||||
return rcvBufferSize;
|
||||
}
|
||||
|
||||
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++;
|
||||
#ifdef INFO
|
||||
printf("[%c][%X]\n",*p,*p);
|
||||
#endif
|
||||
p++;
|
||||
mark++;
|
||||
} while (!((*p == '\0') && (*(p+1)=='\0')) && (mark < size));
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
|
||||
int client_sock, rc, len;
|
||||
struct sockaddr_un server_sockaddr;
|
||||
struct sockaddr_un client_sockaddr;
|
||||
char obuf[BUF];
|
||||
char **ibuf;
|
||||
size_t obs=BUF,ibs;
|
||||
char cpath[22];
|
||||
|
||||
sprintf(cpath,"%d-%s",getpid(),CLIENT_PATH);
|
||||
len = sizeof(client_sockaddr);
|
||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
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");
|
||||
|
||||
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");
|
||||
|
||||
ibs=rcvBuffer(client_sock);
|
||||
if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||
|
||||
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) {
|
||||
perror("send");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
} 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) {
|
||||
perror("recv");
|
||||
close(client_sock);
|
||||
exit(1);
|
||||
} 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++){
|
||||
printf("[%c][%X]",*p,*p);
|
||||
p++;
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
#ifndef INFO
|
||||
char p[obs];
|
||||
char *tp = (char *)ibuf;
|
||||
// if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||
for (int i = 0; i < c; i++){
|
||||
memset(p,0,obs);
|
||||
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);
|
||||
// printf("DATA RECEIVED [%ld] = %s\n", strlen(p[i]),p[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} while (strcmp(obuf,FINC) && strcmp(obuf,FINS));
|
||||
|
||||
close(client_sock);
|
||||
unlink(cpath);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#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 errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
#define SOCK_PATH "/tmp/unix_sock.server"
|
||||
#define DATA "Hello from server"
|
||||
#define BUF 128
|
||||
#define FIN "exit"
|
||||
|
||||
int session(int client_sock, struct sockaddr_un server_sockaddr, struct sockaddr_un client_sockaddr){
|
||||
int rc;
|
||||
char inbuf[BUF];
|
||||
char outbuf[BUF];
|
||||
int bytes_rec = 0;
|
||||
size_t bs = BUF;
|
||||
socklen_t len;
|
||||
len = sizeof(client_sockaddr);
|
||||
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||
if (rc == -1){
|
||||
close(client_sock);
|
||||
errExit("getpeer");
|
||||
} else {
|
||||
printf("Client socket filepath: %s\n", client_sockaddr.sun_path);
|
||||
}
|
||||
|
||||
printf("waiting to read...\n");
|
||||
memset(inbuf, 0, bs);
|
||||
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
||||
if (bytes_rec == -1){
|
||||
close(client_sock);
|
||||
errExit("recv");
|
||||
} else {
|
||||
printf("DATA RECEIVED = %s\n", inbuf);
|
||||
}
|
||||
|
||||
memset(outbuf, 0, bs);
|
||||
strcpy(outbuf, DATA);
|
||||
printf("Sending data...\n");
|
||||
rc = send(client_sock, outbuf, strlen(outbuf), 0);
|
||||
if (rc == -1) {
|
||||
close(client_sock);
|
||||
errExit("send");
|
||||
} else {
|
||||
printf("Data sent!\n");
|
||||
}
|
||||
close(client_sock);
|
||||
return strcmp(inbuf,FIN);
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
int server_sock, client_sock, rc;
|
||||
socklen_t len;
|
||||
struct sockaddr_un server_sockaddr;
|
||||
struct sockaddr_un client_sockaddr;
|
||||
int backlog = 10;
|
||||
|
||||
len = sizeof(server_sockaddr);
|
||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
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){
|
||||
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(client_sock);
|
||||
errExit("accept");
|
||||
}
|
||||
} while (session(client_sock,server_sockaddr,client_sockaddr));
|
||||
|
||||
|
||||
close(server_sock);
|
||||
unlink(SOCK_PATH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
#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>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
#define SOCK_PATH "/tmp/unix_sock.server"
|
||||
#define DATA "Hello from server"
|
||||
#define BUF 128
|
||||
#define FINS "exit_s"
|
||||
#define FINC "exit_c"
|
||||
|
||||
void child_handler(int sig) {
|
||||
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){
|
||||
int rc;
|
||||
char inbuf[BUF];
|
||||
char outbuf[BUF];
|
||||
int bytes_rec = 0;
|
||||
size_t bs = BUF;
|
||||
socklen_t len;
|
||||
pid_t ppid, pid;
|
||||
|
||||
ppid=getppid();
|
||||
pid=getpid();
|
||||
|
||||
len = sizeof(client_sockaddr);
|
||||
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||
if (rc == -1){
|
||||
close(client_sock);
|
||||
errExit("getpeer");
|
||||
} else {
|
||||
printf("[%d] Client socket filepath: %s\n", pid, client_sockaddr.sun_path);
|
||||
}
|
||||
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){
|
||||
close(client_sock);
|
||||
errExit("recv");
|
||||
} else {
|
||||
printf("[%s]: %s\n", client_sockaddr.sun_path, inbuf);
|
||||
}
|
||||
|
||||
memset(outbuf, 0, bs);
|
||||
strcpy(outbuf, DATA);
|
||||
printf("Sending data...\n");
|
||||
rc = send(client_sock, outbuf, strlen(outbuf)+1, 0);
|
||||
if (rc == -1) {
|
||||
close(client_sock);
|
||||
errExit("send");
|
||||
} else {
|
||||
printf("Data sent!\n");
|
||||
}
|
||||
if (strcmp(inbuf,FINS) == 0 ) {kill(ppid,SIGTERM);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) {
|
||||
close(client_sock);
|
||||
errExit("send");
|
||||
}
|
||||
close(client_sock);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(void){
|
||||
|
||||
int server_sock, client_sock, rc;
|
||||
socklen_t len;
|
||||
struct sockaddr_un server_sockaddr;
|
||||
struct sockaddr_un client_sockaddr;
|
||||
int backlog = 10;
|
||||
pid_t pid;
|
||||
|
||||
struct sigaction new_act, old_act;
|
||||
sigemptyset (&new_act.sa_mask);
|
||||
new_act.sa_flags = SA_RESTART;
|
||||
new_act.sa_handler = child_handler;
|
||||
sigaction (SIGCHLD, &new_act, &old_act);
|
||||
|
||||
len = sizeof(server_sockaddr);
|
||||
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
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){
|
||||
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(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);
|
||||
|
||||
|
||||
close(server_sock);
|
||||
unlink(SOCK_PATH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user