I am trying to out put a random number from my server. I have the random number set up and converted into a string for the buffer reader but i am still getting an error, can anyone see where I am going wrong?
If anyone is interested I have worked on the code and it is now working as it should
Updated Working Server code
import java.net.*;
import java.io.*;
import java.util.Random;
public class server extends Thread
{
private ServerSocket serverSocket;
public server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run()
{
System.out.println("Starting game...");
while(true)
{
System.out.println("Client connection established! Game started");
try
{
Socket server = serverSocket.accept();
Random rand = new Random();
int randInt = rand.nextInt(12);
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Turning on button " + randInt);
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
out.writeUTF("Acknowledged - Button 1 pressed");
}// End try
catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}// End catch
catch(IOException e)
{
e.printStackTrace();
break;
}// End catch
}// End while()
}// End run()
/*The start of the main loop */
public static void main(String [] args)
{
int port = 4444;
try
{
Thread t = new server(port);
t.start();
}// End try
catch(IOException e)
{
e.printStackTrace();
}// End catch
}// End main()
}/
Updated Working Client Code
import java.net.*;
import java.io.*;
import java.util.Random;
public class client
{
public static void main(String [] args)
{
String serverName = "localhost";
int port = 4444;
try
{
Socket client = new Socket(serverName, port);
Random rand = new Random();
int randInt = rand.nextInt(12);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Button " + randInt + " pressed");
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
}// End client
catch(IOException e)
{
e.printStackTrace();
}// End catch
}// End main
}