# Ruby and C network interaction

**URL:** <https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120>\
**Category:** Ruby\
**Created:** [November 30, 2005, 4:53am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120 "2005-11-30T04:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cilphex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cilphex](https://www.ruby-forum.com/u/cilphex)\
**Post date:** [November 30, 2005, 4:53am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120/1 "2005-11-30T04:53:28Z")

</div>

Hi,

I’m working on a small client/server application in my spare time,  
using C and win32 for the client and Ruby for the server. I’m having  
an interaction issue and was wondering if anyone could help. I’m not  
an expert in Ruby or networking :\

Anyway, on the client side I set up a socket and connect to the server,  
then send a username with:

send (socket, myMessage, sizeof (myMessage))

Which works fine. The Ruby server receives and prints it with:

username = session.recv(20).strip  
puts “Username: ‘#{username}’”

(session is an instance of TCPServer)

A password is sent the same way. My trouble occurs when I try to send  
data back to the client from the server.

The client tries to receive with:

recv (socket, sockMsg, sizeof(sockMsg), 0);

And the server sends with:

sent = session.send(“success”, 0)  
puts “#{sent} characters sent”

The server outputs “7 characters sent”, but I don’t receive anything on  
the client side. I output the contents of sockMsg, and it’s empty.

Server code is here (It’s not much, just something simple):  
[http://www.craighammell.com/code/server.rb](http://www.craighammell.com/code/server.rb)

Any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![cilphex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cilphex](https://www.ruby-forum.com/u/cilphex)\
**Post date:** [November 30, 2005, 5:13am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120/2 "2005-11-30T05:13:37Z")

</div>

On Wed, 30 Nov 2005, cilphex wrote:

> send (socket, myMessage, sizeof (myMessage))
> 
> the client side. I output the contents of sockMsg, and it’s empty.
> 
> Server code is here (It’s not much, just something simple):  
> [http://www.craighammell.com/code/server.rb](http://www.craighammell.com/code/server.rb)
> 
> Any help would be appreciated.

buffering.

try

socket.sync = true

cheers.

-a

---

<div class="post-metadata">

**Author:** ![cilphex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cilphex](https://www.ruby-forum.com/u/cilphex)\
**Post date:** [November 30, 2005, 6:38am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120/3 "2005-11-30T06:38:32Z")

</div>

Just tried this, it didn’t help :\

Thanks

---

<div class="post-metadata">

**Author:** ![cilphex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cilphex](https://www.ruby-forum.com/u/cilphex)\
**Post date:** [November 30, 2005, 6:58am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120/4 "2005-11-30T06:58:41Z")

</div>

cilphex wrote:

> Just tried this, it didn’t help :\
> 
> Thanks

I tried your server against a ruby client and it worked. Ruby 1.8.2,  
WinXP.

---

<div class="post-metadata">

**Author:** ![cilphex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@cilphex](https://www.ruby-forum.com/u/cilphex)\
**Post date:** [November 30, 2005, 7:35am UTC](https://www.ruby-forum.com/t/ruby-and-c-network-interaction/49120/5 "2005-11-30T07:35:05Z")

</div>

Ah, I had an error in my C code! Sorry for wasting your time everyone.  
I thought it was good so I figured it had to be the Ruby.

Thanks for your help, though, and validating that the server worked.

Craig
