LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2007, 06:23 AM   #1
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Rep: Reputation: 0
socket test problem


I'm writing a program which uses sockets and now I want to start testing if actually works
I wrote a small test program which opens a connection to "127.0.0.1" and transmits two messages to my program.

Program:
1. socket()
2. bind()
3. listen()
4. accept()
5. recv() loop.

Test program:
1. socket()
2. connect()
3. send(msg1)
4. send(msg2)

the accept/connect part completes without any problems but recv() returns 0 before the test program transmits it's 1st message. As I understand this means that the socket has disconnected.
Both send() returns -1.

Isn't it possible to run both my program and test program on the same pc or do I need to the setup differently?

BR, Thomas Fogh
 
Old 07-26-2007, 06:49 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
networking works fine on the same machine no worries.

have you heard of netcat?
man nc

it's ideal for this stuff, it's better to test the client and server
seperately when you have trouble,
netcat enables you to do this quite easily.

e.g; try

ls | nc -lp 50666


then point your browser at
http://localhost:50666
 
Old 07-26-2007, 07:14 AM   #3
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks, that looks pretty good
But how do I send two binary messages from NC?
 
Old 07-26-2007, 07:33 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
ahh, binary eh?
well that's a horse of a different colour.
I always keep to ascii protocols where possible.

nc will pass binary as well as ascii.
it just makes life more difficult for you.

something like?
cat msg1 msg2 | nc -vv -w 2 host port

Last edited by bigearsbilly; 07-26-2007 at 07:34 AM.
 
Old 07-26-2007, 07:46 AM   #5
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Ok, I'll try that. Thanks!
BTW:
I have a struct:
typedef struct {
char msg_id;
long length;
} msg_t;

sizeof(msg_t.msg_id) = 1
sizeof(msg_t.length) = 4
sizeof(msg_t) = 8
I guess this is because my OS is 32 bit, but can I (easily) avoid having to send 8 bytes, and instead just the 5 I need?
(Without making a send-function for every message struct I have.)
 
Old 07-26-2007, 08:13 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what are you doing?

I'd be tempted to use line buffered ascii data to start with,
at least until you understand how sockets operate.
if they are readable messages why not transmit them as such.

sockets aren't difficult, they are just awkward and confusing at times.

you see a lot of inet protocols are text based, SMTP and HTTP to name but two.
try nc localhost 25 and play with the mail server for instance. the advantage is
you can debug easy using tools like nc.
 
Old 07-26-2007, 08:19 AM   #7
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
An XMMS plugin to edit the playlist and control playback over a Wifi connection.
(A PSP connected to a PS3 with Ubuntu.)
I want to display album pictures so I need to send binary.
 
Old 07-26-2007, 08:38 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
so quite a complex protocol then?
but using an ascii protocol does not preclude sending binary data.

I've done it myself.

Not that I'm trying to dissuade you but if it's a complex protocol
not human readable, with lots of messages you'll need a suite of functions
to help debug it too.

if you are sendin a TCP packet with a message in it why not make it
readable.

Last edited by bigearsbilly; 07-26-2007 at 08:41 AM.
 
Old 07-26-2007, 08:48 AM   #9
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
It's not to bad
And I found my error. I thought connect() returned a new socket like accept() does. Whoops!
I only have around 20 messages and only 3-4 complex ones, so I think I'll stick with the binary.
Thank you for your help and suggestions, though! (It's always nice to learn a new linux "command" )
 
Old 07-26-2007, 09:04 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
i'd be interested to see it when it's done
 
Old 07-27-2007, 01:52 AM   #11
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
I'll try to remember to post an update on this thread.
And I'll upload it the XMMS site, when it's done.
(Hopefully in a couple of weeks depended on this crappy weather )
 
Old 07-27-2007, 02:23 AM   #12
thomas_fogh
LQ Newbie
 
Registered: Mar 2006
Location: Denmark
Distribution: Fedora, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
BTW, is TCP error-free or do I need to implement a CRC check?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
connection between java socket and unix socket sathishkumar Programming 1 01-10-2006 03:21 PM
fseek on a socket descriptor to discard socket buffer? Thinking Programming 1 12-06-2005 09:15 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
IO::Socket Test Difficulties aj_araujo Programming 2 08-21-2003 08:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration