Sockety - dlaczego program nie działa?

0

Korzystając z tutoriala Oracla:
http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

próbuję utworzyć echo socket.

Kod:

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

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            System.out.println("A");
            echoSocket = new Socket("192.168.1.5", 7);
            System.out.println("B");
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: 192.168.1.5.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: 192.168.1.5.");
            System.exit(1);
        } catch(SecurityException e){
        	System.err.println("Security");
        	System.exit(1);
        }
        
        System.out.println("połączono");

	BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
	String userInput;

	while ((userInput = stdIn.readLine()) != null) {
	    out.println(userInput);
	    System.out.println("echo: " + in.readLine());
	}

	out.close();
	in.close();
	stdIn.close();
	echoSocket.close();
    }
}
 

Gdzie 192.168.1.5 jest lokalnym IP w mojej sieci. Program po wydrukowaniu "A" w linii 15 zawiesza się, nie drukując już "B".
Co jest nie tak?

0

A próbowałaś się podłączyć jakimś innym programem pod ten port? Bardzo możliwe, że jakiś firewall wycina transfer lub nie ma takiej usługi zaimplementowanej.

0

Próbowałam innych numerów portów - nie działa.

0

To spróbuj wyłączyć całkowicie firewalla

0

Masz kod EchoSerwera (domyślnie pracuje na porcie 5555, dawno nie napotkałem hosta, który pracował jako echo na porcie 7). Uruchom ten serwer i sprawdź swój program.

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

public class EchoServer
{
    public static void main(String[] args)
    {
        ServerSocket ss=null;
        Socket s=null;
        PrintWriter out = null;
        InputStreamReader is=null;
        String pytanie="";
        int port=5555;
        boolean done=false;
        if (args.length>0)
        {
            try
            {
                port=Integer.parseInt(args[0]);
            }
            catch (Exception e)
            {
            }
        }
        try
        {
            ss=new ServerSocket(port);
            System.out.println("Czekam (port: "+port+") - polecenie  quit  kończy pracę serwera");
        }
        catch (Exception e)
        {
            System.out.println("Błąd podczas tworzenia gniazda \n"+e);
            System.exit(0);
        }
        while (!done)
        {
            try
            {
                s=ss.accept();
                System.out.println("Odebrano połączenie z "+s.getInetAddress());
                is=new InputStreamReader(s.getInputStream());
                BufferedReader br=new BufferedReader(is);
                pytanie=br.readLine();
                System.out.println(pytanie);
                if (pytanie.equals("quit"))
                {
                    done=true;
                }
                else
                {
                    out = new PrintWriter(s.getOutputStream());
                    out.println(pytanie);
                    out.flush();
                }
            }
            catch (Exception e)
            {
                System.out.println("Błąd :"+e);
                System.exit(0);
            }
        }
        try
        {
             s.close();
        }
        catch (Exception e)
        {
        }
    }
}
0

Nie mam włączonego firewalla.

Dzięki za kod ;)
Program EchoClient działa, ale tylko dla ip komputera z którego jest uruchomiony i tylko gdy uruchomiony jest serwer :(

Na dodatek odbija tylko 1 echo, potem przestaje działać.

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