niedziela, 30 kwietnia 2017

Use the socket (gcc)

#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

char *path;
char *text;
int length_text;
int my_socket;
struct sockaddr_un name_value;

void init_Socket(char *p, char *t)
{
    path=p;
    text=t;
    my_socket=socket(PF_LOCAL,SOCK_STREAM,0);
    name_value.sun_family=AF_LOCAL;
    length_text=strlen(text)+1;
    strcpy(name_value.sun_path,path);
}
void write_Socket()
{
    connect(my_socket,&name_value,SUN_LEN(&name_value));
    write(my_socket,&length_text,sizeof(length_text));
    write(my_socket,text,length_text);
    close(my_socket);
}


int main(int argc, char **argv)
{
    char *path="/home/tmp/text";
    char text[128];
    printf("Write text - ");
    scanf("%s",text);
    init_Socket(path,text);
    write_Socket();
    return 0;
}

Brak komentarzy:

Prześlij komentarz