0

I've written a server in python and the client in C.

The python server send i.e. "1 1000\n" to the C client.

The C function that recieves this string should parse it into two long int's.

void receive_job() {
 char tmp_buffer[256];
 long start, stop;
 recv(sock, tmp_buffer, 255, 0);
 /*
 here I wonder how I can parse tmp_buffer to set start and stop values.
 */
}

I am not very proficient in C, so I would appreciate a comment on this.

4 Answers 4

5

Have a look at strtol() or strtoul(), depending on your concrete data type:

char end;
long x = strtol(tmp_buffer,&end,10);
long y = strtol(end+1,NULL,10);
Sign up to request clarification or add additional context in comments.

Comments

4

Use strtol.

char data[] = "1 1000\n";
char *next, *err;
long a = strtol(data, &next, 10);
long b = strtol(next, &err, 10);

Comments

2

Look up sscanf manual page - but check the return value!

Comments

1

It is better to clear the tmp_buffer using memset before calling recv or at least to add a null byte in it. Otherwise it would contain garbage that you might parse if something got transmitted wrongly.

1 Comment

thanks, I'm currently using sscanf with the expression "%ld %ld\n", so I reckon it shouldn't be necessary to clear the tmp_buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.