Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561

    Errore in un client/server

    Ho realizzato i seguenti client e server:

    public class Server {
    private static int port=2000;
    private static int T=0;
    private Socket client;
    private DataInputStream input;
    private DataOutputStream output;


    private boolean isDigit(String n) throws NumberFormatException{
    try{
    Integer.parseInt(n);
    return true;
    }catch(NumberFormatException nfe){
    return false;
    }
    }

    public Server(int port){
    this.port=port;
    }
    public void Start() throws IOException {
    while(true){
    try{
    System.out.println ("Server in partenza sulla porta " + port);
    ServerSocket server = new ServerSocket(port);
    System.out.println ("Server partito sulla porta " + server.getLocalPort() ) ;
    System.out.println ("In attesa di connessioni...");
    client = server.accept ();
    System.out.println ("Richiesta di connessione da " + client.getInetAddress ());
    input =new DataInputStream(new BufferedInputStream (client.getInputStream()));
    output = new DataOutputStream( new BufferedOutputStream(client.getOutputStream()));
    do{
    try{
    byte c=input.readByte();
    switch(c){
    case 10:
    System.out.println("mmmmmm");
    default:
    System.out.println("zzzzzzzzz");
    }
    }catch(IOException ioex){
    System.err.println(ioex.getMessage());
    }
    }while(true);
    }catch(EOFException ex){
    System.out.println("Client ha terminato la connessione");
    }catch(IOException io){
    io.printStackTrace();
    }finally{
    input.close();
    output.close();
    client.close();
    }
    }
    }

    }
    mentre il client è:
    public class SocketClient {


    private boolean autorizzazione=false;
    private Socket socket=null;
    private DataInputStream input = null;
    private DataOutputStream output = null;
    private static boolean DEBUG = false;

    public SocketClient() {
    }

    public void run(GUI frame){
    FileConfig f=new FileConfig();
    try{
    frame.writeTextArea("Trying to connect.....");
    f.readProperties();
    socket=new Socket(InetAddress.getByName("localhost"),f.getPor t());
    input =new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    output = new DataOutputStream( new BufferedOutputStream(socket.getOutputStream()));
    output.write(10);
    output.flush();
    }catch (IOException e) {
    System.err.println((e.getMessage()));
    if(e.getMessage().equals("Connection refused: connect"))
    frame.writeTextArea("Connection refused");
    else
    frame.writeTextArea("Problem with configuration file");
    }catch(Exception eof){
    System.err.println(eof.getMessage());
    }
    finally{
    try{
    output.close();
    input.close();
    socket.close();
    }catch(IOException e){
    System.err.println(e.getMessage());
    }
    }
    }
    public boolean getAutorizzazione(){
    return autorizzazione;
    }
    }

    quello che ottengo è che sul server viene stampato il seguente messeggio di errore:

    null


    A che è dovuto questo?

    tulipan

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    Ho provato a toglere del codice e sono arrivato ad una situazione del genere:

    Server

    public class Server {
    private static int port=2000;
    private static int T=0;
    private Socket client;
    private DataInputStream input;
    private DataOutputStream output;
    private String idFile="2000";



    public Server(int port){
    this.port=port;
    }
    public void Start() throws IOException {
    while(true){
    try{
    System.out.println ("Server in partenza sulla porta " + port);
    ServerSocket server = new ServerSocket(port);
    System.out.println ("Server partito sulla porta " + server.getLocalPort() ) ;
    System.out.println ("In attesa di connessioni...");
    client = server.accept ();
    System.out.println ("Richiesta di connessione da " + client.getInetAddress ());

    }catch(EOFException ex){
    System.out.println("Client ha terminato la connessione");
    }catch(IOException io){
    io.printStackTrace();
    }finally{

    client.close();
    }
    }
    }
    }

    CLIENT

    public class SocketClient {


    private boolean autorizzazione=false;
    private Socket socket=null;
    private DataInputStream input = null;
    private DataOutputStream output = null;
    private static boolean DEBUG = false;

    public SocketClient() {
    }

    public void run(GUI frame){
    FileConfig f=new FileConfig();
    try{
    frame.writeTextArea("Trying to connect.....");
    f.readProperties();
    socket=new Socket(InetAddress.getByName("localhost"),f.getPor t());
    }catch (IOException e) {
    System.err.println((e.getMessage()));
    if(e.getMessage().equals("Connection refused: connect"))
    frame.writeTextArea("Connection refused");
    else
    frame.writeTextArea("Problem with configuration file");
    }catch(Exception eof){
    System.err.println(eof.getMessage());
    }
    finally{
    try{
    socket.close();
    }catch(IOException e){
    System.err.println(e.getMessage());
    }
    }
    }
    public boolean getAutorizzazione(){
    return autorizzazione;
    }
    }

    il risultato è il seguente errore sul server:
    java.net.BindException: Address already in use: JVM_Bind

    perchè?

    tulipan

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    Ho modificato il client così:

    public class SocketClient {
    private boolean autorizzazione=false;
    private Socket socket=null;
    private DataInputStream input = null;
    private DataOutputStream output = null;
    private static boolean DEBUG = false;

    public SocketClient() {
    }

    public void run(GUI frame){
    FileConfig f=new FileConfig();
    try{
    frame.writeTextArea("Trying to connect.....");
    f.readProperties();
    socket=new Socket(InetAddress.getByName("localhost"),f.getPor t());
    }catch (IOException e) {
    System.err.println((e.getMessage()));
    if(e.getMessage().equals("Connection refused: connect"))
    frame.writeTextArea("Connection refused");
    else
    frame.writeTextArea("Problem with configuration file");
    }catch(Exception eof){
    System.err.println(eof.getMessage());
    }
    }

    public boolean getAutorizzazione(){
    return autorizzazione;
    }
    }

    Ho tolto in pratica il blocco finally e quando il server è giu funziona correttamente nel senso che stampa nella textarea Connect refused, il problema è adesso quando il server è attivo che produce sempre lo stesso errore:java.net.BindException: Address already in use: JVM_Bind

    non so perchè
    tulipan

  4. #4
    Il messaggio di errore indica che quella porta è occupata da un'altro processo... Che porta stai utilizzando?

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    problema risolto ...grazie lo stesso


    ho riavviato il pc

    tulipan

  6. #6
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328

    Moderazione

    Non sei proprio "nuovo" del forum, eppure continui a postare il codice senza utilizzare i tag [ CODE ]... In questo modo erndi difficile la vita agli utenti che intendono leggerlo.

    Ti consiglio di cominciare ad utilizzarlo...


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  7. #7
    Comunque il problema si è risolto perché riavviando il pc il processo che ti impegnava la porta è terminato. Non riavviare il pc ogni volta che ti succede... usa Ctrl + Alt + Canc e termina manualmente il processo.

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.