Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ Server & Tcl client

201 views
Skip to first unread message

m.j...@charter.net

unread,
Jul 11, 2008, 4:23:57 PM7/11/08
to c...@jlab.org
I'm trying to write a simple tcl client to talk to a simple C++ server
using tcl's socket command. Can this be done? I've seen lots of
examples of both client and server being written in tcl, and it looks
straight forward, but when I try to talk to the C++ server...

#The C++ server "sees" the connection, and the client can "see" when
the server goes away, but the communication in between isn't working

I'm using tcl8.4 on rhel-4-0-ia32

> set socket [socket "129.57.228.105" 32500]
sock5
> puts $socket "LISTEN" (#no errors, but server never sees this)
> flush $socket
> gets $socket line (#never returns from "gets")

Is there something special needed in the C++ server side that may be
missing?
Any help would be truly appreciated.
Michele

Óscar Fuentes

unread,
Jul 11, 2008, 4:43:08 PM7/11/08
to
m.j...@charter.net writes:

[snip]

> Is there something special needed in the C++ server side that may be
> missing?

C++ has no a "standard" socket library. There are lots of C++ (and C)
libraries for sockets (plus raw socket manipulation). Once the data is
in the socket, it is just data, it has no relation with the programming
language used for putting it on the wire.

You need to check the *exact* requirements of your C++ server (data
formats, protocols, etc). For instance: is "LISTEN" supposed to be
followed by a newline character? Tcl's `puts' does that. Try:

puts -nonewline $socket"LISTEN"

If your C++ server-side code is simple, post it here, and I'll look at
it.

HTH.

--
Oscar

Gerald W. Lester

unread,
Jul 11, 2008, 4:51:36 PM7/11/08
to
m.j...@charter.net wrote:
> I'm trying to write a simple tcl client to talk to a simple C++ server
> using tcl's socket command. Can this be done?

Yes. TclLib has many examples of this (smtp, http, ftp, ntp, nntp, ...).

> I've seen lots of
> examples of both client and server being written in tcl, and it looks
> straight forward, but when I try to talk to the C++ server...
>
> #The C++ server "sees" the connection, and the client can "see" when
> the server goes away, but the communication in between isn't working
>
> I'm using tcl8.4 on rhel-4-0-ia32
>
>> set socket [socket "129.57.228.105" 32500]
> sock5
>> puts $socket "LISTEN" (#no errors, but server never sees this)
>> flush $socket
>> gets $socket line (#never returns from "gets")
>
> Is there something special needed in the C++ server side that may be
> missing?
> Any help would be truly appreciated.

Well the C++ side is much harder then the Tcl side. Do you have experience
with writing C++ server code?

You may want to post your C++ code here.


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

dave.j...@googlemail.com

unread,
Jul 11, 2008, 6:46:55 PM7/11/08
to

I once worked with a MS windows programmer trying to get client server
working, and it was much simpler on Unix.

Things that worked for us:
Stick to an ASCII protocol
Write an alternate simple server in Tcl which will eventually be
replaced
When in doubt, test the server by using telnet systemname portnumber
from the client side

Almost all the problems were caused by the libraries trying to emulate
sockets, and in the end we decided to do the sensible thing, and run
the Unix machines as servers (which was back to front as far as the
application design was concerned).

Dave

Óscar Fuentes

unread,
Jul 11, 2008, 8:33:54 PM7/11/08
to
"dave.j...@googlemail.com" <dave.j...@googlemail.com> writes:

> I once worked with a MS windows programmer trying to get client server
> working, and it was much simpler on Unix.
>
> Things that worked for us:
> Stick to an ASCII protocol
> Write an alternate simple server in Tcl which will eventually be
> replaced
> When in doubt, test the server by using telnet systemname portnumber
> from the client side

You can do exactly this on Windows too. Probably, the Tcl code you wrote
for Unix will work on Windows with no changes. (Which, of course, is a
merit of Tcl. But I can't guess why you think that ASCII protocols are
more difficult to implement on Windows)

> Almost all the problems were caused by the libraries trying to emulate
> sockets,

The Windows Socket API is a bit different from Unix, but it is sockets
all the way.

[snip]

--
Oscar

dave.j...@googlemail.com

unread,
Jul 12, 2008, 5:35:02 AM7/12/08
to
On Jul 12, 1:33 am, Óscar Fuentes <o...@wanadoo.es> wrote:

> "dave.joub...@googlemail.com" <dave.joub...@googlemail.com> writes:
> > I once worked with a MS windows programmer trying to get client server
> > working, and it was much simpler on Unix.
>
> > Things that worked for us:
> > Stick to an ASCII protocol
> > Write an alternate simple server in Tcl which will eventually be
> > replaced
> > When in doubt, test the server by using telnet systemname portnumber
> > from the client side
>
> You can do exactly this on Windows too. Probably, the Tcl code you wrote
> for Unix will work on Windows with no changes. (Which, of course, is a
> merit of Tcl.

I totally agree.

> But I can't guess why you think that ASCII protocols are
> more difficult to implement on Windows)

No, I am not saying that they are more difficult; I am saying that
even if they were more difficult, the choice should favour ASCII.

>
> > Almost all the problems were caused by the libraries trying to emulate
> > sockets,
>
> The Windows Socket API is a bit different from Unix, but it is sockets
> all the way.

They may be now. This was at a time (early 90's) when MS windows had
just discovered networking, and the methodologies out of 'Stevens -
Unix network programming' were useless under Windows. I am hoping that
for the original poster's sake they better now. Inherent issues in the
OS made life 'interesting'.

However, from a personal point of view, the experience was beneficial
since I found out the easy way that I would never want to switch from
*nix to MS windows, and that C would never be my preferred language,
let alone C++.

Dave

Uwe Klein

unread,
Jul 12, 2008, 5:50:03 AM7/12/08
to
dave.j...@googlemail.com wrote:

>>But I can't guess why you think that ASCII protocols are
>>more difficult to implement on Windows)
>
>
> No, I am not saying that they are more difficult; I am saying that
> even if they were more difficult, the choice should favour ASCII.

I have just converted an embedded app that transfered binary structures
for talking to with no version information to plain text ascii.
Added basic introspection to the protocol too.( basically a restricted
jim interpreter behind a socket )

Testability and robustness are greatly improved.

Donal K. Fellows

unread,
Jul 12, 2008, 9:52:00 AM7/12/08
to
Óscar Fuentes wrote:
> You can do exactly this on Windows too. Probably, the Tcl code you wrote
> for Unix will work on Windows with no changes. (Which, of course, is a
> merit of Tcl. But I can't guess why you think that ASCII protocols are
> more difficult to implement on Windows)

In general, on all platforms, binary protocols are a bit harder to
implement. OK, maybe not to do the first draft, but text protocols are
far easier than binary ones to debug. (A protocol's not implemented in
my book until it's done right.) The big advantage of a binary protocol
is that it is easier to make it go fast, but that doesn't make it any
easier to correctly implement.

Tcl's networking code is good though. From the scripting level, it
works *the same* on all platforms, which is great! (Well, so long as
you're not asking to create low-numbered server sockets.)

Donal.

sriniva...@gmail.com

unread,
Aug 23, 2016, 10:36:10 AM8/23/16
to
Hi Michele,

I am trying to solve the same problem, i.e., wanted to have a C++ server and tcl clients in linux. Were you able to setup your server and clients successfully?
An example would be great.

Thanks,
boppu

Gerald W. Lester

unread,
Aug 23, 2016, 1:18:02 PM8/23/16
to
You do realize the OP was 8 years ago -- so they may not be listening any more.

I've had Tcl Clients talk to Java/C++/C# servers for over 20 years (see old
conference papers).

What *exact* problems are you having?

Have you done any network programming before?

Is there a particular protocol you are using?


--
+------------------------------------------------------------------------+

sriniva...@gmail.com

unread,
Aug 24, 2016, 7:37:01 AM8/24/16
to
On Tuesday, August 23, 2016 at 7:18:02 PM UTC+2, Gerald W. Lester wrote:
> On 8/23/16 9:36 AM, sriniva...@gmail.com wrote:
> > On Friday, July 11, 2008 at 10:23:57 PM UTC+2, m.j...@charter.net wrote:
> >> I'm trying to write a simple tcl client to talk to a simple C++ server
> >> using tcl's socket command. Can this be done? I've seen lots of
> >> examples of both client and server being written in tcl, and it looks
> >> straight forward, but when I try to talk to the C++ server...
> >>
> >> #The C++ server "sees" the connection, and the client can "see" when
> >> the server goes away, but the communication in between isn't working
> >>
> >> I'm using tcl8.4 on rhel-4-0-ia32
> >>
> >>> set socket [socket "129.57.228.105" 32500]
> >> sock5
> >>> puts $socket "LISTEN" (#no errors, but server never sees this)
> >>> flush $socket
> >>> gets $socket line (#never returns from "gets")
> >>
> >> Is there something special needed in the C++ server side that may be
> >> missing?
> >> Any help would be truly appreciated.
> >> Michele
> >
> > Hi Michele,
> >
> > I am trying to solve the same problem, i.e., wanted to have a C++ server and tcl clients in linux.
> >Were you able to setup your server and clients successfully?
> > An example would be great.
Hi,


Thanks for your reply.

I was able to connect to the C++ server. At least the servrer acknowledges the connection. On the client side, I am able to write something to sever. For instance:

set chan [socket 10.162.0.203 2000]
puts -nonewline $chan "hello *"
flush $chan

after these commands, I am able to get the message on the server side but with some additional text.

Now, when I write something from the server, for instance:

puts "[get $chan]"
6@6µ{ÍzÀ\¡ü^À\¡ü"▒ÍzÚ0E6"[Â▒"Â\¡üm&Íz6@6ðÀ\¡üÁ\¡üm&ÍzÁ\¡ü"▒ÍzíéC+@6°Íz§­-z¸&6Â\¡üH<6ø6Àb'ÍzX¸{Íz`Â\¡üÂ\¡üµ{Íz°Ízb6pÍzð&ÍzÔ@X6(@àb6àÂ\¡Ã\¡üb6pÍzíéC+

I get something garbage. I am guessing it is something to do with encoding but I am not sure what exactly is the problem.


Coming to your specific questions, it is the first time I am doing network programming. It would be great, if you could point me to the papers that you are referring to.

Thanks,
Srinivas

Joerg Honerla

unread,
Aug 24, 2016, 10:38:09 AM8/24/16
to
Hi Srinivas,

I also have written a lot of server/clients application with tcltk and
C. In general it is normmaly simple and save. Your problem seems that
there is anything in the IO buffers.

My code at the C server side is

while (1) {
bzero(buffer, 256);
nchar = read(newsockfd, buffer, 255);
if (nchar < 0) error("ERROR reading from socket");
handle_command(buffer,line);
nchar = write(newsockfd,line,strlen(line));
}

Joerg

Joerg Honerla
High Voltage Laboratory
University Duisburg Essen
Germany

> Coming to your specific questions, it is the first time I am doing network programming. It would be great, if you could point me to the papers that you are referring to.
>
> Thanks,
> Srinivas
>>
>
>
>> You do realize the OP was 8 years ago -- so they may not be listening any more.
>>
>> I've had Tcl Clients talk to Java/C++/C# servers for over 20 years (see old
>> conference papers).
>>
>> What *exact* problems are you having?
>>
>> Have you done any network programming before?
>>
>> Is there a particular protocol you are using?
>>
>>
>> --
>> +------------------------------------------------------------------------+
>> | Gerald W. Lester |
>> |"The man who fights for his ideals is the man who is alive." - Cervantes|
>> +------------------------------------------------------------------------+


--
Joerg Honerla
High Voltage Laboratory
University Duisburg-Essen
Germany
http://www.ets.uni-due.de

Gerald W. Lester

unread,
Aug 24, 2016, 7:11:46 PM8/24/16
to
What additional text do you get?

> Now, when I write something from the server, for instance:
>
> puts "[get $chan]"
> 6@6µ{ÍzÀ\¡ü^À\¡ü"▒ÍzÚ0E6"[Â▒"Â\¡üm&Íz6@6ðÀ\¡üÁ\¡üm&ÍzÁ\¡ü"▒ÍzíéC+@6°Íz§­-z¸&6Â\¡üH<6ø6Àb'ÍzX¸{Íz`Â\¡üÂ\¡üµ{Íz°Ízb6pÍzð&ÍzÔ@X6(@àb6àÂ\¡Ã\¡üb6pÍzíéC+

What did you write out?

Can you do the following directly before the current puts?
puts [fconfigure $chan]

> I get something garbage. I am guessing it is something to do with encoding but I am not sure what exactly is the problem.

Here is one of the papers:
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0ahUKEwjp4IXTldvOAhVH5WMKHYJeDsQQFggpMAI&url=https%3A%2F%2Fwww.usenix.org%2Fconference%2F5th-annual-tcltk-workshop-1997%2Fredesigning-tcl-dp&usg=AFQjCNGnzNAt-hpo_ESS3dEkp-RSYTus9g&sig2=WPGv1RsqOMc1eZNStBCiHA&bvm=bv.130731782,d.cGc

sriniva...@gmail.com

unread,
Aug 29, 2016, 8:41:07 AM8/29/16
to
Hi Gerald,

Finally, it worked. I used the following:

set sid [socket irc.quakenet.org 6667]
fconfigure $sid -buffering line
fileevent $sid readable "readdata $sid"


There is an example in the following link:

http://tclhelp.net/unb/195

Thanks a lot for your help.

0 new messages