0

I need to store sockets into database, gen_tcp:accept returns something like

#Port<0.5>

I can convert it to a bitstring using io_lib:format

["Port<0.5>"]

to store it into database but how do i convert it back to its original state to use it to send and receive data?

Update: I tried term_to_binary but mysql-otp failed to insert it so i did binary_to_list(term_to_binary(Socket)) , it inserted fine but adds extra <<194,xxx,xx...>> at the start of the binary into the database, any solutions? Why is it happening? If it doesn't add extra data i can convert it back fine using binary_to_term.

asim
  • 533
  • 6
  • 17
  • Hello, welcome to Erlang! I think you are going against the flow. Wanting to store a socket handle into a db seems a "design smell". Embrace Erlang, instead of trying to adapt Erlang to how you would do something in another language :-) – marco.m Jan 07 '19 at 11:14
  • Its a chat server and i need to store online users sockets, storing big list of sockets in a variable isn't a good idea, is it? I was concerned about it but unfortunately asking these questions on stack overflow results in down votes for being too broad and ban from posting future questions. – asim Jan 07 '19 at 12:45
  • Is dictionary gonna handle thousands of sockets fine enough? – asim Jan 07 '19 at 12:49
  • I see the concern about using a list, sure, it would not be a good approach in this case. You could use for example ETS, which is built-in and doesn't need any conversion. But for this kind of questions, I suggest asking on the Erlang mailing list or Slack channel. – marco.m Jan 07 '19 at 16:23
  • Thanks for your valuable time and suggestions i really appreciate. – asim Jan 07 '19 at 16:52

1 Answers1

2

I changed varchar to varbinary and now i can directly insert term_to_binary into database and then reuse it using binary_to_term.

asim
  • 533
  • 6
  • 17