FAQ | Points | Add... | Recent posts | Search | Register | Log in


[java] Progam(y) typu server - klient problemy


Post new topic  Reply to topic      Main Page -> Forum Index -> Programming Generally -> Beginners Programming -> [java] Progam(y) typu server - klient problemy
Author
Message
iie
Poziom 14
Poziom 14


Joined: 13 Sep 2008
Posts: 199
Location: Łódź

Post#1 Post from the author of the topic 22 Feb 2009 14:12   

[java] Progam(y) typu server - klient problemy


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:
Code:
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:

Code:
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:
Code:
   for(int i=0; i <10; i++)     

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

                }



i efektem jest:
Code:
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 )?
Back to top
   
Google

Google Adsense


Post# Post from the author of the topic 22 Feb 2009 14:12   





Back to top
   
Dżyszla
Poziom 24
Poziom 24


Joined: 15 May 2005
Posts: 4528
Location: Żory (kiedyś Opole)

Post#2 22 Feb 2009 19:32   

Re: [java] Progam(y) typu server - klient problemy


1. To klient komunikuje się z serwerem, a nie odwrotnie.
2. Stworzyłeś samochód, a nie umiesz wynaleźć koła? Tak samo jak pobierasz dane z socketa, tak samo pobiera się dane z klawiatury, lecz tu bazą jest obiekt System (ten sam, do którego przekazujesz tekst do wyświetlenia na ekranie analogicznie, jak przekazujesz tekst do socketu).
Back to top
   
Google

Google Adsense


Post# 22 Feb 2009 19:32   





Back to top
   
mykhaylo
Poziom 12
Poziom 12


Joined: 17 Jan 2009
Posts: 102
Location: Warszawa

Post#3 22 Feb 2009 19:42   

Re: [java] Progam(y) typu server - klient problemy


Możesz to zrobić na przykład tak:
Code:

public static void main(String[] args) {
      InputStreamReader input = new InputStreamReader(System.in);
      BufferedReader in = new BufferedReader(input);
      String line = "";
      try {
         do {
            System.out.println("Prosze wprowadzic dane:");
            System.out.print(">");
            line = in.readLine();
            if(line.equalsIgnoreCase("exit")) {
               System.out.println("Koniec");
               break;
            }
            System.out.println(new StringBuffer(line).reverse().toString());
         } while(true);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

Back to top
   
Google

Google Adsense


Post# 22 Feb 2009 19:42   





Back to top
   
iie
Poziom 14
Poziom 14


Joined: 13 Sep 2008
Posts: 199
Location: Łódź

Post#4 Post from the author of the topic 22 Feb 2009 20:17   

Re: [java] Progam(y) typu server - klient problemy


@ 1. Mały błąd logiczny
@ 2. Programowanie to nie mój konik = ). Staram się coś wykombinować, nie zawsze wychodzi.
3. Problem mój został już rozwiązany

Dodano po 1 [minuty]:

skoro już topik stoi, czy możecie mi wytłumaczyć te wszystkie inputstreamreadery, bufferedreadry etc? Nie mogę załapać o co tam kaman
Back to top
   
mykhaylo
Poziom 12
Poziom 12


Joined: 17 Jan 2009
Posts: 102
Location: Warszawa

Post#5 22 Feb 2009 20:44   

Re: [java] Progam(y) typu server - klient problemy


[1] InputStreamReader
[2] BufferedReader
Back to top
   
Post new topic  Reply to topic      Main Page -> Forum Index -> Programming Generally -> Beginners Programming -> [java] Progam(y) typu server - klient problemy
Page 1 of 1
Similar topics
JAVA (JDBC) java.lang.NullPointerException HELP!!! (3)
Progam od oscysloskopu na ATmedze32. (2)
problemy z formularzem - java (1)
[JAVA] RealJ 3.6 Dodawanie liczb typu double. Skąd ten błąd? (2)
[java] Jak wczytać ciąg typu string na wejscie (12)
Generowanie schematu blokowego, Java Server Page, PHP (5)
[JAVA] Klient TCP wysyła ale nie odbiera wiadomości (2)
C++ i program typu klient-server (6)
JAVA java.lang.NoClassDefFoundError POMOCY! (18)
JAVA- kalkulator / pierwszy program w java (2)

Page generation time: 0.261 seconds


FAQ || Administrator || Moderators || Widgets and banners || Contact
elektroda.pl topic RSS feed