Server & klient problem

0

Witam

Otóż mój problem wygląda następująco, mam do napisania server, który będzie komunikował się z klientem pobierał od niego jakiś text i zwracał mu ten tekst odwrócony. Znalazłem w pewnej książce podobny server i po odpowiednim przerobieniu działa, przetestowałem na telnecie. [ "exit" ma kończyć program ] Problem mam natomiast z klientem. Oto źródła

server:

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

public class fo {

		public static void main (String [] args) 
		{
		try{
			ServerSocket s = new ServerSocket(13);
			Socket incoming=s.accept();
			try{
				InputStream inStream = incoming.getInputStream();
				OutputStream outStream= incoming.getOutputStream();
				Scanner in = new Scanner(inStream);
				PrintWriter out = new PrintWriter(outStream, true );
				out.println("hello! Enter exit to exit");
				boolean done = false;
				while (!done && in.hasNextLine())
				{
					String line = in.nextLine();
					out.println("echo: "+ new StringBuffer(line).reverse());
					if(line.trim().equals("exit"))
						done = true;
				}
				
			}
			finally
			{
				incoming.close();
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		}
}

i klient:

import java.net.*;
import java.io.*;
import java.util.*;
public class so {
	public static final int PORT=13;
	public static Scanner sc=null;
	public static void main(String[] args) throws IOException
	{
		// Scanner sc = new Scanner(System.in);
	

		InetAddress addr = InetAddress.getByName(null);  
		System.out.println("address: " +addr);  
		Socket sd = new Socket(addr, PORT);
		try
		{
		System.out.println("Socket: " +sd);  
		BufferedReader in = new BufferedReader(new InputStreamReader(sd.getInputStream()));  
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sd.getOutputStream())), true);
		for(int i=0; i <10; i++)      

		{
		out.println("hello" +i);
		String st = in.readLine();
		System.out.println(st);

		}

		

		out.println("END");
		}
		finally

		{

		System.out.println("Closing...");

		sd.close();     //close socket

		}
	}

}

Jak widzicie mój testowy klient wysyła do servera:

	for(int i=0; i <10; i++)      

		{
		out.println("hello" +i);
		String st = in.readLine();
		System.out.println(st);

		}

i efektem jest:

address: localhost/127.0.0.1
Socket: Socket[addr=localhost/127.0.0.1,port=13,localport=1903]
hello! Enter exit to exit
echo: 0olleh
echo: 1olleh
echo: 2olleh
echo: 3olleh
echo: 4olleh
echo: 5olleh
echo: 6olleh
echo: 7olleh
echo: 8olleh
Closing...

Jaki kod trzeba umieścić zamiast ww pętli for żeby to user wpisywał słowa ( i były one odwracane )?

0
private final static String EXIT = "exit";
...
Scanner userInput = new Scanner(System.in);
while (true) {
    String line = userInput.nextLine();
    if (EXIT.equalsIgnoreCase(line)) {
        System.out.println("Bye!");
        break;
    }
    String reverse = in.readLine();
    System.out.println(reverse);
}
0

nie działa = /

teraz kod wygląda tak ( klient ):

import java.net.*;
import java.io.*;
import java.util.*;
public class so {

	private final static String EXIT = "exit";
	public static final int PORT=13;
	public static Scanner sc=null;
	public static void main(String[] args) throws IOException
	{
		// Scanner sc = new Scanner(System.in);
	

		InetAddress addr = InetAddress.getByName(null);   
		System.out.println("address: " +addr); 
		Socket sd = new Socket(addr, PORT);   
		try
		{
		System.out.println("Socket: " +sd);  
		BufferedReader in = new BufferedReader(new InputStreamReader(sd.getInputStream()));   
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sd.getOutputStream())), true);
		Scanner userInput = new Scanner(System.in);
		while (true) {
		    String line = userInput.nextLine();
		    if (EXIT.equalsIgnoreCase(line)) {
		        System.out.println("Bye!");
		        break;
		    }
		    String reverse = in.readLine();
		    System.out.println(reverse);
		}
		

		out.println("END");
		}
		finally

		{

		System.out.println("Closing...");

		sd.close();     //close socket

		}
	}

}

a otrzymuje coś takiego ( " joł " sam wpisałem :) )

address: localhost/127.0.0.1
Socket: Socket[addr=localhost/127.0.0.1,port=13,localport=1577]
joł
hello! Enter exit to exit
joł
joł
jołjoł
joł
joł

nie odwraca wyrazu ( powinno być "łoj" )

0

Na moje to brakuje wysylania wyrazu na serwer. Nie wklejaj kodu bezmyslnie, tylko zastanawiaj sie to co ci wiara podsyla.

0

siedzę nad tym od piątku i nie mam pomysłu więc sorry, łapię co dają= )

edit:

Program nie kończy się po wpisaniu "exit"

edit 2:

Po mojemu ten scanner tutaj coś nie halo, on szwankuje

0

PrintWriter przyjmuje w konstruktorze klase OutputStream wiec zdrowo namotałeś:D
linijka :
String reverse = in.readLine();
System.out.println(reverse);

na mój prosty rozum nie wysłałeś informacji na serwer a oczekujesz jej z powrotem.
Mylą Ci sie chyba strumienie in i System.in i out - System.out a to nie jest wszystko jedno:)
Myśle ze jeśli nie napisałem zrozumiale to wystarczy zdebugować klienta i skminisz szybko co jest nie tak.
Powodzenia!

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