How to code:client/server using Unix domain socket/tcp on Ubuntu?

2,075 views
Skip to first unread message

Constantine Vasil

unread,
Oct 12, 2013, 3:10:16 PM10/12/13
to golan...@googlegroups.com
How to create unified client and server code working in:

1) same machine: Unix domain socket
2) different machines: tcp

--Constantine

 

al...@lx.lc

unread,
Oct 12, 2013, 3:56:19 PM10/12/13
to golan...@googlegroups.com
You're going to have to either ask more targeted questions about where you're stuck, and/or post a valiant effort I think before more people will be willing to help you on this list.  

There are tons of client/server examples out on the internet - just search for them (ex. golang tcp server client).

As a nudge(and, in case this shows up in a search result one day) , here's the documentation for the net package which includes a full tcp listener example - http://golang.org/pkg/net/

Here's some discussion AND code for unix sockets - http://stackoverflow.com/questions/2886719/unix-sockets-in-go

Good luck, and post back if you have any questions after you've given it a shot.

Regards,
Alex

Constantine Vasil

unread,
Oct 12, 2013, 4:39:25 PM10/12/13
to golan...@googlegroups.com
Alex,

Thank you for your help.

I have experience with tcp but don't have any with Unix domain sockets. 

--Constantine

Constantine Vasil

unread,
Oct 12, 2013, 5:40:39 PM10/12/13
to golan...@googlegroups.com
On the server:
ListenType := "unix"
ListenAddr := "/tmp/MyCall.sock"
ln, err := net.Listen(ListenType, ListenAddr)

On the client:
c, err := net.Dial(ListenType, address)

Got the message:
address already in use

Constantine Vasil

unread,
Oct 12, 2013, 6:01:32 PM10/12/13
to golan...@googlegroups.com
Alex,

I gave it a shot and as I suspected it is not working because the link is too old.
Here's some discussion AND code for unix sockets - http://stackoverflow.com/questions/2886719/unix-sockets-in-go
is from Jan 21 '11 at 13:21

this does not compile:
c, err := net.Dial(ListenType, "", address)
error: too many arguments

--Constantine

al...@lx.lc

unread,
Oct 12, 2013, 6:11:05 PM10/12/13
to golan...@googlegroups.com
Just remove the middle argument.   There are a few other nuances - err.String() should be err.Error(), but otherwise, the code in the first answer there works fine.

Regards,
Alex

Constantine Vasil

unread,
Oct 12, 2013, 6:14:20 PM10/12/13
to golan...@googlegroups.com
Got the message:
address already in use

al...@lx.lc

unread,
Oct 12, 2013, 6:19:22 PM10/12/13
to golan...@googlegroups.com
Because the file exists.  Remove the file manually, then make your server clean it up properly upon exit.

Thanks,
Alex

DisposaBoy

unread,
Oct 14, 2013, 9:12:06 AM10/14/13
to golan...@googlegroups.com
just put an at sign in front of the.path. e.g. @/tmp/... and or will be automatically cleaned up for you

Constantine Vasil

unread,
Oct 14, 2013, 11:07:18 AM10/14/13
to golan...@googlegroups.com
The file was created automatically
Reply all
Reply to author
Forward
0 new messages