Dzień Dobry,
mam klasę serwera i klasę klienta. Odpalam w jednym terminalu serwer a w dwóch następnych terminalach uruchamiam klasę klienta. Tak, żeby klienci mogli się między sobą komunikować. Na początek podaję pliki źródłowe tychże klas:
Klient:
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String[] args) throws IOException
{
Socket s1 = new Socket("localhost", 8189);
try
{
InputStream sIn = s1.getInputStream();
OutputStream sOut = s1.getOutputStream();
Scanner in = new Scanner(sIn);
PrintWriter out = new PrintWriter(sOut, true);
Scanner standardInput = new Scanner(System.in);
String serverMessage = in.nextLine();
int place = Integer.parseInt(serverMessage);
System.out.println("Place: " + place);
while(true)
{
if (place == 1)
{
System.out.print("Wpisz wiadomość: ");
String inStd = standardInput.next();
System.out.println("Wpisales: " + inStd);
out.println(inStd);
System.out.println("Przeciwnik wykonuje ruch. Czekaj na swoją kolej!");
serverMessage = in.nextLine();
System.out.println("Wiadomosc przeciwnika: " + serverMessage);
}
else
{
System.out.println("Przeciwnik wykonuje ruch. Czekaj na swoją kolej!");
serverMessage = in.nextLine();
System.out.println("Wiadomosc przeciwnika: " + serverMessage);
System.out.print("Wpisz wiadomość: ");
String inStd = standardInput.next();
System.out.println("Wpisales: " + inStd);
out.println(inStd);
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Serwer:
import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadedEchoHandler implements Runnable
{
private Socket firstClient;
private Socket secondClient;
public ThreadedEchoHandler(Socket firstClient, Socket secondClient)
{
this.firstClient = firstClient;
this.secondClient = secondClient;
}
public void run()
{
try
{
try
{
/** Wejscie - wyjscie */
InputStream firstInStream = firstClient.getInputStream();
OutputStream firstOutStream = firstClient.getOutputStream();
InputStream secondInStream = secondClient.getInputStream();
OutputStream secondOutStream = secondClient.getOutputStream();
Scanner firstIn = new Scanner(firstInStream);
PrintWriter firstOut = new PrintWriter(firstOutStream, true);
Scanner secondIn = new Scanner(secondInStream);
PrintWriter secondOut = new PrintWriter(secondOutStream, true);
/** */
firstOut.println("1");
secondOut.println("2");
boolean done = false;
while(!done)
{
String firstLine = firstIn.nextLine();
if (firstLine.trim().equals("BYE"))
{
done = true;
secondOut.println("Przeciwnik zrezygnował!");
break;
}
secondOut.println(firstLine);
String secondLine = secondIn.nextLine();
if (secondLine.trim().equals("BYE"))
{
done = true;
firstOut.println("Przeciwnik zrezygnował!");
break;
}
firstOut.println(secondLine);
}
}
finally
{
System.out.println("Polączenie przerwane");
firstClient.close();
secondClient.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
I teraz mój problem jest następujący:
Gdy jeden z użytkowników czeka na tekst klienta, i gdy wpiszę jakiś ciąg znaków z klawiatury to gdy przyjdzie jego kolej na wpisanie właściwego tekstu, to ten stary, wcześniej wpisany (w złym momencie) tekst wczyta się i zostanie wysłany drugiemu klientowi. Ja bym chciał aby było to nie możliwe, tzn. gdy jeden z klientów czeka i wpisze z klawiatury jakieś znaki to one nie powinny się liczyć. Poprawne są tylko te które są wpisywane w odpowiednim miejscu, tzn. tylko wtedy gdy ma wyświetlone
"Wpisz wiadomość: "
Próbowałem coś porobić z metodą hasNext(), ale ona mi blokuje działanie programu i czeka na jakieś dane z strumienia wejściowego. Mam nadzieję że zrozumiecie mój problem, jeżeli nie piszcie a postaram się to w inny sposób wytłumaczyć.
Pozdrawiam Kysiek.