Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Simple readline algorithm for Networking

Name: Anonymous 2008-08-19 16:52

Are there any simple standardized way of reading a full line from a socket that mallocs the memory for you? If so, what are they? If not, is there a way to do it in approximately O(1) time (obviously not including the time for the memory allocation).

Name: Anonymous 2008-08-19 17:11

fgetln

Also, don't.

Name: Anonymous 2008-08-19 17:41

>>2
Why?

Name: Anonymous 2008-08-19 17:55

The better way is to read data into a buffer, and after there's no more data (at this moment), read all complete lines from the buffer. Implementation is left as an exercise for the reader.

Name: Anonymous 2008-08-19 17:56

>>4
I think you mean BUFFA.

Name: Anonymous 2008-08-19 18:04

BufferedReader r = new BufferedReader(new InputStreamReader(new Socket(host, port).getInputStream()))
String s;
while((s = r.readLine() != null) process(s);
r.close();

Name: Anonymous 2008-08-19 18:14

In before FactoryFactoryFactoryFactory

Name: Anonymous 2008-08-19 18:16

watsa fatcory? xplain, plz.

Name: Anonymous 2008-08-19 18:20

>>8
0/10

Name: Anonymous 2008-08-19 18:26

>>8
back to /b/, plz.

Name: Anonymous 2008-08-19 18:27

sageing this thread because of >>8

Name: Anonymous 2008-08-19 19:14

Bumping this thread because of BUFFA

Name: Anonymous 2008-08-19 20:48

>>6
Still gotta connect dat dere Socket up, nigga

Name: Anonymous 2008-08-19 21:31

wut wut is a factory?

Name: Anonymous 2008-08-19 21:37

>>3
Because you don't want to BUFFA unlimited data from the network just because it has no newline.

Name: Anonymous 2008-08-19 21:56

>>15
DON'T HELP HIM

Name: Anonymous 2008-08-19 23:58

malloc it yourself, use dynamic expansion (double, triple, whatever) on the buffer.

If not, is there a way to do it in approximately O(1) time
Is it possible to read 1 byte and 1,000,000 bytes in the same amount of time? That was a rhetorical question.

Name: Anonymous 2008-08-20 0:15

I guess ill just send integers over the socket to the server, that way I don't have to do any parsing!

Name: Anonymous 2008-08-20 0:44

First choice: Use Erlang.
Second choice:  Use JAVA.
else: kill yourself.

Name: Anonymous 2008-08-20 7:35

FILE* f;
char buffa[LINE_MAX];

f = fdopen(socket, "r+");
fgets(buffa, sizeof(buffa), f);

Name: Anonymous 2008-08-20 8:14

O(1)?  I highly doubt it because socket memory is claimed by the kernel.  I think you have to copy it unless you work in kernel mode.

Name: Anonymous 2008-08-20 14:00

>>20
#define LINE_MAX UINT_MAX

Now what?

Name: Anonymous 2008-08-20 14:22

Dynamic languages usually have a function to read an entire line into dynamically allocated memory. Tons of people use these functions every day, so why is it any different to want to do this in C? By the way, using fdopen is a total fucking hack and you know it.

An example of why I want a function like this: Say you're building a web server, and you want to read in the request headers. You could read them line by line, parsing each line as you go, or you could read the whole request at once until the client disconnects or something. Either way theres the possibility that the request is malformed and it could totally fuck up your application.

Obviously you could set the maximum amount of characters per line, because I believe there is an HTTP code specifically for that, but why?

Name: Anonymous 2008-08-20 15:00

>>23
By the way, using fdopen is a total fucking hack and you know it.

0/10

Name: Anonymous 2008-08-20 15:03

Tons of people use these functions every day, so why is it any different to want to do this in C?
Because C has no infrastructure in which to do anything automatic.  Enjoy your portable assembler.

Name: Anonymous 2008-08-20 15:07

>>25

2/10

Name: Anonymous 2008-08-20 15:30

>>26
0/10

I agree with >>25 .  You can use C to make programs that do all sorts of cool shit, but you can't do cool shit inside the programming language itself unless you resort to code generation or other such non-C solutions.

Name: Anonymous 2008-08-20 15:30

>>25
I will, along with my job in the embedded market.

Name: Anonymous 2008-08-20 15:49

>>28
I will, along with fighting against constraints and manual re-implementations of everything.
You can't have your cake and eat it, too, with flexibility and embedded limitations.  So the answer to >>1 is:

if (embedded || low-level)
  deal with the shittiness of C
else
  use anything else, fuckhead!  C is a waste of time!

Name: Anonymous 2008-08-20 16:15

>>27
Regardless of your opinion of C, it's retarded to say C doesn't support what he asked when the answer was given in the very first response.

Name: Anonymous 2011-01-31 20:30

<-- check em dubz

Name: Anonymous 2011-02-04 11:30


Don't change these.
Name: Email:
Entire Thread Thread List