Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

socket problem (connection)...

Thread view: 
marcpirat@yahoo.com - 07 Dec 2005 18:50 GMT
hi

i have problem to connect to server
i run the code and i get:

->connection ok
and after
->not able to connect to server

if i use telnet to connect server, i don't have any problem..

this is the code

public void ConnexionServeur(){
       Socket kkSocket = null;
       PrintWriter out = null;
       BufferedReader in = null;
       try {
           kkSocket = new Socket(getIpServeur(), getPortServeur());
           out = new PrintWriter(kkSocket.getOutputStream(), true);
           in = new BufferedReader(new
InputStreamReader(kkSocket.getInputStream()));

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

           while ((fromServer = in.readLine()) != null){
               String[] words = fromServer.split(":");
               if(words[0].equals("succes"))
                 System.out.println("connection ok");
               else if(words[0].equals("lstClient")){
                 System.out.println("client list: ");
                 for(int i=0;i<words[i].length()-1;i++)
                     System.out.println(words[i] + " ");
               }
               System.out.println("serveur: "+ fromServer);
               fromUser = stdIn.readLine();
               if (fromUser != null) {
                 out.println(fromUser);
               }
           }
           out.close();
           in.close();
           stdIn.close();
           kkSocket.close();
       } catch (UnknownHostException e) {
           System.err.println("host unknow");
           System.exit(1);
       } catch (IOException e) {
           System.err.println("not able to connect to server");
           System.exit(1);
       }
   }

any idea?
Gordon Beaton - 07 Dec 2005 18:26 GMT
> i have problem to connect to server
> i run the code and i get:
>
> ->connection ok
> and after
> ->not able to connect to server

Start by displaying the text of the exception itself
(e.printStackTrace()), not your own message.

Where is the server code?

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Marc Collin - 08 Dec 2005 09:54 GMT
>> i have problem to connect to server
>> i run the code and i get:
[quoted text clipped - 9 lines]
>
> /gordon

more information now... they seem to be a client side problem..

i'm connect to a gateway to get some information to connect to a server...

in my main i do:

client.ConnexionPasserelle();
client.ConnexionServeur();

[code]
public void ConnexionPasserelle(){
       Socket kkSocket = null;
       PrintWriter out = null;
       BufferedReader in = null;
       
       try {
           kkSocket = new Socket(getIpPasserelle(), getPortPasserelle());
           out = new PrintWriter(kkSocket.getOutputStream(), true);
           in = new BufferedReader(new
InputStreamReader(kkSocket.getInputStream()));
           
           BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
           String fromPasserelle;
           String fromUser;
           
           while ((fromPasserelle = in.readLine()) != null) {
               /*Connexion a la passerelle*/
               System.out.println("Passerelle>" + fromPasserelle);
               
               String[] words = fromPasserelle.split(":");
               if (words[0].equals("serveur-info")){
                   setIpServeur(words[1]);
                   setPortServeur(Integer.parseInt(words[2]));
                   System.out.println("Passerelle> " + words[1] + ":" +
words[2]);
               } else if(words[0].equals("erreur")){
                   System.out.println("Passerelle> probleme divers
survenu");
               }
               if (fromPasserelle.equals("Bye")){
                   break;
               }

               fromUser="Bye";
               if (fromUser != null) {
                   out.println(fromUser);
               }
               if(fromUser.equals("Bye")){
                   //ConnexionServeur();  /***********here***********/
                   break;
               }
           }
           out.close();
           in.close();
           stdIn.close();
           kkSocket.close();
           
       } catch (UnknownHostException e) {
           System.err.println("Passerelle Inconnue");
           System.exit(1);
       } catch (IOException e) {
           e.printStackTrace();
           System.err.println("Imposible d'etablir une connexion avec la
passerelle");
           System.exit(1);
       }
   }
[/code]

and now the code to connect to the server
[code]
 public void ConnexionServeur(){
       Socket kkSocket = null;
       PrintWriter out = null;
       BufferedReader in = null;
       try {
           
           kkSocket = new Socket(getIpServeur(), getPortServeur());
           out = new PrintWriter(kkSocket.getOutputStream(), true);
           in = new BufferedReader(new
InputStreamReader(kkSocket.getInputStream()));
           
           BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
           String fromServer;
           String fromUser;
           
           while ((fromServer = in.readLine()) != null){
               System.out.println("serveur> "+ fromServer);
               String[] words = fromServer.split(":");
               if(words[0].equals("succes"))
                   System.out.println("connection reussi");
               else if(words[0].equals("lstClient")){
                   System.out.println("Liste des clients: ");
                   for(int i=0;i<words[i].length()-1;i++){
                       System.out.println(words[i] + " ");
                   }
               }
               System.out.print("client:~>");
               fromUser = stdIn.readLine();
               if (fromUser != null) {
                   out.println(fromUser);
               }
           }
           out.close();
           in.close();
           stdIn.close();
           kkSocket.close();
       } catch (UnknownHostException e) {
           System.err.println("Serveur Inconnue");
           System.exit(1);
       } catch (IOException e) {
           e.printStackTrace();
           System.err.println("Imposible d'etablir une connexion avec le
serveur");
           System.exit(1);
       }
   }
[/code]

the connection seem to stop correctely between client and Passerelle
(gateway)
now i connect to the server i get:

serveur> succes:Connection au serveur reussi
connection reussi
client:~>java.io.IOException: Stream closed
       at
java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
       at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
       at
sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:411)
       at
sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:453)
       at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:183)
       at java.io.InputStreamReader.read(InputStreamReader.java:167)
       at java.io.BufferedReader.fill(BufferedReader.java:136)
       at java.io.BufferedReader.readLine(BufferedReader.java:299)
       at java.io.BufferedReader.readLine(BufferedReader.java:362)
       at Client.ConnexionServeur(Client.java:105)
       at Client.main(Client.java:189)
Imposible d'etablir une connexion avec le serveur

but in my main if i do only

client.ConnexionPasserelle();

don't call ConnectionServeur.....

and uncomment code /**********here*******/

i'm able to connect to the serveur....

any idea?
Gordon Beaton - 08 Dec 2005 09:47 GMT
> client:~>java.io.IOException: Stream closed
>         at
[quoted text clipped - 24 lines]
>
> any idea?

Not really sure, but it looks to me like the exception is caused *not*
by your attempt to connect to the server, but by your attempt to read
from stdIn.

After you connected to the gateway, you closed stdIn (which closes
System.in). When you to connect to the server, you attempt to read
from stdIn again. Even though you created a new BufferedReader for
this, System.in remains closed, so the operation fails.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.