begin process at 2010 02 09 13:38:17
  Trouver un code source :
 
dans
 
Accueil > Forum > 

Archive Java

 > 

Archives

 > 

Au secours

 > 

Dialogue Server / Client


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

Dialogue Server / Client

dimanche 20 juin 2004 à 19:18:10 | Dialogue Server / Client

Kaktus

J'aimerai faire communiquer 4 PC avec un serveur.
Apparement, ça ne marche pas et je vois pas pourquoi...
Merci d'avance.
Voici les codes sources:

SERVEUR - Class SERVER

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

public class Server
{
final static int PORT = 5100;
public int threadNb;
public ServerThread thread1, thread2, thread3, thread4;

public Server() throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
threadNb = 1;

try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
System.err.println("Could not listen on port: 5100.");
System.exit(-1);
}

while (listening) {
switch (threadNb) {
case 1:
thread1 = new ServerThread(serverSocket.accept(), threadNb);
thread1.start();
case 2:
thread2 = new ServerThread(serverSocket.accept(), threadNb);
thread2.start();
case 3:
thread3 = new ServerThread(serverSocket.accept(), threadNb);
thread3.start();
case 4:
thread4 = new ServerThread(serverSocket.accept(), threadNb);
thread4.start();
listening = false;
serverSocket.close();
}
threadNb++;
}

String name1 = thread1.getAlias();
thread3.send("B" + name1 + "\n");
String name2 = thread2.getAlias();
thread4.send("B" + name2 + "\n");
String name3 = thread3.getAlias();
thread1.send("B" + name3 + "\n");
String name4 = thread4.getAlias();
thread2.send("B" + name4 + "\n");

thread1.setThreads(thread1, thread2, thread3, thread4);
thread2.setThreads(thread1, thread2, thread3, thread4);
thread3.setThreads(thread1, thread2, thread3, thread4);
thread4.setThreads(thread1, thread2, thread3, thread4);

thread1.go();
thread2.go();
thread3.go();
thread4.go();
}
}

SERVER - Class SERVERTHREAD

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

public class ServerThread extends Thread {

private Socket socket = null;
private int threadNb;
private ServerThread thread1, thread2, thread3, thread4;

public ServerThread(Socket socket, int threadNb)
{
super("ServerThread");
this.socket = socket;
this.threadNb = threadNb;
}

public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

String input, output;

while ((input = in.readLine()) != null) {
String s0 = input.substring(0, 0);
if (s0 == "A") {
// int i = 1;
// String s;
// String name = new String("");
// while ((String s = input.substring(i, i)) != "\n") {
// name = name + s;
// i++;
// }
} else if (s0 == "F") {
String text = input.substring(0, 4);
switch (threadNb) {
case 1: thread3.send(text);
case 2: thread4.send(text);
case 3: thread1.send(text);
case 4: thread2.send(text);
}
} else if (s0 == "O") {
String text = input.substring(0, 4);
switch (threadNb) {
case 1: thread2.send(text);
case 2: thread1.send(text);
case 3: thread4.send(text);
case 4: thread3.send(text);
}
}

}
out.close();
in.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String text)
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(text);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}

public String getAlias()
{
send("A");
String name = new String("");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
for (int i = 1; i < 50; i++) {
String s = in.readLine().substring(i, i);
if (s != "\n") {name = name + s;}
else return name;
}
} catch (IOException e) {
e.printStackTrace();
}
return name;
}

public void setThreads(ServerThread thread1, ServerThread thread2, ServerThread thread3, ServerThread thread4)
{
this.thread1 = thread1;
this.thread2 = thread2;
this.thread3 = thread3;
this.thread4 = thread4;
}

public void go()
{
//
}
}

CLIENT - Class THREADSOCKET

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

public class ThreadSocket
{
private Socket socket;
private ChessEngine chessEngine;

public ThreadSocket(Socket socket)
{
this.socket = socket;
}

public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input, output;

while ((input = in.readLine()) != null) {
String s0 = input.substring(0, 0);
if (s0 == "A") {
out.println("A" + chessEngine.getPlayer().getName() + "\n");
} else if (s0 == "B") {
for (int i = 1; i < 50; i++) {
String name = new String("");;
String s = in.readLine().substring(i, i);
if (s != "\n") {name = name + s;}
else chessEngine.getPlayer().setFriendName(name);
}
} else if (s0 == "F") {
String s1 = input.substring(1, 1);
String s2 = input.substring(2, 2);
String s3 = input.substring(3, 3);
String s4 = input.substring(4, 4);

if (s1 == "9") {
Piece piece = new Piece(chessEngine.getPlayer().getColor(), Integer.parseInt(input));
chessEngine.getPlayer().addPiece(piece);
}
} else {
String s1 = input.substring(1, 1);
String s2 = input.substring(2, 2);
String s3 = input.substring(3, 3);
String s4 = input.substring(4, 4);
if (s1 == "9") {
Piece piece = new Piece(Integer.parseInt(s2), (chessEngine.getPlayer().getColor()+1)%2);
int[] to = new int[] {Integer.parseInt(s3), Integer.parseInt(s4)};
chessEngine.getChessboard().setPiece(piece, to);
} else if (s1 == "8") {
//promotion
} else {
int[] from = new int[] {Integer.parseInt(s1), Integer.parseInt(s2)};
int[] to = new int[] {Integer.parseInt(s3), Integer.parseInt(s4)};
int[][] move = new int[][] {from, to};
chessEngine.movePiece(move);
}
}
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void setChessEngine(ChessEngine chessEngine)
{
this.chessEngine = chessEngine;
}
}
lundi 21 juin 2004 à 08:47:09 | Re : Dialogue Server / Client

wargre


Deja y'a quoi comme erreur?

Sinon substring(0,0) ca renvoie toujours rien!!!!


Cette discussion est classée dans : input, string, socket, thread2, thread1


Répondre à ce message

Sujets en rapport avec ce message

Connection refused [ par Skyffer3 ] Bonjour a tous, j'ai un petit probleme de connexion avec socket. Je m'explique, voici tout d'abord mon code, l ets tres court :import java.io.*;import Java : lenteur avec Objet envoyer par Socket avec KeyEvent [ par nemata ] Bonsoir, je développe actuellement un Bomberman en java et en réseau 2 joueur, je peut actuellement envoyer un objet léger avec simplement la position enchainer des requetes http dans un client java [ par squezzyb ] Bonjour a tous. J'essaye de developper un client java pour envoyerdes requetes HTTP à un serveur JBOSS. Ce qui coince, c'est qu'à partir de la seconde boutton [ par KERKENNAH ] slt ;j'ai 2 bouttons 1 "demarrer"qui permet de demarrer un seveur qui reste en ecoute sur un port 514 puis affiche les messages recus ainsi lorsque on ENREGISTRER DANS UN .TXT PLUSIEURS CHAMPS (JTXTFIELD, JTXTAREA) [ par guigre06 ] Voila je bosse en ce moment sur une interface graphique créée a partir de JBUILDER X.On s'intéressera uniquement aux 5 jtxtfield d'en haut et au jtxta recuperer une String protected [ par ObiWanKennedy ] Bonjour, voici mon problème: J'utilise une JTextArea http://java.sun.com/j2se/1.4.2/docs/api/index.html jtable+rafraichir [ par KERKENNAH ] bonjour à tous je suis un etudiant et j'ai besoin d'aide àproposde jtableon faite j'ai une application qui joue le role d'un serveur qui recoit des tr Conversion d'un tableau ASCII en STRING [ par neena ] bonjour, je voudrai savoir comment faire pour convertir mon tableau qui contient du code ASCII en STRING voila mon code (le cryptage marche mais pas l Probleme de Collection [ par onini ] Alors voila j'ai fait une class TextHtml mais losque ke je compile avec javac il me sort une erreur qui est : TextHtml.java:11: expected     Linked Base de données et requête [ par tari ] Bonour a tousVoila je développe un programme pour gérer des clients.J'ai créé une classe ConnexionBase:import java.io.*;import java.sql.*; public clas


Nos sponsors


Appels d'offres

Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,468 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales