Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-16-2008, 07:40 PM
Member
 
Join Date: Feb 2008
Posts: 5
stessie is on a distinguished road
Help Java!!!!!!!!!!!!!!!!!
Bonjour,j'ai des difficultes à resoudre un problème Client/serveur,.il s'agit d'ecrire un serveur pour un chat de tel sorte que le client envoie un message au serveur et le serveur le renvoie à tous ceux qui sont connectés en ajoutant le nom de celui qui le lui a envoyé devant le message.J'ai essayé d'ecrire ce code mais je ne sais pas pourquoi ça ne compile pas et meme quand je l'ai d'abord compilé,le serveur ne reçevait pas de message.Help,c'est vraiment urgent que vous m'aidiez.voici le code que j'ai essayé d'ecrire.


package Chat;

import java.io.*;
import java.net.*;
import java.util.*;
public class ServerChat {
private ArrayList<Chatteur> listChatteur = null;
private ServerSocket serv = null;
static int port = 6667;
//ArrayList<Chatteur> copieList=null //n'est pas à sa place

private class Chatteur {
private Socket socket;
private OutputStream sockOut;
public Chatteur(Socket sock) throws IOException {
socket = sock;
sockOut = socket.getOutputStream();
}
public Socket getSocket() { return socket; }
public OutputStream getSockOut() { return sockOut; }
}


public static void main(String args[]) {
try{
ServerChat server = new ServerChat();
server.go();
} catch (Exception e) {
System.err.println("erreur server : "+e.getMessage());
}


}

public ServerChat() throws IOException {
serv = new ServerSocket(port);
listChatteur = new ArrayList();
while (true){
Chou client = new Chou();
client.start();
}
}

public void go() throws IOException {
for(; {
Socket socket = serv.accept();
System.out.println("connection de : "+socket);

synchronized(listChatteur) {
listChatteur.add(new Chatteur(socket));
}
}
}
}
private class Chou extends Thread{
public void run(){
BufferedReader sockIn = null;
String message = null;
ArrayList<Chatteur> copieList = null;
byte[] buffer = new byte[1024];
OutputStream sockOut = null;

sockIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
try {
message = sockIn.readLine()+"\n";
} catch (IOException e) {
}
synchronized(listChatteur) {
copieList = (ArrayList<Chatteur>)listChatteur.clone();
}
buffer = message.getBytes();
for (Chatteur chatteur : copieList) {
try {
sockOut = chatteur.getSockOut();
System.out.println("Diffusion d'un message du socket : "+message+"/"+chatteur.getSocket());
sockOut.write(buffer, 0, message.length());
sockOut.flush();
} catch (IOException e2) {
listChatteur.remove(chatteur);
}
}
}
}
}




Le resultat doit etre de cette forme


connection de : Socket[addr=/127.0.0.1,port=59177,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=59177,localport=6667] : stella : je
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=59177,localport=6667] : stella : suis
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
connection de : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=46547,localport=6667] : toto : oui
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=59177,localport=6667] : stella : java
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=46547,localport=6667] : toto : forums
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=46547,localport=6667] : toto : ainsi
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=59177,localport=6667] : stella : est il
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]
diffusion d'un message du socket : Socket[addr=/127.0.0.1,port=46547,localport=6667] : toto : merci
envoi au socket : Socket[addr=/127.0.0.1,port=59177,localport=6667]
envoi au socket : Socket[addr=/127.0.0.1,port=46547,localport=6667]

sachant que :

Le client fait :
- le port utilisé est 6667
- le client saisit des messages en console et les envoie au serveur
- il envoie en caractère son_pseudo : message\n sur le socket et flush le tampon
- simultanément lit les messages qui arrivent et les affichent en console
- quand le client saisit q pour quitter, il envoie le message "au revoir" et ferme proprement
http://www.java-forums.org/images/smilies/confused.gif
tester le client marche avec telnet -e $ localhost 6667
merci de votre aide
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-16-2008, 08:43 PM
Member
 
Join Date: Mar 2008
Posts: 2
Thewhitelynx is on a distinguished road
Loquor Latine
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-17-2008, 01:11 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Please try to post your question in English.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-17-2008, 02:12 PM
Member
 
Join Date: Feb 2008
Posts: 5
stessie is on a distinguished road
i have resoved it!!!!
sorry ,i didn't know the site was in english because i'm a quite new member but i've finally resolved it .There was a problem on the thread i used but it's ok now.thanks a lot
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-19-2008, 07:57 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Nice to here that you have clear-up your issue.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 11:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org