Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it L'avatar di energie
    Registrato dal
    Apr 2003
    Messaggi
    446

    [java]Specificare porta dell'ip del server

    Salve a tutti, ho una domanda su un sorgente che ho scaricato dalla rete:

    client:

    import java.awt.Dimension;
    import java.awt.FileDialog;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Vector;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;

    interface RemoteMetod extends Remote {
    void writefile(Vector text, String name) throws RemoteException;
    }

    class ClientWriter extends UnicastRemoteObject implements RemoteMetod {

    public ClientWriter() throws RemoteException {
    }

    public void writefile(Vector text, String name) {
    try {
    Object[] texti = text.toArray();
    FileOutputStream fos = new FileOutputStream(name);
    for(int i = 0; i < texti.length; i++) {
    fos.write(Integer.parseInt(texti[i].toString()));
    }
    JOptionPane.showMessageDialog(null, "Ricevuto file", "Ricevuto", JOptionPane.INFORMATION_MESSAGE);
    } catch(Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    }
    }

    /**
    * @author Matteo Tomasulo(netarrow)
    */
    public class FileSender extends JFrame implements Runnable {
    private Thread send;
    private JTextField textField;
    private String name;
    private Vector text;

    public static void main(String args[]) {
    FileSender frame = new FileSender();
    frame.setBounds(100, 100, 404, 116);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.show();
    }
    public FileSender() {
    super();
    getContentPane().setLayout(null);
    {
    final JPanel panel = new JPanel();
    panel.setBounds(42, 9, 127, 26);
    getContentPane().add(panel);
    {
    final JLabel label = new JLabel();
    panel.add(label);
    label.setText("Inserire file da inviare");
    }
    }
    {
    final JPanel panel = new JPanel();
    panel.setBounds(174, 5, 182, 35);
    getContentPane().add(panel);
    {
    final JTextField textField = new JTextField();
    textField.setPreferredSize(new Dimension(100, 20));
    panel.add(textField);

    final JFrame parent = this;

    final JButton button = new JButton();
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    FileDialog fd = new FileDialog(parent);
    fd.setVisible(true);
    if(!fd.getDirectory().equals("null") && !fd.getFile().equals("null")) {
    textField.setText(fd.getDirectory() + fd.getFile());
    name = fd.getFile();
    reader.start();
    }
    }

    Thread reader = new Thread(new Runnable() {
    public void run() {
    Vector v = new Vector();
    try {
    File file = new File(textField.getText());
    FileInputStream fis = new FileInputStream(file);
    int i = 0;
    while((i = fis.read()) != -1) {
    v.add(new Integer(i));
    }
    fis.close();
    } catch(Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    text = v;
    }
    });
    });
    panel.add(button);
    button.setText("Cerca");
    }
    }
    {
    final JLabel label = new JLabel();
    label.setHorizontalAlignment(SwingConstants.CENTER );
    label.setBounds(50, 40, 120, 25);
    getContentPane().add(label);
    label.setText("Inserire ip");
    }
    {
    textField = new JTextField();
    textField.setBounds(180, 45, 100, 20);
    getContentPane().add(textField);
    }
    {
    final JButton button = new JButton();
    button.setBounds(285, 40, 65, 25);
    getContentPane().add(button);
    button.setText("Invia");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    send.start();
    }
    });
    }
    setTitle("FileSender");
    setResizable(false);
    send = new Thread(this);
    }


    public void run() {
    try {
    String url = "rmi://" + textField.getText() + "/Server";
    RemoteMetod cw = (RemoteMetod) Naming.lookup(url);
    cw.writefile(text, name);
    JOptionPane.showMessageDialog(null, "File inviato", "Inviato", JOptionPane.INFORMATION_MESSAGE);
    } catch(Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    }
    }



    Server:

    import java.rmi.Naming;

    import javax.swing.JOptionPane;

    class Server {

    public static void main(String args[]) {
    try {
    System.out.print("Eseguendo rmiregistry...");
    Runtime r = Runtime.getRuntime();
    r.exec("rmiregistry");
    Thread.sleep(1500);
    System.out.println("ok");
    ClientWriter rm = new ClientWriter();
    Naming.rebind("Server", rm);
    System.out.println("Server in ascolto");
    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    }
    }


    Come faccio ad indicare la porta da utilizzare per questo programma?

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Non sono ferrato in materia di RMI, però leggendo la documentazione

    http://java.sun.com/j2se/1.5.0/docs/...-server24.html
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it L'avatar di newbie
    Registrato dal
    Dec 2005
    Messaggi
    299
    Se non rocordo male, basta che quando registri il server (e ovviamente anche quando usi la lookup), posponi all'ip orta; ad esempio, per connetterti sulla porta 8080 usi, che ne so, 111.111.111.111:8080
    Svegliati, Neo. Matrix ti possiede...

  4. #4
    Utente di HTML.it L'avatar di energie
    Registrato dal
    Apr 2003
    Messaggi
    446
    mmm si ma il problema stà nel server...di sicuro utilizzerà una porta...ma non capisco quale...forse sarà random...nel client,certo posso utilizzare ip:8000 per connettermi al server,ma è necessario che il server sia attivo su qlla porta

  5. #5
    Utente di HTML.it L'avatar di netarrow
    Registrato dal
    Apr 2004
    Messaggi
    1,425
    la porta è quella rmi, se non sbaglio 1099 è quella di default.

    Ricordati che il file rmiregistry.exe deve essere in esecuzione, in teoria non ci sono problemi visto che lo lancio dalla classe(nelle cartelle per sistemi windows dove ho cpiato il file .exe e creato un wapper del jar), su altri sistemi lo lanci tu prima o lo copi vicino jar eseguibile(o forse funziona anche se lo metti solo nel path per riconoscerlo come comando)

    P.S: con file troppo grandi non mi funzionava

    Inoltre il file registry è della 1.4 che usavo quella volta, se ora hai la 5 devi sostituirlo

    Imparare è un'esperienza, tutto il resto è solo informazione. (Albert Einstein)

  6. #6
    Utente di HTML.it L'avatar di netarrow
    Registrato dal
    Apr 2004
    Messaggi
    1,425
    per cambiare porta quando lanci rmiregistry

    rmiregistry 1000

    Se modifichi la classe server basta che aggiungi il parametro nelle classe Runtime dove lo lancio
    Imparare è un'esperienza, tutto il resto è solo informazione. (Albert Einstein)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.