doon
doon

Reputation: 2361

How to force partial writes to test a network server

I have a written an event driven server (request response protocol - HTTP like) using non blocking sockets. I tested partial reads by writing a client that sends the request in discrete chunks after every few seconds. How do I test partial writes i.e. I need to write 100 bytes and then send call returns with only 10 bytes written.

I am writing perl based clients. This is over SSL but for the purposes of this question, I am ok with partial writes on TCP.

Upvotes: 5

Views: 324

Answers (3)

hookenz
hookenz

Reputation: 38907

Write lots of data very quickly.

Make the output buffer very small (as suggested by Jan)

or

Create your own send function

e.g.

ssize_t mysend(int sockfd, const void *buf, size_t len, int flags)
{
    int shortlen = len - 100 > 0 ? len - 100; len
    return send(sockfd, buf, shortlen, flags);
}

Which will always send 100 bytes short except if sending < 100 bytes. Otherwise it'll send what you give it if it can. Once you've verified your partial send handling is working correctly, change the call to mysend back to the ordinary send and delete mysend from your hackary code.

Upvotes: 0

Jan Wrobel
Jan Wrobel

Reputation: 7099

Try to reduce the size of the output buffer:

 err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 1)

This should make send output only part of the buffer.

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

Use TCP flow control - make your client read from the socket very slowly, so the sender (server) socket send buffer fills up with data.

Upvotes: 0

Related Questions