ok I think I got it. It is a little rough around the edges but the clients take turns entering a string at the keyboard and then it sends it to the server and the other client also receives it. I have questions about the loops though. Since there are only 2 players can the connections be made before the main loop. So in the Server class can the s.accept() call be made outside of the loop twice for the 2 clients? Also in the NewMain class can the client.connect() and client.streams() methods be called before the loop or do they need to be done every time? Right now the clients take turns which i guess works since battleship is a turn based game anyway when its one persons turn the other will not be sending anything.
Server class
import java.net.*;
import java.io.*;
//import java.net.*;
public class Server
{
private static Socket connection;
private static ObjectOutputStream output;
private static ObjectInputStream input;
public static void main(String[] args) throws java.io.IOException
{
ServerSocket s = new ServerSocket(1234);
String string = "";
//connection = s.accept();
while(true)
{
connection = s.accept();
System.out.println("client connected");
streams();
//send("Send this to client");
send(string);
string = receive();
//send(string);
//System.out.println("Listenning...");
//(new ServerThread(s.accept())).start();
//System.out.println("Spawned Thread.");
} // end while
} // end main
// establish the streams
private static void streams() throws IOException
{
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
} // end streams
private static void send(String or_variable)
{
try
{
output.writeObject("String" + or_variable);
output.flush();
} // end try
catch(IOException ioException)
{
}
} // end send
private static String receive() throws IOException
{
String test = "";
try
{
test = (String)input.readObject();
System.out.print("\n" + test);
} // end try
catch(ClassNotFoundException classNotFoundException)
{
System.out.print("\nUnknown object recieved");
} // end catch
return test;
} // end receive
} // end Server
Client class
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
private Socket client;
private ObjectOutputStream output;
private ObjectInputStream input;
//Client constructor
private void client()
{
try
{
connect();
streams();
processConnection();
} // end try
catch (EOFException eofException)
{
System.err.println("Client terminated connection");
} // end catch
catch (IOException ioException)
{
ioException.printStackTrace();
} // end catch
finally
{
closeConnection();
} // end finally
} // end client
public void connect() throws IOException
{
System.out.print("Attempting Connection\n");
client = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
System.out.print("Connected to: " + client.getInetAddress().getHostName());
} // end connect
public void streams() throws IOException
{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
System.out.print("\nStreams Estabolished\n");
} // end streams
public void processConnection() throws IOException
{
String test = "";
// do
// {
try
{
test = (String)input.readObject();
System.out.print("\n" + test);
} // end try
catch(ClassNotFoundException classNotFoundException)
{
System.out.print("\nUnknown object recieved");
} // end catch
// }while(!test.equals("Server Connection Lost")); // end do while
} // end processConnection
public void closeConnection()
{
try
{
output.close();
input.close();
client.close();
} // end try
catch(IOException ioException)
{
ioException.printStackTrace();
} // end catch
} //closeConnection
public void sendData(String data)
{
try
{
output.writeObject(data);
output.flush();
} // end try
catch(IOException ioException)
{
System.out.print("\nError writing object");
} // end catch
} // end sendData
public static String promptUser() throws IOException
{
BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.print("Enter something: ");
try
{
line = keyInput.readLine();
} // end try
//catches bad user input and throws exception
catch (NumberFormatException ex)
{
System.out.print("Error bad data: ");
} // end catch
return line;
} // promptUser
} // end client
NewMain
import java.io.*;
public class newMain
{
public static void main(String[] args) throws IOException
{
Client c = new Client();
while (true)
{
c.connect();
c.streams();
c.processConnection();
String line = c.promptUser();
System.out.println(line);
c.sendData(line);
}
} // end main
} // end newMain