Partial Write for sockets in LINUX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Partial Write for sockets in LINUX
# 1  
Old 05-11-2011
Partial Write for sockets in LINUX

We have a server-client communication in our application. Sockets are used for the communication. We are using AF_INET sockets with SOCK_STREAM(TCP/IP). Also these sockets are in Non Blocking mode (O_NONBLOCK). Application is written in C++ on UNIX.

In our system the Server will write to a socket and Client will read from it. We had written code to handle partial writes. If a partial happens, we will try 30 more times to write the entire data.

Our Server try to write 2464 bytes to the socket. In some cases it could not write entire data. So server will try writing 30 more times to transfer entire data. Most of the times the entire data will be written within 30 tries. But some times the even after 30 reties sever wil not be able to write the entire data. Here it will throw EAGAIN error. Problem happens in the Client Side when it tries to read this partially written data.

Consider the server tried to write 2464 bytes. But after the repeated 30 attempts it could write only 1080 bytes. Server will raise a EAGAIN at this point. Client try to read 2464 bytes. The read command will return 2464 and hence the read itself is ok. But the data we received is a corrupted one (Partially written data only). So client crashes.

Can any one please advise on following,

1) Is it possible to remove only the partially written data by the server itself. Thus the client will not recieve corrupted incomplete data?. (We cannot use a read() function from server to remove this. Consider server successfully written n messages to the socket. Client is in busy state and not able to read them. Then the server try to write the n+1 th message and Partial write occured. If we use read command from the server, the entire n successfull messages alo get removed. We need to remove the Partially witten (n+1 th) message only)

2) Is there any way to identify in client side that we had read a partially written message?.

Please note that we are facing the partial write issue in LINUX(REDHAT 5.4) only. System is working fine in Solaris (In solaris either eh entire data will be written OR NO data witll be written with in 30 tries of write).

Thanks in advance.
# 2  
Old 05-12-2011
Quote:
Also these sockets are in Non Blocking mode (O_NONBLOCK)
Any reason for not using standard blocking mode?

Quote:
Consider the server tried to write 2464 bytes. But after the repeated 30 attempts it could write only 1080 bytes. Server will raise a EAGAIN at this point. Client try to read 2464 bytes. The read command will return 2464 and hence the read itself is ok. But the data we received is a corrupted one (Partially written data only). So client crashes.
I can't quite follow. If only 1080 has been sent, I don't think that read() returns 2464 bytes...

Quote:
2) Is there any way to identify in client side that we had read a partially written message?.
You need to handle partial read anyway with TCP, since you don't know how your message will be segmented. The usual design pattern goes along those lines:
- a message is composed of a header and payload section,
- the header contains (among other) the payload size,
- the receiver read() first the header (a few bytes), decodes the expected payload length. To get the payload, it re-iterates read() until the wanted size is reached or an error occurs.

Quote:
Please note that we are facing the partial write issue in LINUX(REDHAT 5.4) only. System is working fine in Solaris (In solaris either eh entire data will be written OR NO data witll be written with in 30 tries of write).
Perhaps tune kernel parameters TCP send/recv queue length ?

HTH, Loïc
This User Gave Thanks to Loic Domaigne For This Post:
# 3  
Old 05-12-2011
You're probably spinning your write() retries faster than the buffers that feed the socket can drain. If it's not returning an error, keep trying to send the bytes that you didn't already send.

And FWIW, the number of bytes a read() returns on the other end are only loosely correlated with what you write(). The data comes out as a stream - literally. If you have to take it apart, you have to provide your own delimiters as Loic already posted.

Just because it works fine under Solaris and not under Linux means almost nothing.
This User Gave Thanks to achenle For This Post:
# 4  
Old 05-19-2011
Thank you all very much for your answers. The real problem is i cannot perform the write infinitely/BLOCKING mode, since there are lot of other clients waiting the response from server. Let me try some code changes with inputs from your answers.
# 5  
Old 05-19-2011
Quote:
Originally Posted by sanushchacko
Thank you all very much for your answers. The real problem is i cannot perform the write infinitely/BLOCKING mode, since there are lot of other clients waiting the response from server. Let me try some code changes with inputs from your answers.
Consider going multi-threaded (or multi-forked)... This may simplify greatly your concurrency concerns.

Cheers, Loïc
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

SE Linux write permission denied

Hi, In my server I am getting below errors in "/var/log/messages": Oct 8 14:45:44 LKOGOMEEMM01 kernel: type=1400 audit(1444295744.792:15818): avc: denied { write } for pid=53421 comm="ip" path="/var/VRTSvcs/log/tmp/IPMultiNIC-8" dev=dm-0 ino=2754879 scontext=system_u:system_r:ifconfig_t:s0... (4 Replies)
Discussion started by: rochitsharma
4 Replies

2. Cybersecurity

root cannot write to Linux RAM

void main() { long ((long)(&array)); int x; for (;;) { (array) =+ 1023; printf("%c", array); } } What is wrong with this code to print Linux RAM? (13 Replies)
Discussion started by: Alux
13 Replies

3. Programming

help: problem with sockets write/read

I am trying to make a server and client, the client will choose between some options and the server will react accordingly. After a some reads and writes that work the server needs to read from client an INT i use this: read(newSd,&k,sizeof(int));But even if all the other times there was no... (1 Reply)
Discussion started by: theSling
1 Replies

4. Programming

Sockets (C under Linux)

Hi, I have to write an UDP broadcast. It works so far, but how can I get the actual network address at runtime? Should run under Linux and Unix (Solaris). Thanks (1 Reply)
Discussion started by: peng1967
1 Replies

5. Shell Programming and Scripting

How to bulk changing partial file name in Linux?

I have a bunch of database files in a directory. Their name are like: 1234_ABCD_01.dbf, 28hrs_ABCD_02.dbf I want to change them in bulk to: 1234_XXXU_01.dbf, 28hrs_XXXU_02.dbf. I mean I just want to replace ABCD by XXXU. don't change other part of the file name and the contents of the... (4 Replies)
Discussion started by: duke0001
4 Replies

6. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

7. Programming

c++, raw sockets, stopping kernel write in header?

Hi, im trying to lern about raw sockets with my debian and c++. Tried to make a icmp and tcp packet and send it with sendto. Checked on wireshark and recognized that kernel changed my headers. So searched about stopping the kernel change the header and tried it with setsockopt, like said in at this... (4 Replies)
Discussion started by: sandcastle
4 Replies

8. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

9. Programming

Linux BSD sockets blocking issue

I am using BSD TCP sockets under Debian Linux 2.6 and no matter what I do, the socket blocks on recv. I have set O_NONBLOCK and O_NDELAY using fcntl to no effect. Any ideas ? (3 Replies)
Discussion started by: johnmb
3 Replies
Login or Register to Ask a Question