Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2005, 7:52 AM   #1
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
invalid literal problem driving me nuts

i have a socket program running thanks to cerulean. it passes data back and forth between clients. when i print the data coming in to a client, it prints a series of 0's. but its a string. so i wanted to make it an int, i used int(data). unfortunately, this produces an invalid literal error. why wont it let me do this? they Are 0's in string form. i thought perhaps there was extra whitespace that was making the conversion from string to int mess up, so i used strip(). still no luck. what can be the problem?

sigh..i've found the problem. somehow when i send the data FROM my client, =what is recieved is inconsistent. i only noticed this when i slowed the output down to a delay of about 10000 ms. damn..

well, i checked everything. the data that the clients are sending sometimes comes a bit skewed back to the other client. theres an extra 0 or that really messes things up sometimes. i know its not my clients send data, cuz i checked it, its consistently good. i compared what client 1 was sending directly to what client 2 was recieving and it WAS different. damn...i'll guess i'll have to accept that these errors occasionally occur and use a try/except block or something

Last edited by cypherkronis; Jul 2nd, 2005 at 8:03 AM.
cypherkronis is offline   Reply With Quote
Old Jul 2nd, 2005, 9:00 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I would suggest that you probably have an error in your code; particularly if you're using TCP/IP.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 2nd, 2005, 9:41 AM   #3
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
I assume you're using UDP? With UDP there is no way of telling what has ended up where, but it tends to be much faster.. If you are, I really do recommend you use TCP - you won't get much of a performace gain with UDP unless you really know what you're doing.
If you're using TCP, bear in mind the fact that sometimes packets may get grouped together, or you'll receive different bits at different times. With that said, I have found consistency to be excellent with all the client/server applications i've created.
Cerulean is offline   Reply With Quote
Old Jul 2nd, 2005, 11:51 AM   #4
cypherkronis
Newbie
 
Join Date: Jun 2005
Posts: 12
Rep Power: 0 cypherkronis is on a distinguished road
how do u mean, different bits at a time? i was expecting to get one full thing of data from one "send" command to arrive intact. is this not the case?
cypherkronis is offline   Reply With Quote
Old Jul 3rd, 2005, 9:23 AM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
To be honest I was basing this on prior experience with a TCP client application which may have grouped packets together for the sake of efficiency (it was a game client). I'm not sure if Python does anything of the same.
If you're using TCP, show some code and let's see if we can get it to work.
Cerulean is offline   Reply With Quote
Old Jul 3rd, 2005, 2:03 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
There's actually a flag to set on the socket to prevent this scheduling when working with a real time application like a game. According to some sources I have laying around you need to set the socket option "TCP_NODELAY".

From my TCP and socket experience, you absolutely cannot assume that the socket will give you the amount of data you requested. The parameter for max bytes to read or send is in many cases not how many you actually read or send. Check the return value. That tells you how many bytes were actually read or sent, and in the case of reading will give you 0 if the remote client closed the connection. In many cases the stream is chopped up into small segments. Even for file transfer, it's not just a straightaway send-fest with data going in one direction. Data goes back and forth. Something like packets on top of packets. The simplest way to do this is to in your sending code first write the length of the chunk you are sending, then in the receiving code read the length (don't assume it comes in with one recv call even though it is only a few bytes. It most likely will but do you really want your stream offset by a few bytes and get a bunch of garbage?) and then you can keep reading until you read that exact amount of bytes. The length can be a little nasty. Do a lot of sanity checking on the length before you start trying to read that many bytes. It could very well come in as 0 (which has caused me some nasty debugging sessions...reading with a max of 0 bytes doesn't work very well, let me tell you, but took me forever to track down) or an insanely large number that will keep you in the loop for eternity. An easy way to determine if you are getting garbage data is to, before the length, stick a little header. This is the oldest trick in the book as far...its used on just about every file format and protocol I've worked with. This includes HTTP (Capture some packets. Every chunk starts with HTTP) and the PE executable format used on windows (First two bytes are always "MZ"). This adds another few lines, but you can immediately tell if one of the clients isn't working properly. If the first few bytes of the message aren't what you expect, throw an error. Feel free to use your initials. Just my $0.02.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270

Last edited by Dameon; Jul 3rd, 2005 at 2:20 PM.
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC