Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Problema Client-Server

  1. #1

    Problema Client-Server

    Ciao a tutti,
    ho un problema, ho provato a fare un semplice programmino per far comunicare un client con un server...il tutto funziona in locale o comunicando con un altro pc all'interno della rete domestica, ma quando provo ad utilizzare un ip esterno mi da un connection timeout...qualcuno mi potrebbe aiutare?
    Posto di seguito il codice dei 2 file:

    TCPEchoClient.java:

    codice:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class TCPEchoClient
    {
    	private static InetAddress host;
    	private static String hostName;
    	private static final int PORT = 1234;
    		
    	public static void main(String[] args)
    	{		
    		
    		try
    		{
    			host = InetAddress.getByName(//"IP.ESTERNO.PC");  // <- Così non funziona
    			//host = InetAddress.getLocalHost();    // <- Così funziona
    			hostName = host.getHostName();
    			System.out.println(hostName);
    		}
    		
    		catch(UnknownHostException uhEx)
    		{
    			System.out.println("Host ID not found!");
    			System.exit(1);
    		}
    		
    		accessServer();
    	}
    	
    	private static void accessServer()
    	{
    		Socket link = null; //Step 1.
    		
    		try
    		{
    			
    			link = new Socket(host,PORT); //Step 1.
    			Scanner input =	new Scanner(link.getInputStream()); //Step 2.
    			PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 2.
    			//Set up stream for keyboard entry...
    			Scanner userEntry = new Scanner(System.in);
    			String message, response;
    			
    			do
    			{
    				System.out.print("Enter message: ");
    				message = userEntry.nextLine();
    				output.println(message); //Step 3.
    				response = input.nextLine(); //Step 3.
    				System.out.println("\nSERVER> "+response);
    			}
    			
    			while (!message.equals("***CLOSE***"));
    		}
    		
    		catch(IOException ioEx)
    		{
    			ioEx.printStackTrace();
    		}
    		finally
    		{
    			try
    			{
    				System.out.println(
    				"\n* Closing connection... *");
    				link.close(); //Step 4.
    			}
    			
    			catch(IOException ioEx)
    			{
    				System.out.println(
    				"Unable to disconnect!");
    				System.exit(1);
    			}
    		}
    	}
    }
    il problema sorge quando inserisco un indirizzo IP come parametro del metodo InetAddress.getByName() all'interno della main del Client.

    Ecco infine il codice del server che non da problemi:

    TCPEchoServer.java:

    codice:
    //Server that echoes back client's messages.
    //At end of dialogue, sends message indicating number of
    //messages received. Uses TCP.
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    
    public class TCPEchoServer
    {
    	private static ServerSocket servSock;
    	private static final int PORT = 1234;
    	
    	public static void main(String[] args)
    	{
    		System.out.println("Opening port...\n");
    		try
    		{
    			servSock = new ServerSocket(PORT); //Step 1.
    		}
    		catch(IOException ioEx)
    		{
    			System.out.println(	"Unable to attach to port!");
    			System.exit(1);
    		}
    		do
    		{
    			handleClient();
    		}
    		while (true);
    	}
    		
    	private static void handleClient()
    	{
    		Socket link = null; //Step 2.
    		
    		JFrame frame = new JFrame();
    		JTextField textField = new JTextField(20);
    		
    		final int FRAME_WIDTH = 300;
    		final int FRAME_HEIGHT = 300;
    		
    		frame.setTitle("Ricezione messaggi server");
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		try
    		{
    			link = servSock.accept(); //Step 2.
    			Scanner input =	new Scanner(link.getInputStream());//Step 3.
    			PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 3.
    			int numMessages = 0;
    			String message = input.nextLine(); //Step 4.
    			
    			while (!message.equals("***CLOSE***"))
    			{
    				System.out.println("Message received. - " + message);
    				numMessages++;
    				output.println("Message " + numMessages + ": " + message); //Step 4.
    				// Aggiunge al componente frame il messaggio
    				
    				textField.setText(message);
    				textField.setEditable(false);
    				frame.add(textField);
    				
    				frame.setVisible(true);
    				
    				message = input.nextLine();
    				
    			}
    			
    			output.println(numMessages + " messages received.\n");//Step 4.
    		}
    		
    		catch(IOException ioEx)
    		{
    			ioEx.printStackTrace();
    		}
    		
    		finally
    		{
    			try
    			{
    				System.out.println(
    				"\n* Closing connection... *");
    				link.close(); //Step 5.
    			}
    			
    			catch(IOException ioEx)
    			{
    				System.out.println(
    				"Unable to disconnect!");
    				System.exit(1);
    			}
    		}
    	}
    }
    Vi prego aiutatemi, non so piu'dove sbattere la testa

  2. #2
    Ok ho capito che il problema era la porta chiusa dal firewall del router...
    Ora il problema e' un altro, non c'e' un modo per far si che non si debba sbloccare nessuna porta?
    (come alcune applicazioni tipo i vari messenger che per scambiare messaggi o file non necessitano di sbloccare nessuna porta sul router)

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2006
    Messaggi
    31

    !!!

    Ankio ho il tuo stesso problema!!

    tu hai risolto?

    se trovo la solutione te la posto.. ce la faremo!!

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.