C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 14th, 2006, 05:58 AM
tombell tombell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 423 tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 23 h 35 m 46 sec
Reputation Power: 13
Send null to socket

Hi to all

I would like to know how can I send null to a socket, so that I can evaluate it to null in Java.

My knowledge of C is limited.

Thanks for any help

Reply With Quote
  #2  
Old May 14th, 2006, 10:54 AM
animatronic animatronic is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 287 animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Weeks 6 m 56 sec
Reputation Power: 13
Your question is a little unclear about your intent, perhaps you could post the Java code of what your trying to acheive.

Reply With Quote
  #3  
Old May 14th, 2006, 11:04 AM
tombell tombell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 423 tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 23 h 35 m 46 sec
Reputation Power: 13
What I am doing is that the client (Java) sends a command to the server (C).
The server processes the command and sends back some data.
Most of the time, the response of the server is a single line (terminated by \n).

My problem is with a particular command which the server sends back multiple lines.
To read this in Java, I use readLine in a while loop.
The problem is that I have no way of breaking out of the loop.

Someone suggested that I should have the server send null to the socket as soon as he is done sending the actual data, so that I can evaluate it and break out of the loop.

Thanks

Reply With Quote
  #4  
Old May 14th, 2006, 11:06 AM
jafet jafet is offline
Redpill
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2005
Posts: 1,660 jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 150
Send a message via MSN to jafet
Is this a C question, or Java question? What socket? What do you mean by "send NULL"? Please elaborate.

[edit] Whoops, 2 minutes late. Oh well.
__________________
Code:
#include <stdio.h>
int main(int o,char**O){return o>-1?o-2||!main(-1,1+O)?!!fprintf(stderr,"%s [0-"
"9]{81}\n",*O):main(-83,++O):o>-83?(*O)[-1-o]?81==(o=-o-1)||o[*O]<'0'||'9'<o[*O]
?0:main(-2-o,O):o==-82:o>-164?(*O)[-83-o]<'1'?main(o-82,O):main(--o,O):o+164?o>-
246?(*O)[-165-o]<'1'?main(o-82,O):main(--o,O):o+246?o>-328?(*O)[o=-o-247]<='8'?(
main(-328-o,(++o[*O],O)),main(-247-o,O)):!(o[*O]='0'):(o=-o-328)<729?(o%9/3*3-o%
27+o/243*9+o/81%3&&(*O)[o%81]==(*O)[o%81-o%27+o%9/3*3+o/243*9+o/81%3])||(o%81-o%
9-o/81*9&&(*O)[o%81]==(*O)[o%9+o/81*9])||(o/81-o%9&&(*O)[o%81]==(*O)[o%81-o%9+o/
81])?0:main(-409-o,O):main(-165-o%81,O):!puts(*O):0                           ;}

Last edited by jafet : May 14th, 2006 at 12:25 PM.

Reply With Quote
  #5  
Old May 14th, 2006, 11:49 AM
animatronic animatronic is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 287 animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level)animatronic User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Weeks 6 m 56 sec
Reputation Power: 13
(guessing) the server is in Java and the cilent is in C?

Reply With Quote
  #6  
Old May 14th, 2006, 12:09 PM
tombell tombell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 423 tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 23 h 35 m 46 sec
Reputation Power: 13
No.
It is the other way around
See post 3

Reply With Quote
  #7  
Old May 14th, 2006, 07:19 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 35 m 10 sec
Reputation Power: 2194
Assuming you're done with the socket on the server, just close it:

c Code:
Original - c Code
  1. close(socket_file_descriptor);


I'm guessing that would have the effect for which you're looking.

Reply With Quote
  #8  
Old May 14th, 2006, 09:10 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
I guess part of the question is the overall effect you want.

When you break out of that loop, do you want to close the connection? If so, then ptr2void's suggestion would be a way.

If you are wanting to keep the connection (for more commands, I would think), then close() is not the solution.


Take that suggestion of sending null figuratively as meaning that you need to send some special character or string that indicates end-of-data. In C, "null" means a zero value (actually, it refers to a pointer value of zero), which has special meaning in C because you make the end of a string with a NIL (a byte containing the ASCII value of zero). Would the client expect any empty strings in that data? If not, then that could be your "null". If an empty string would be acceptable data, then use a control character; ie, characters of ASCII code from 0 to 31. Can Java handle control characters in an input stream?
sendn

Reply With Quote
  #9  
Old May 15th, 2006, 05:23 AM
tombell tombell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 423 tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 23 h 35 m 46 sec
Reputation Power: 13
Thank you for the replies.

Quote:
close(socket_file_descriptor);

Well, I need the socket to remain open.

dwise1_aol, thank you for the good explanation.
I will try sending the ASCII value of zero.

Reply With Quote
  #10  
Old May 16th, 2006, 11:46 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
An additional idea (which came to me as I was falling asleep last night): Or you could open a second socket for the actual data transfer and keep the original one open for commands and responses.

This is what FTP does. You start the session and issue commands and receive responses through port 21, but the actual transfer of files is through port 20 (designated ftp-data in your services file).

While embedding an end-of-data character (eg, a null character or ^D ('\x04') ) will work for text data, it won't work for binary data which could easily have your end-of-data values as valid data. So I'm just tossing this out as an idea for the future even though you should already have the remedy for your immediate problem.

Reply With Quote
  #11  
Old May 17th, 2006, 01:14 AM
tombell tombell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 423 tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level)tombell User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 23 h 35 m 46 sec
Reputation Power: 13
Thanks for the idea.

However, I am concentrating on the client part to solve my problem.
Nice idea though, for future reference
Thanks

Reply With Quote
  #12  
Old May 19th, 2006, 08:27 AM
ripunjaytripath ripunjaytripath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ripunjaytripath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 38 sec
Reputation Power: 0
Another Probability....

In such cases we can create a message header that contains the length of data followed by the data itself. The reading process first reads the length of the out put and then the out put itself. After that it calculates, the size and resumes processing.


You can modify the overall sceme to suit ur purpose.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Send null to socket

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap