mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
new pull
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -10,17 +10,19 @@
|
||||
|
||||
#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));
|
||||
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){
|
||||
if (server_sock == -1)
|
||||
{
|
||||
perror("socket");
|
||||
exit(1);
|
||||
}
|
||||
@@ -29,28 +31,28 @@ int main(void){
|
||||
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){
|
||||
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){
|
||||
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);
|
||||
else
|
||||
{
|
||||
printf("DATA RECEIVED = %s\n", buf);
|
||||
}
|
||||
|
||||
|
||||
close(server_sock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
#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>
|
||||
#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 main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
socklen_t length;
|
||||
struct sockaddr_in server;
|
||||
@@ -17,12 +18,13 @@ int main(int argc, char *argv[]){
|
||||
char buf[1024];
|
||||
int rval;
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(sock == -1){
|
||||
if (sock == -1)
|
||||
{
|
||||
perror("opening stream socket");
|
||||
exit(1);
|
||||
}
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = htoml(INADDR_ANY;
|
||||
server.sin_addr.s_addr = htoml(INADDR_ANY);
|
||||
if(argc == 2){
|
||||
server.sin_port = htons(atoi(argv[1]));
|
||||
}
|
||||
@@ -41,23 +43,27 @@ int main(int argc, char *argv[]){
|
||||
printf("socket port %d\n", ntohs(server.sin_port));
|
||||
listen(sock, 5);
|
||||
do{
|
||||
if((msgsock = accept(sock,(struct sockaddr *) NULL, (socklen_t *) NULL)) == -1){
|
||||
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)
|
||||
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){
|
||||
if (rval == 0)
|
||||
{
|
||||
printf("connecting finished\n");
|
||||
}
|
||||
else{
|
||||
dprintf(1,"[%s]\n", buf);
|
||||
else
|
||||
{
|
||||
dprintf(1, "[%s]\n", buf);
|
||||
}
|
||||
}while((rval > 0) && (strcmp(buf,FIN)));
|
||||
close(msgsock);
|
||||
} while ((rval > 0) && (strcmp(buf, FIN)));
|
||||
close(msgsock);
|
||||
|
||||
}while(strcmp(buf,FIN));
|
||||
exit(0);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#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,62 @@
|
||||
#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.
Reference in New Issue
Block a user