Alright I have an echo server that can connect to clients and echo back what they enter. Now I am stuck on how do i get the input from one client and send it to another client rather than back to the client it came from. I have 3 classes and i will post them. If someone could let me know what i need to do to make it so instead of the server echoing the information it passes the information to a second client.
Sever, main method
import java.net.*;
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws java.io.IOException
{
ServerSocket s = new ServerSocket(1234);
while(true)
{
System.out.println("Listenning...");
(new ServerThread(s.accept())).start();
System.out.println("Spawned Thread.");
} // end while
} // end main
} // end Server
ServerThread - created for each new client that connects
import java.net.*;
import java.io.*;
public class ServerThread extends Thread
{
private BufferedReader input = null;
private PrintWriter output = null;
private PrintWriter output1 = null;
private BufferedReader keyInput = null;
/** Creates a new instance of MudServerThread */
public ServerThread(Socket sock)
{
try
{
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
output = new PrintWriter(sock.getOutputStream(), true);
keyInput = new BufferedReader(new InputStreamReader(System.in));
} // end try
catch(IOException e)
{
System.err.println("Problem with thread!!");
System.exit(1);
} // end catch
} // end ServerThread
public void run()
{
String line = "";
while (true)
{
try
{
line = input.readLine();
System.out.println(line);
output.println(line);
} // end try
catch(IOException e)
{
System.err.println(e);
System.exit(1);
} // end catch
} // end while
} // end run
} // end class ServerThread
the Client class
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws IOException
{
Socket skt1 = new Socket("192.168.1.102", 1234);
PrintWriter out = new PrintWriter(skt1.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
skt1.getInputStream()));
BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
Random r = new Random();
int[] ar = new int[10];
for (int i = 0; i < 10; i++)
ar[i] = r.nextInt(10);
String line = "Send this to the other client";
while(true)
{
try
{
// line = in.readLine();
System.out.println("Enter something");
out.println(keyInput.readLine());
System.out.println(in.readLine());
//out.println(line);
for (int i = 0; i < 10; i++)
System.out.print(ar[i] + " ");
System.out.println();
} // 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