Arduino Wifi Rover!

Hurray! i finished my WiFi rover! though be it inefficient....extremely inefficient, im glad to say, i have a working prototype. The basic layout is, i drive it using a Java program that i made...Heres where we encounter our first problem, im not great with networking IO's yet, and, it shows. Here is my Java code.
Java Code:

[b]Main.java[/b]
[code]
package arduinocontrol;
import javax.swing.JFrame;

class Main extends JFrame{

    public void frameSetup(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Setup s = new Setup();
        add(s);
        s.run();
        setSize(200, 150);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        Main m = new Main();
        m.frameSetup();
    }

}

Setup.java

package arduinocontrol;

import java.awt.Color;
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class Setup extends JPanel {

    boolean rove = false;
    boolean check = true;
    boolean check1 = true;
    boolean check2 = true;
    boolean check3 = true;
    public static final int PORT = 1000;
    String serverName = "192.168.1.5";
    InetAddress serverIP = null;
    Socket clientSocket = null;
    Scanner in = null;
    PrintWriter out = null;

    JLabel label = new JLabel("Ready to drive...");
    public void run() {
        System.out.println("Running");
        setFocusable(true);
        setBackground(Color.black);
        label.setForeground(Color.green);
        label.setLocation(getX()/2, getY()/2);
        add(label);
        //setBackground(Color.BLACK);
        addKeyListener(new klAdapter());
    }

    public void goForword() {
        System.out.print("Foword");
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("1");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void goBackword() {
        System.out.print("Backword");
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("2");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void turnLeft() {
        System.out.print("Left");
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("3");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void turnRight() {
        System.out.print("Right");
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("4");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void Stopped() {
        System.out.print("Stopped");
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("0");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void roveStateON(){
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("6");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    public void roveStateOFF(){
        try {
            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            System.out.println("connected.");
        } catch (UnknownHostException e) {
            System.out.println("not found.");
        } catch (IOException e) {
            System.out.println("error connecting.");
        }
        out.println("7");
        try {
            clientSocket.close();
            System.out.println("Disconnected.");
        } catch (IOException e) {
            System.out.println("error disconnecting.");
        }
    }

    private class klAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            if ((key == KeyEvent.VK_UP) && check == false) {
                label.setText("Stopped...");
                Stopped();
                check = true;
            }
            if ((key == KeyEvent.VK_DOWN) && check1 == false) {
                 label.setText("Stopped...");
                Stopped();
                check1 = true;
            }
            if ((key == KeyEvent.VK_LEFT) && check2 == false) {
                 label.setText("Stopped...");
                Stopped();
                check2 = true;
            }
            if ((key == KeyEvent.VK_RIGHT) && check3 == false) {
                 label.setText("Stopped...");
                Stopped();
                check3 = true;
            }
        }

        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if ((key == KeyEvent.VK_SPACE)) {
                if(rove == true){
                    rove = false;
                    roveStateOFF();
                }else{
                    rove = true;
                    roveStateON();
                }
            }
            if ((key == KeyEvent.VK_UP) && check == true) {
                label.setText("Moving Forword!");
                goForword();
                check = false;
            }
            if ((key == KeyEvent.VK_DOWN) && check1 == true) {
                label.setText("Moving Backword!");
                goBackword();
                check1 = false;
                
            }
            if ((key == KeyEvent.VK_LEFT) && check2 == true) {
                label.setText("Turning Left!");
                turnLeft();
                check2 = false;
                
            }
            if ((key == KeyEvent.VK_RIGHT) && check3 == true) {
                label.setText("Turning Right!");
                turnRight();
                check3 = false;
                
            }
        }
    }
}

I need to improve that...ALOT! haha, help would be appreciated. I will post the Arduino code under this one!

Finally, here is the Arduino code!
RoboDuino:

#include <WiShield.h>
#include <Servo.h>
#define WIRELESS_MODE_INFRA      1
#define WIRELESS_MODE_ADHOC      2

int extern xVal;

unsigned char local_ip[] = {192,168,1,5};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};      // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};      // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"conway"};            // max 32 bytes
unsigned char security_type = 0;      // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
const prog_char security_passphrase[] PROGMEM = {"conway12"};      // Passwaord

prog_uchar wep_keys[] PROGMEM = {      0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,      // Key 0
                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00,      // Key 1
                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00,      // Key 2
                                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      0x00      // Key 3
                                                };
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;

const int pingPin = 6;
int leftVal;
int rightVal;
Servo rightServo;
Servo leftServo;
long inches; 
boolean roveState = false;
boolean check = false;

void setup(){
      WiFi.init();
        rightServo.attach(5);
        leftServo.attach(4);  
}

void loop(){
  WiFi.run();

if(xVal == 6){
  roveState = true;
}else if(xVal == 7){
 roveState = false;  
}
if(roveState == true){
  if(inches < 10 && check == true){
    leftServo.write(180);
    rightServo.write(180);
    delay(500);
    leftServo.write(90);
    rightServo.write(91);
    getRight();
    delay(100);
    
    leftServo.write(0);
    rightServo.write(0);
    delay(1000);
    leftServo.write(90);
    rightServo.write(91);
    getLeft();
    delay(100);
    
    leftServo.write(180);
    rightServo.write(180);
    delay(500);
    leftServo.write(90);
    rightServo.write(91);
    check = false;
    }
 if(leftVal < rightVal){
   leftServo.write(0);
    rightServo.write(0);
 }if(rightVal < leftVal){
   leftServo.write(180);
    rightServo.write(180);
 }
 
  else{
   leftServo.write(0);
  rightServo.write(180); 
  Serial.println("Forword!");
  check = true;
  }
}else{
if(xVal == 1){
   
   rightServo.write(180);
   leftServo.write(0);
}else if(xVal == 2){

  rightServo.write(0);
   leftServo.write(180);
}else if(xVal == 3){

  rightServo.write(0);
   leftServo.write(0);
}else if(xVal == 4){

  rightServo.write(180);
   leftServo.write(180);
}else{

  rightServo.write(91);
   leftServo.write(90);
}
  delay(100); 
}
}
void getRight(){
  getDistance();
  rightVal = inches;
}

void getLeft(){
   getDistance();
  leftVal = inches;
}

void getDistance(){
  long duration;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

socketapp:

#include "socketapp.h"
#include "uip.h"
#include <string.h>
int xVal; 

static int handle_connection(struct socket_app_state *s);

void socket_app_init(void){
  uip_listen(HTONS(1000));
}
void socket_app_appcall(void){
  struct socket_app_state *s = &(uip_conn->appstate);
  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  }
  handle_connection(s);
}

static int handle_connection(struct socket_app_state *s)
{

    PSOCK_BEGIN(&s->p);
  
  PSOCK_READTO(&s->p, '\n');
  xVal=atoi(s->inputbuffer);
  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
  
  PSOCK_CLOSE(&s->p);
  PSOCK_END(&s->p);
}

Soo, i need help wit the code! improvements improvements improvements! haha.

            serverIP = InetAddress.getByName(serverName);
            clientSocket = new Socket(serverIP, PORT);
            in = new Scanner(clientSocket.getInputStream());
            out = new PrintWriter(clientSocket.getOutputStream(), true);

You should be doing this once, in the constructor, not in every method that needs to send data.