Probleme socket

Fermé
Hacker#1 Messages postés 76 Date d'inscription mardi 6 janvier 2009 Statut Membre Dernière intervention 20 avril 2010 - 21 oct. 2009 à 15:29
nabil07 Messages postés 140 Date d'inscription vendredi 14 septembre 2007 Statut Membre Dernière intervention 10 avril 2014 - 21 oct. 2009 à 23:08
Bonjour,
j ai un tp qui propose de faire un programme serveur qui compte les caracteres recus et renvoie le total courant au programme client a travers outputstream de sa socket z chaque fois qu un caratere est recu
mon code
server.java
package tt;

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


public class server {
public static void main(String args[]) throws IOException{
ServerSocket s=new ServerSocket(20000);
System.out.println("serveur en ecoute sur le port "+
s.getLocalPort());
Socket conn= s.accept();
System.out.println("connexion reçue depuis"+
		conn.getRemoteSocketAddress());
PrintStream out = new PrintStream(conn.getOutputStream());
InputStream in=conn.getInputStream();
while(true)
{   String str = in.readLine();
    System.out.println(str); 
	}


		conn.close();
	System.out.println("fin du flux de donnees");	


}
}


client.java
package tt;

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

public class client {
	public static void main(String args[]) throws IOException{
Socket s=new Socket("localhost",20000);
PrintStream out= new PrintStream(s.getOutputStream());
InputStream in=s.getInputStream();
String str="Salut";
int len=str.length();
System.out.println(str+""+len);

str=in.readLine();   

s.close();
	}	
}

a l'execution de client j
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method readLine() is undefined for the type InputStream

	at tt.client.main(client.java:15)

et server
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method readLine() is undefined for the type InputStream

	at tt.server.main(server.java:18)

aider moi svp

2 réponses

Hacker#1 Messages postés 76 Date d'inscription mardi 6 janvier 2009 Statut Membre Dernière intervention 20 avril 2010
21 oct. 2009 à 20:44
pas de proposition
0
nabil07 Messages postés 140 Date d'inscription vendredi 14 septembre 2007 Statut Membre Dernière intervention 10 avril 2014 64
21 oct. 2009 à 23:08
Bonsoir,

ton problème réside dans l'utilisation du flux d'entrée InputStream, car ce dernier ne possède pas une méthode pour lire toute une ligne.

voici une proposition de code :

Code client

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

public class client {
	public static void main(String args[]) throws IOException{
		Socket s=new Socket("localhost",20000);
		PrintStream out= new PrintStream(s.getOutputStream());
		InputStream in=s.getInputStream();
		String str="Salut";
		int len=str.length();
		System.out.println(str+" "+len);
		out.println(str+""+len);
		//str=in.readLine();   
		s.close();
	}	
}



Code serveur :
import java.io.*;
import java.net.*;


public class server {
	public static void main(String args[]) throws IOException{
		ServerSocket s=new ServerSocket(20000);
		System.out.println("serveur en ecoute sur le port "+s.getLocalPort());
		Socket conn= s.accept();
		System.out.println("connexion reçue depuis"+conn.getRemoteSocketAddress());
		PrintStream out = new PrintStream(conn.getOutputStream());
		InputStream in=conn.getInputStream();
		//while(true){
			int c;
            String str = "";
			while((c=in.read()) != -1)
            {
				str += (char)c;
            }
			System.out.println(str); 
			conn.close();
			System.out.println("fin du flux de donnees");	
		//}
	}
}



bonne soirée.
0