Problem in Serial Communication.

I am working on my final year project these days which is Arduino based hexapod. I am using Ubuntu 12.04 operating system. Everything was going perfect till last night but today when i tried to check my code its showing me errors and I am unable to communicate with arduino. I am using Arduino Mega 2560.
Here is my code:

#include <Servo.h>

const int normal_angle = 90;

Servo leg1_servo_1;
Servo leg1_servo_2;
Servo leg1_servo_3;

void setup() {
  Serial.begin(9600);
  leg1_servo_1.attach(2);
  leg1_servo_2.attach(4);
  leg1_servo_3.attach(6);
  leg1_servo_1.write(normal_angle);
  leg1_servo_2.write(normal_angle);
  leg1_servo_3.write(normal_angle);
}

void loop() {
  if (Serial.available()) {
    byte incomingByte = Serial.read();
    while (incomingByte != '\n') {
      if (incomingByte == 'w' || incomingByte == 'W')
        move_forward();
      if (incomingByte == 'd' || incomingByte == 'D')
        move_right();
      incomingByte = Serial.read();
    }
  }
}

void move_forward() {
  leg1_servo_2.write(normal_angle - 20);
  delay(200);
  leg1_servo_1.write(normal_angle + 20);
  delay(200);
  leg1_servo_2.write(normal_angle);
  delay(200);
  leg1_servo_1.write(normal_angle);
  delay(200);
}

void move_right() {
  leg1_servo_2.write(normal_angle - 20);
  delay(200);
  leg1_servo_3.write(normal_angle + 20);
  delay(200);
  leg1_servo_2.write(normal_angle);
  delay(200);
  leg1_servo_3.write(normal_angle);
  delay(200);
}

and here are my errors

java.io.IOException: Input/output error in writeArray
	at gnu.io.RXTXPort.writeArray(Native Method)
	at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1173)
	at processing.app.Serial.write(Serial.java:469)
	at processing.app.Serial.write(Serial.java:492)
	at processing.app.SerialMonitor.send(SerialMonitor.java:197)
	at processing.app.SerialMonitor.access$100(SerialMonitor.java:31)
	at processing.app.SerialMonitor$3.actionPerformed(SerialMonitor.java:86)
	at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
	at javax.swing.JTextField.postActionEvent(JTextField.java:721)
	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1662)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2878)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2925)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2841)
	at java.awt.Component.processEvent(Component.java:6282)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
	at java.awt.Component.dispatchEventImpl(Component.java:4731)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Window.dispatchEventImpl(Window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:694)
	at java.awt.EventQueue$3.run(EventQueue.java:692)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:708)
	at java.awt.EventQueue$4.run(EventQueue.java:706)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I have tried small block of codes to check if the board is working properly, and everything is fine.

I just compiled it and sent it to a Mega using a Mac. Not very helpful, but it perhaps suggests that you have an issue with your environment.

there is no issue in uploading and verifying. when i try to communicate through serial monitor after sending one value it just displays this value and when i try to upload again it asks me to change the serial port. do not want to re-install my operating system

Compiles under windows....

there is no issue in uploading and verifying. when i try to communicate through serial monitor after sending one value it just displays this value and when i try to upload again it asks me to change the serial port. do not want to re-install my operating system

So, if one can compile and upload, one realizes that the basic serial/USB functionality in the Arduino and in the OS are working as are the cables and ports.

When you run the sketch, you get an error. This appears to be a serial issue either in how the OS is managing serial for the Arduino or the serial communications protocol, etc. Try running a serial loopback example on the Mega2560. Something like: http://forum.arduino.cc/index.php?topic=73748.0

Maybe try another USB port, too, or try through a powered-hub. When you do these things, the OS builds new entries for the devices.

When you run the sketch, you get an error. This appears to be a serial issue either in how the OS is managing serial for the Arduino or the serial communications protocol, etc.

this is the issue i am having. because after the error is displayed IDE allocates a different port to the board.

i have also checked the loopback...its working fine.. but still unable to work with my sketch

Disconnect the servos and try the program. If you are powering the servos from the Arduino you may be overloading the USB port.

Disconnect the servos and try the program. If you are powering the servos from the Arduino you may be overloading the USB port.

This can be the problem, but it was working yesterday night. now unable to work with one servo as well... another code in which i am trying to give specific angle to servos is also not working. after 2 3 values program stuck with same error

The USB ports changing is a sign that the Arduino is disconnecting and reconnecting. Overloading the USB port (or the Arduino power supply) may cause this or it could be a bad USB cable, try a different cable. Just because it worked before doesn't mean it will work now. I don't think it is likely to be your code. But if you think it is the code post the code. Your other code worked fine on my Linux system. DO NOT POWER MOTORS FROM THE ARDUINO POWER SUPPLY.