so to start i am just trying to make 2 clients and a server. The clients will connect to the server and pass a message from one client to the other and it is not working right. The message never gets sent and the server and both clients just hang. Do i need a different name for the different client Sockets? If so I don't know how i would do this because both people playing will be running exact copies of the same program and connect to the server and then the server passes messages between them.
Here is the server code i have
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] args) throws IOException
{
ServerSocket srvr = new ServerSocket(1234);
Socket skt1 = new Socket();
skt1 = srvr.accept();
System.out.print("Server has connected!\n");
skt1 = srvr.accept();
System.out.print("Server has connected!\n");
// network IO for client
PrintWriter out1 = new PrintWriter(skt1.getOutputStream(), true);
BufferedReader in1 = new BufferedReader(
new InputStreamReader(
skt1.getInputStream()));
String line = null;
line = in1.readLine();
out1.println(line);
out1.close();
in1.close();
skt1.close();
} // end main
} // end class Server
here is the client code I have
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException
{
Socket skt1 = new Socket("Carl1", 1234);
PrintWriter out = new PrintWriter(skt1.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
skt1.getInputStream()));
String line = "Send this to the other client";
while(true)
{
try
{
line = in.readLine();
System.out.println(line);
out.println(line);
} // end try
//catches bad user input and throws exception
catch (NumberFormatException ex)
{
System.out.print("Error bad data: ");
} // end catch
} // end while
} // end main
} // end Client
I want the one client to send the String line to the server and then the server to pass it to the second client. This is like how the battleship game will work one player sends the row and column he wants to hit and the server sends that information to the other client.