problème pour envoyer avec des sockets
Dernière réponse : dans Programmation
J'ai un projet qui a pour but de faire une interaction entre un robot et une station. La sation doit envoyer des commandes au robot qui lui doit pouvoir les lire et renvoyer des données à la station.
Je n'arrive pas à envoyer les donnees à la station. Car je cherche à envoyer des entiers (correspondant à la position du robot, à la température et à la pression) du robot à la station.
Le problème c'est que j'ai pu envoyer de la station au robot une chaine correspindant aux commandes avec des BufferReader( on utilisait System.in pour lire la chaine entrée au clavier et des ObjectInPutStream pour recevoir l'objet ) mais ici je ne sais pas comment faire avec des entiers non entrés au clavier.
Voici la classe station
public class station {
static carte c;
static commande interpreteur = new commande(c);
public station() {
}
public static void main(String[] args) {
try{
String chaine="";
String fin = "quit";
Socket a=new Socket("localhost",6000);
System.out.println("connexion etablie");
ServerSocket srv=new ServerSocket(6001);
System.out.println("port 6001 cree");
Socket Sk=srv.accept();
System.out.println("serveur accepté");
PrintWriter out=new PrintWriter(new OutputStreamWriter(a.getOutputStream()));
BufferedReader clin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("entrez vos commandes pour guider le robot: ");
chaine = clin.readLine();
out.println(chaine);
recoitinfo();
System.out.print("fin de connexion");
srv.close();
out.close();
clin.close();
}
catch(Exception e){
e.printStackTrace();
}
}
// c'est ici qu'il y a un problème
public static void recoitinfo(){
try{int x;
Socket a=new Socket("localhost",6000);
ObjectInputStream don = new ObjectInputStream(a.getInputStream());
x= don.readInt();
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
}
public class robot {
static carte Carte = new carte();
static commande interpreteur = new commande(Carte);
static ServerSocket srv;
static int x=16;
static int y=16;
public static void main(String[] args) {
String fin = "quit";
try {
srv=new ServerSocket(6000);
System.out.println("port 6000 cree");
Socket Sk=srv.accept();
System.out.println("serveur accepte");
Socket a=new Socket("localhost",6001);
System.out.println("connexion 6001 établie");
BufferedReader srvin=new BufferedReader(new InputStreamReader(Sk.getInputStream()));
StringBuffer s=new StringBuffer(srvin.readLine());
avance(s);
informations(x,y,s);
srvin.close();
srv.close();
}
catch (Exception e){
System.out.println("erreur");
}
}
public static void avance(StringBuffer s){
String fin = "quit";
interpreteur.Interprete(s.toString());
}
public static void informations(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
y=interpreteur.ordonnee(s.toString());
Carte.affiche_données(x,y);
}
public static int abs(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
return x;
}
// le probleme se trouve aussi ici
public static void envoiinfo(StringBuffer s){
try{
Socket a=new Socket();
a=srv.accept();
ObjectOutputStream donnee=new ObjectOutputStream(a.getOutputStream());
donnee.writeInt(abs(x,y,s));
System.out.println("donnee envoyée");
donnee.close();
}catch(Exception e){}
}
}
Je n'arrive pas à envoyer les donnees à la station. Car je cherche à envoyer des entiers (correspondant à la position du robot, à la température et à la pression) du robot à la station.
Le problème c'est que j'ai pu envoyer de la station au robot une chaine correspindant aux commandes avec des BufferReader( on utilisait System.in pour lire la chaine entrée au clavier et des ObjectInPutStream pour recevoir l'objet ) mais ici je ne sais pas comment faire avec des entiers non entrés au clavier.
Voici la classe station
public class station {
static carte c;
static commande interpreteur = new commande(c);
public station() {
}
public static void main(String[] args) {
try{
String chaine="";
String fin = "quit";
Socket a=new Socket("localhost",6000);
System.out.println("connexion etablie");
ServerSocket srv=new ServerSocket(6001);
System.out.println("port 6001 cree");
Socket Sk=srv.accept();
System.out.println("serveur accepté");
PrintWriter out=new PrintWriter(new OutputStreamWriter(a.getOutputStream()));
BufferedReader clin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("entrez vos commandes pour guider le robot: ");
chaine = clin.readLine();
out.println(chaine);
recoitinfo();
System.out.print("fin de connexion");
srv.close();
out.close();
clin.close();
}
catch(Exception e){
e.printStackTrace();
}
}
// c'est ici qu'il y a un problème
public static void recoitinfo(){
try{int x;
Socket a=new Socket("localhost",6000);
ObjectInputStream don = new ObjectInputStream(a.getInputStream());
x= don.readInt();
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
}
public class robot {
static carte Carte = new carte();
static commande interpreteur = new commande(Carte);
static ServerSocket srv;
static int x=16;
static int y=16;
public static void main(String[] args) {
String fin = "quit";
try {
srv=new ServerSocket(6000);
System.out.println("port 6000 cree");
Socket Sk=srv.accept();
System.out.println("serveur accepte");
Socket a=new Socket("localhost",6001);
System.out.println("connexion 6001 établie");
BufferedReader srvin=new BufferedReader(new InputStreamReader(Sk.getInputStream()));
StringBuffer s=new StringBuffer(srvin.readLine());
avance(s);
informations(x,y,s);
srvin.close();
srv.close();
}
catch (Exception e){
System.out.println("erreur");
}
}
public static void avance(StringBuffer s){
String fin = "quit";
interpreteur.Interprete(s.toString());
}
public static void informations(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
y=interpreteur.ordonnee(s.toString());
Carte.affiche_données(x,y);
}
public static int abs(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
return x;
}
// le probleme se trouve aussi ici
public static void envoiinfo(StringBuffer s){
try{
Socket a=new Socket();
a=srv.accept();
ObjectOutputStream donnee=new ObjectOutputStream(a.getOutputStream());
donnee.writeInt(abs(x,y,s));
System.out.println("donnee envoyée");
donnee.close();
}catch(Exception e){}
}
}
Autres pages sur : probleme envoyer sockets
Lassé par la pub ? Créez un compte
Salut
Je ne vois pas trop où est le problème.
Déjà, tu peux envoyer tes entiers dans une chaine et ensuite convertir en entier. (méthode parseInt() ) si t'as un problème avec ça.
Ensuite, pourquoi ne pas faire un truc dans le genre :
Nas'
Je ne vois pas trop où est le problème.
Déjà, tu peux envoyer tes entiers dans une chaine et ensuite convertir en entier. (méthode parseInt() ) si t'as un problème avec ça.
Ensuite, pourquoi ne pas faire un truc dans le genre :
// a est ta Socket (client) BufferedOutputStream bos = new BufferedOutputStream(a.getOutputStream()); PrintWriter os = new PrintWriter(bos,false); // envoyer os.print(15); // envoyer 15 os.flush();
Nas'
J'ai un autre probleme, ma fonction abs me renvoi un entier, je le met dans une variable z, puis je mats ce z dans le out.println(). Mais le prg s'arrête au niveau du abs() il doit donc y avoir un prblème à ce niveau.
Voici le prg coté robot:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z;
z=abs(x,y,s);
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
prg coté station:je ne sais pas si ca va marcher
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
ObjectInputStream don = new ObjectInputStream(a.getInputStream());
StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
Voici le prg coté robot:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z;
z=abs(x,y,s);
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
prg coté station:je ne sais pas si ca va marcher
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
ObjectInputStream don = new ObjectInputStream(a.getInputStream());
StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
ce sont des paramètres(x et y sont des entiers, s est un StringBuffer)et ils sont utilisés ds une autre classe (classe commande)
public static int abs(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
return x;
}
voici ma classe commmande:
public void Interprete(String chaine) {
boolean fini=false;
for(int i=0;i<chaine.length();i++)
{
if((chaine.charAt(i)=='n')||(chaine.charAt(i)=='N'))
{ x--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");/*fini=false;*/}}
else if((chaine.charAt(i)=='s')||(chaine.charAt(i)=='S'))
{x++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='e')||(chaine.charAt(i)=='E'))
{y++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='o')||(chaine.charAt(i)=='O'))
{y--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else {System.out.println("erreur de commande le robot retourne à sa case de départ");i=chaine.length()-1;x=16;y=16;}
}
}
public int abscisse(String chaine){
x=16;
Interprete(chaine);
return x;
}
public int ordonnee(String chaine){
y=16;
Interprete(chaine);
return y;
}
public static int abs(int x,int y,StringBuffer s){
x=interpreteur.abscisse(s.toString());
return x;
}
voici ma classe commmande:
public void Interprete(String chaine) {
boolean fini=false;
for(int i=0;i<chaine.length();i++)
{
if((chaine.charAt(i)=='n')||(chaine.charAt(i)=='N'))
{ x--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");/*fini=false;*/}}
else if((chaine.charAt(i)=='s')||(chaine.charAt(i)=='S'))
{x++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='e')||(chaine.charAt(i)=='E'))
{y++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='o')||(chaine.charAt(i)=='O'))
{y--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else {System.out.println("erreur de commande le robot retourne à sa case de départ");i=chaine.length()-1;x=16;y=16;}
}
}
public int abscisse(String chaine){
x=16;
Interprete(chaine);
return x;
}
public int ordonnee(String chaine){
y=16;
Interprete(chaine);
return y;
}
je vai revoir ma fonction qui renvoi l'entier mais par contre je n'arrive pas à recevoir cet entier de l'autre côté je ne sais pas trop quoi mettre. en fait je n'arive pas à lire l'entier.
voici le côté qui recoit l'entier:
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
BufferedReader don=new BufferedReader(new InputStreamReader(Sk.getInputStream())); StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
voici le côté qui envoie l'entier:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z;
z=8;
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
voici le côté qui recoit l'entier:
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
BufferedReader don=new BufferedReader(new InputStreamReader(Sk.getInputStream())); StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(Exception e){e.printStackTrace();}
}
voici le côté qui envoie l'entier:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z;
z=8;
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
je t'envoie ma classe commande où sont calculés mes entiers :
public class commande {
char n,N,s,S,e,E,o,O;
int x=16,y=16;
carte c;
/** Creates a new instance of commande */
public commande(carte unecarte) {
c = unecarte;
}
public void Interprete(String chaine) {
for(int i=0;i<chaine.length()-1;i++)
{
if((chaine.charAt(i)=='n')||(chaine.charAt(i)=='N'))
{ System.out.println("yes3");x--;System.out.println(x);if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='s')||(chaine.charAt(i)=='S'))
{x++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='e')||(chaine.charAt(i)=='E'))
{y++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='o')||(chaine.charAt(i)=='O'))
{y--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else {System.out.println("yes4");System.out.println("erreur de commande le robot retourne à sa case de départ");i=chaine.length()-1;x=16;y=16;}
}
// System.out.println("Le point se trouve en : ligne"+x+" colonne"+y);
}
public int abscisse(String chaine){
x=16;
Interprete(chaine);
return x;
}
public int ordonnee(String chaine){
y=16;
Interprete(chaine);
return y;
}
}
et je n'arrive toujours pa s à envoyer et revevoir un entier si vous pouviez m'envoyer un modèle de code Merci.
public class commande {
char n,N,s,S,e,E,o,O;
int x=16,y=16;
carte c;
/** Creates a new instance of commande */
public commande(carte unecarte) {
c = unecarte;
}
public void Interprete(String chaine) {
for(int i=0;i<chaine.length()-1;i++)
{
if((chaine.charAt(i)=='n')||(chaine.charAt(i)=='N'))
{ System.out.println("yes3");x--;System.out.println(x);if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='s')||(chaine.charAt(i)=='S'))
{x++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='e')||(chaine.charAt(i)=='E'))
{y++;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else if((chaine.charAt(i)=='o')||(chaine.charAt(i)=='O'))
{y--;if(c.verif(x,y)==true){System.out.println("impossible obstacles");}}
else {System.out.println("yes4");System.out.println("erreur de commande le robot retourne à sa case de départ");i=chaine.length()-1;x=16;y=16;}
}
// System.out.println("Le point se trouve en : ligne"+x+" colonne"+y);
}
public int abscisse(String chaine){
x=16;
Interprete(chaine);
return x;
}
public int ordonnee(String chaine){
y=16;
Interprete(chaine);
return y;
}
}
et je n'arrive toujours pa s à envoyer et revevoir un entier si vous pouviez m'envoyer un modèle de code Merci.
je n'ai pas envie de tout refaire mon prg. DOnc si y avait moyen d'avoir un modèle pour pouvoir envoyer des données(entiers) et de les recevoir ça m'arrangerait . Un modèle du genre ci dessus mais celui là ne marchepas :
coté reception:
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
BufferedReader don=new BufferedReader(new InputStreamReader(a.getInputStream()));
StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(java.io.IOException e){e.printStackTrace();}
}
coté envoi:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z=8;
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
coté reception:
public static void recoitinfo(){
try{
Socket a=new Socket("localhost",6000);
BufferedReader don=new BufferedReader(new InputStreamReader(a.getInputStream()));
StringBuffer s=new StringBuffer(don.readLine());
System.out.println(s);
System.out.println("donnee reçu");
don.close();
}
catch(java.io.IOException e){e.printStackTrace();}
}
coté envoi:
public static void envoiinfo(){
try{
Socket a=new Socket("localhost",6001);
PrintWriter out=new PrintWriter(new BufferedOutputStream(a.getOutputStream()));
int z=8;
out.println(z);
out.flush();
out.close();
}catch(Exception e){}
}
j'i réussi à envoyer en mettant les entiers dans des StringBuffer par contre est ce normal si j'envoie une ligne de commande assez long qu'il y a un problème:
voici le problème
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
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 station.main(station.java:52)
voici le problème
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
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 station.main(station.java:52)
Lassé par la pub ? Créez un compte
- Contenus similaires :
- ForumProbleme avec les socket en C
- ForumProbleme outlook 2003 sur windows 7 envoie de messages
- ForumSocket UDP : envoyer et recevoir
- ForumProblème d'envoi d'SMS
- ForumProblème d'envoi de message sur Free
- ForumProblème: Envoie de formulaire PHP vers Mysql
- Forumproblème d'envoi de mail
- ForumProblème d'envoi de mail sous Outlook 2003
- Forumproblème outlook express envoi/reception
- Voir plus