[JAVA] komunikacja klientów przez serwer

0

Witam. Przejrzałem wątki związane z serwerami w javie ale nie trafiłem na nic podobnego.
Problem jest taki. Próbuję napisać program, który komunikowałby klientów przez serwer. Celem ostatecznym jest gra Reversi, może to troszkę wyjaśni dla czego kod w niektórych miejscach jest taki a nie inny ;]

W tej chwili mój serwer odbiera informacje wysyłane przez klientów ale nie mogę nic zdziałać, żeby klienci odbierali informacje z serwera.

Na wszelki wypadek nie wycinam kilku zbędnych rzeczy, które są pozostałością po czymś co przerabiałem

Będę wdzięczny za każdą pomoc.

kody:

FileServer.java

import java.io.*;
import java.net.*;

public class FileServer implements Runnable {
	Socket socket;
	ObjectInputStream ois;	
	ObjectOutputStream oos;


	public FileServer(Socket s) {
		socket = s;
	}

	public void run() {
		try {

			ois = new ObjectInputStream(socket.getInputStream());
			oos = new ObjectOutputStream(socket.getOutputStream());
			while (true) {
				Command cmd = (Command) ois.readObject();
				

				//te ify to pozostałość po próbach, czegoś czego już nie ma

				if (cmd.getCommand() == Command.EXIT) {
					send(oos, new Response(cmd, "Bye"));
					break;
				}
				else if (cmd.getCommand() == Command.LIST) {
					File file = new File(".");
					//send(oos, new Response(cmd, file.list()));
				}
				else if (cmd.getCommand() == Command.GET) {
					String filename = (String) cmd.getCommandArg();
					File file = new File(filename);
					byte buff [] = new byte[(int) file.length()];
					FileInputStream fis = new FileInputStream(file);
					fis.read(buff);
					fis.close();
					//send(oos, new Response(cmd, buff));
				}else {

				// w gore do wyciecia
					
						String v = (String)cmd.getCommandArg();
						System.out.println("Odebrano: " + v );
											
						send (oos, new Response (cmd, "cos odsylam") ) ;
			
				}
				
			}
			ois.close();
			oos.close();
			socket.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(ObjectOutputStream os, Response response) throws IOException {
		os.writeObject(response);
		os.flush();
	}
	
	private Response send(Command cmd) throws Exception {
		oos.writeObject(cmd);
		oos.flush();
		return (Response) ois.readObject();
	}

	public static void main(String args[]) throws IOException {
		ServerSocket ss = new ServerSocket(4444);
		System.out.println("Server is listening on 4444...");
		while (true) {
			Socket s = ss.accept();
			System.out.println("Accepting connection from: "+s.getInetAddress());
			(new Thread(new FileServer(s))).start();
		}
	}
}

FileClient.java


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;

public class FileClient extends Frame implements ActionListener, Runnable {
public Button b1;
public Button b2;
public String line;

	Socket socket;
	ObjectOutputStream oos;
	ObjectInputStream ois;

	public FileClient(String host, int port) throws IOException {
		socket = new Socket(host, port);
		oos = new ObjectOutputStream(socket.getOutputStream());
		ois = new ObjectInputStream(socket.getInputStream());
		
		int sizeX = 200;
		int sizeY = 350;
		setSize(sizeX,sizeY);
		
		Panel top = new Panel();
		top.setLayout(new GridLayout(8, 8));

		//na razie 2 testowe guziczki	
		b1 = new Button("1");
		b1.addActionListener(this);
		b1.setSize(10,10);
		top.add(b1);
		
		b2 = new Button("2");
		b2.addActionListener(this);
		b2.setSize(10,10);
		top.add(b2);
		
		add(top, "Center");
		
		pack();
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event){
		
		if(event.getSource() == b1) {
			System.out.println("guzik 1");
			line = "1";			
			try{
				Command cmd = new Command(Command.TXT, line);
				Response response = send(cmd);
				String name = "imie";
				Command cmd2 = new Command(Command.TXT, name);
				Response response2 = send(cmd2);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(event.getSource() == b2) {
			System.out.println("guzik 2");
			line = "2";
			System.exit(0);
		}
		
	}

	public void go() {
		try {
			//tu byla reszta tego co jest wykomentowane w Serwerze a juz nie ma
			while(true) {
					
				if(line.startsWith("list")) {
				}else {
					//String v = (String)cmd.getCommandArg();
					//System.out.println("Odebrano z serwaera: " + v);
				}				
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void run(){
		try {

			ois = new ObjectInputStream(socket.getInputStream());	
			oos = new ObjectOutputStream(socket.getOutputStream());
			
			while (true) {
				Command cmd = (Command) ois.readObject();
				
				if (cmd.getCommand() == Command.EXIT) {//znowu maly zbyt
					send(oos, new Response(cmd, "Bye"));
					break;
				}
				else {
					
						String v = (String)cmd.getCommandArg();
						System.out.println("Odebrano: z serwera" + v);
										
				}
				}
			ois.close();
			oos.close();
			socket.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(ObjectOutputStream oos, Response response) throws IOException {
		oos.writeObject(response);
		oos.flush();
	}
	
	private Response send(Command cmd) throws Exception {
		oos.writeObject(cmd);
		oos.flush();
		return (Response) ois.readObject();
	}

	public static void main(String args[]) throws IOException {
		(new FileClient("localhost", 4444)).go();
	}
}

Command.java

import java.io.*;

public class Command implements Serializable {
	public final static int LIST 	= 0;
	public final static int GET 	= 1;
	public final static int EXIT 	= 2;
	public final static int TXT 	= 3;

	private int cmd;
	private String arg;

	public Command (int cmd) {
		this.cmd = cmd;
	}

	public Command (int cmd, String arg) {
		this.cmd = cmd;
		this.arg = arg;
	}

	public int getCommand() {
		return cmd;
	}

	public String getCommandArg() {
		return arg;
	}
}

Response.java

import java.io.*;

public class Response implements Serializable {
	private String data;
	private Command cmd;
	public Response (Command cmd, String data) {
		this.cmd = cmd;
		this.data = data;
	}

	public String getData() {
		return data;
	}

	public Command getCommand() {
		return cmd;
	}
}
0

hmm...to chyba jest jakiś znany temat dawany przez prowadzących? Z jakiego przedmiotu to jest? Przecież to plaga jakaś - tego tematu na tym forum były kilogramy a poza tym co z was za studenci? myśmy pomagali sobie wzajemnie a do tego były zebrane progsy na necie ponad 5 lat wstecz....a tu ciul- znowu.....to zaiste spamowanie dobrego forum:)

0

nie wiem czy zauważyłeś SZUKAM BŁĘDU w tym co już napisałem. Nie szukam gotowca i nie liczę na to, że ktoś mi to napisze, liczyłem no to, że ktoś mi wskaże błąd. Może byś to zauważył gdybyś tak się nie spieszył ze zjechaniem kolejnego nooba i przyjrzał się dokładniej temu co napisałem.

Przeglądałem inne tematy związane z serwerami w javie ale nie bardzo mogły mi pomóc, we wszystkich jest to na czym się opierałem pisząc ten kod.

lipkerson napisał(a)

to zaiste spamowanie dobrego forum:)

natomiast Twój post pęka od wartościowych informacji...

Miałem nadzieję, że ktoś przynajmniej rzuci okiem na ten kod zanim zacznie komentować

1 użytkowników online, w tym zalogowanych: 0, gości: 1