Transmitting and receiving data to and fro the same arduino

I am transmitting an integer value from the Tx pin (PIN 1) and receving the same data at the recever pin (PIN0). However, after 2 iterations, the received data is getting changed with respect to the data sent. Here is my code, Kindly give your suggestions, where I am going wrong.

#include <SoftwareSerial.h>

char str[4];
char str1[4];
int senddata= 1234;

void setup()

{
Serial.begin(9600);

}

void loop()
{
int i=0;
itoa(senddata, str1, 10); //Turn value into a character array

Serial.write(str1);

if (Serial.available())

{

while(Serial.available() && i<4)
{

str[i++] = Serial.read();

}

if (i>0)
Serial.println(str);
}

}

I am not receiving 1234 at the output but 123 or 12 or 1234 sometimes.

itoa(senddata, str1, 10); //Turn value into a character arraysenddata is 1234 so str1 needs to be 5 characters long to allow for the terminating '\0' added by itoa()

Is Serial.write appropriate to send 5 bytes?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. If you have a good scheme for receiving data the sending part is simple.

...R