Witam. Mam takie problem chciałem zrobić grę w trybie tekstowym ale mam problem z przesyłaniem danych. Próbowałem przerobić czat który wcześniej miałem napisany ale jakoś marnie mi idzie. Może ktoś będzie w stanie pomóc?
Klient
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.*;
public class halma {
public static void main(String[] args) {
int id=-1;
try{
// ustanawianie polaczenia z serwerem
Socket socket = new Socket("localhost", 2080);
System.out.println("Connected To Server ...");
//Pobieranie strumieni do gniazda
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
//Pobieranie strumienia do standardowego urzadzenia wejscia(klawiatury)
//i buforowanie danych z niego przychodzacych
int k = 0;
StringBuffer sb = new StringBuffer();
while((k=in.read())!=-1 && k!='\n')// czytanie po jednym bajcie
sb.append((char)k); // zamiana bajtu na char i wstawianie do bufora
id = Integer.parseInt(sb.toString().trim());
Thread t1 = new TextReader(socket,in,out,id);
while(true){
//FAZA 2. CZYTANIE DANYCH ZE STRUMIENIA I WYŚWIETLANIE NA KONSOLI
//Czytanie danych ze strumienia wejsciowego (od serwera)
k = 0;
sb = new StringBuffer();
while((k=in.read())!=-1 && k!='\n')// czytanie po jednym bajcie
sb.append((char)k); // zamiana bajtu na char i wstawianie do bufora
//pobieranie danych z bufora i obcinanie pustych znaków
String data = sb.toString().trim();
if(data.toLowerCase().equals("exit")){
//W pzypadku gdy odebrano 'exit' zamykamy strumienie, polaczenie oraz aplikacje
in.close();
out.close();
socket.close();
System.exit(0);
}
System.out.println(data);
//KONIEC FAZA 2.
}
// obsluga wyjatkow
}catch(UnknownHostException uhe){
System.err.println(uhe);
}catch(IOException ioe){
System.err.println(ioe);
}
}
}
class TextReader extends Thread
{
Socket socket; InputStream in; OutputStream out;
BufferedReader fromKeyboard = new BufferedReader(new InputStreamReader(System.in));
int id = -1;
TextReader(Socket is, InputStream iis, OutputStream ios,int i){
socket=is;
in=iis;
out=ios;
id = i;
start();
}
public void run(){
int x=0;
int y=0;
int[][] plansza = new int[10][10];
for (int i=0; i<3; i++)
{
for (int j=7; j<10; j++)
{
plansza[i][j]=1;
}
}
for (int i=7; i<10; i++)
{
for (int j=0; j<3; j++)
{
plansza[i][j]=2;
}
}
while(!interrupted()){
try{
while(x!=11 & y!=11)
{
for (int i=0; i<10; i++)
{
System.out.print("\r");
for (int j=0; j<10; j++)
{
System.out.print("|"+plansza[i][j]+"|");
}
}
System.out.print("\r");
System.out.print("Który pionek chcesz przesunąć?");
String pierwsza = JOptionPane.showInputDialog ("podaj pierszwą wspołrzędną");
String druga = JOptionPane.showInputDialog ("podaj drugą wspołrzędną");
int a = Integer.parseInt(pierwsza);
int b = Integer.parseInt(druga);
if(plansza[a][b]!= 0)
{
System.out.print("\r");
System.out.print("Przesówasz pionek o współrzędnych X:"+a+" Y:"+b);
System.out.print("\r");
System.out.print("Gdzie chcesz przesunąć ten pionek?");
pierwsza = JOptionPane.showInputDialog ("podaj pierszwą wspołrzędną");
druga = JOptionPane.showInputDialog ("podaj drugą wspołrzędną");
x = Integer.parseInt(pierwsza);
y = Integer.parseInt(druga);
if((Math.abs(a-x)>1) || (Math.abs(b-y)>1))
{
System.out.print("\r");
System.out.print("Nie można wykonac takiego ruchu");
}
else
{
if (plansza[x][y]==0)
{
plansza[x][y]= plansza[a][b];
plansza[a][b] = 0;
}
else
{
if ((plansza[x][y]==1) || (plansza[x][y]==2))
{
if(((a-x)<0) && ((b-y)==0))
{
x++;
if (plansza[x][y]==0)
{
plansza[x][y]= plansza[a][b];
plansza[a][b] = 0;
System.out.print("warunek 1");
}
else
{
System.out.print("\r");
System.out.print("nie można wykonać takiego ruchu");
}
}
if(((a-x)>0) && ((b-y)==0))
{
x--;
if (plansza[x][y]==0)
{
plansza[x][y]= plansza[a][b];
plansza[a][b] = 0;
System.out.print("warunek 2");
}
else
{
System.out.print("\r");
System.out.print("nie można wykonać takiego ruchu");
}
}
if(((a-x)==0) && ((b-y)>0) )
{
y--;
if (plansza[x][y]==0)
{
plansza[x][y]= plansza[a][b];
plansza[a][b] = 0;
System.out.print("warunek 3");
}
else
{
System.out.print("\r");
System.out.print("nie można wykonać takiego ruchu");
}
}
if(((a-x)==0) && ((b-y)<0) )
{
y++;
if (plansza[x][y]==0)
{
plansza[x][y]= plansza[a][b];
plansza[a][b] = 0;
System.out.print("warunek 4");
}
else
{
System.out.print("\r");
System.out.print("nie można wykonać takiego ruchu");
}
}
}
}
}
}
else
{
System.out.print("\r");
System.out.print("Na tym polu nie ma pionka do przesunięcia");
}
}
int [] data = new int [2];
data[0]=x;
data[1]=y;
out.write(data.getBytes());
out.write("\r\n".getBytes());
/*if(data.toLowerCase().equals("exit")){
in.close();
out.close();
socket.close();
System.exit(0);
}*/
}catch(IOException ioe){
System.err.println(ioe);
}
}
}
}
Serwer
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiServer{
Vector<ClientThread> v = new Vector<ClientThread>();
int i=0;
void runServer() throws IOException{
ServerSocket server = new ServerSocket(2080);
System.out.println("Server run ... ");
while(true){
Socket socket = server.accept();
System.out.println("New client");
ClientThread thread = new ClientThread(socket,i);
v.addElement(thread);
i++;
}
}
void sendToAll(byte[] data){
for(int i=0; i<v.size(); i++)
v.elementAt(i).send(data);
}
void sendTo(byte[] data,int id){
v.elementAt(id).send(data);
}
public static void main(String[] args){
try{
new MultiServer().runServer();
}catch(IOException e){
System.out.println(e);
}
}
class ClientThread extends Thread{
Socket socket;
InputStream in;
OutputStream out;
int id=-1;
ClientThread(Socket socket,int i)throws IOException{
this.socket = socket;
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
id=i;
start();
}
public void run(){
try{
out.write((""+id).getBytes());
out.write("\r\n".getBytes());
while(true){
int k = 0;
StringBuffer sb = new StringBuffer();
while((k=in.read())!=-1 && k!='\n')
sb.append((char)k);
String data = sb.toString().trim();
Boolean flAll = true;
if(flAll){
sendToAll(sb.toString().getBytes());
System.out.println("Send to all");
}
}
}catch(IOException e){
System.out.println(e);
}
}
void send(byte[] data){
try{
out.write(data);
out.write("\r\n".getBytes());
}catch(IOException e){
System.out.println(e);
}
}
}
}