How to use UNIX sockets

That's all, thanks........
Or maybe also the value returned from one main to another in one crate

How to pass instance of struct in main from lib

I don't understand what you're asking here, could you give some more detail about what you're trying to do?

Or maybe also the value returned from one main to another in one crate

You can't have more than one main function in a crate. Anyway, if you want to pass Rust values around, you need to use regular, non-main functions. main is special: its job is to interact with the outside, non-Rust world. You'd never write this in Rust code:

let my_val = main();

I have struct of some type, want to initialise it, push some value and transfer to main(why- not matter)
No, i mean not main function, task- main.rs one and other

Like this?

struct MyStruct(String);

fn main() {
    let my_val = MyStruct(String::from("foo"));
    // do something with `my_val`
}

Yes, from lib.rs to main then transfer

If you have a type defined in lib.rs, you can use it in main.rs like this:

// in lib.rs

struct MyStruct(String);

// in main.rs

fn main() {
    let my_val = crate::MyStruct(String::from("foo"));
}
1 Like

Maybe i can use from lib functions, but I want for example do some calculations in one main and then in another
Struct instance that i need to use in all programs in crate, in c++ i see using sockets do smth like

Are you talking about having multiple threads launched from your main function?

Without providing code, there will be lots of back and forth as people guess what you mean. Please just make it simpler for everyone and give us something to work with.

1 Like

Don't remember where i saw.

//main.c #include <sys/types.h>
 #include <sys/socket.h> 
#include <sys/un.h> 
#include <stdio.h> 
#include <stdlib.h>
 int main() { 
int sock;
 struct sockaddr_un addr; 
int numbers[] = {1,2,3,4};
 //array to send _ массив для отправки
 if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) //creating a socket _ создание сокета 
{ perror("socket"); exit(1); } 
addr.sun_family = AF_UNIX;
 //domain _ домен
 strcpy(addr.sun_path, "mysocket"); 
//socket file _ файл сокета 
sendto(sock, numbers, sizeof(numbers), 0, (struct sockaddr *)&addr, sizeof(addr)); //we send 1 data packet (array) _ oтправляем 1 пакет данных (массив) 
close(sock); return 0; }
//UNIX sockets can be used to transfer data between programs on computers. 
//To use this interface , data transfer
//you will need only 3 libraries from the C language:
// sys/socket. h, sys/types.h and sys/un.h.
_
//Для передачи данных между программами на 
//ЭВМ можно использовать UNIX-сокеты. 
//Для использования подобного интерфейса //передачи данных 
//потребуются всего 3 библиотеки из языка Си: 
//sys/socket.h, sys/types.h и sys/un.h.

//Server side:
//Серверная часть:

//main2.c #include <sys/types.h> 
#include <sys/socket.h> 
#include <sys/un.h>
 #include <stdio.h>
 #include <stdlib.h> 
#define LEN_ARR 16 int main() { 
int sock;
struct sockaddr_un addr;//стуктура с адресом 
int *numbers = (int*) malloc(sizeof(int)*LEN_ARR);
//array for reading data _ массив для чтения данных
 int bytes_read; 
//number of data read _ количество считанных данных
 if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) //creating a local socket _ создание локального сокета 
{ perror("socket"); 
exit(1); } 
addr.sun_family = AF_UNIX; //домен 
strcpy(addr.sun_path, "mysocket"); //copying the socket file name _ копирование названия файла сокета

 if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) // { perror("bind"); exit(2); }
 bytes_read = recvfrom(sock, numbers, LEN_ARR, 0, 0, NULL); //т.к. принимаем 1 пакет данных, то используем recvfrom for(int i = 0; i< bytes_read; i++) printf("%d ", numbers[i]); //выводим результат передачи
 printf("\n"); close(sock); //interrupting the transmission _ прерываем передачу 
system("rm mysocket"); //deleting a socket file _ удаление файла сокета 
free(numbers); //freeing up memory _ освобождаем память 
return 0; }

That's not even Rust?

That's question- on rust I don't know)

Please show us what you have in Rust. It doesn't have to work, of course. Right now it sounds like you're asking about a problem that you haven't even run into, whatever that might be.

Thanks, but as i said- type of struct doesn't matter
UNIX sockets can be used to transfer data between programs on computers.
To use this interface data transfer
you will need only 3 libraries from the C language:
sys/socket. h, sys/types.h and sys/un.h.
Said comments above on Russian

Perhaps you're asking how you can have multiple executables (i.e. main functions) within one Cargo project? The way to do that is to have each executable program defined in a different source file under src/bin/, like this:

src/
- lib.rs
- bin/
  - first_program.rs
  - second_program.rs

Then first_program.rs and second_program.rs should each define a main function. They can both import definitions from lib.rs. You can run the compiled programs like this:

$ cargo run --bin first_program
$ cargo run --bin second_program

You can work with sockets in Rust using the std::net module.

1 Like

I don't read Russian. This is a forum for the Rust programming language. I've flagged this thread as off-topic as it's not even clear what you're asking, and it doesn't seem like you've even tried to do anything in Rust. A basic level of effort is appreciated before posting.

It's interesting, thanks, but that's not as code above on c can do)
I ask because on rust I didn't see this yet

I can't read the comments there, but this code appears to be an example of Unix-domain sockets. In Rust, that functionality is provided by the std::os::unix::net module.

Note: I changed the title of this thread to reflect this.

2 Likes