Jump to content

Envoyer data PHP ==> Java ?


Recommended Posts

Salut a tous,

Après avoir suivi moulte tutoriel sur le web je suis maintenant bloqué ..

Mon serveur JAVA ne recoi absolument rien! I need help :(

ps: le serveur est hoster sur mon PC, la page PHP est hoster sur mon serveur web

Voila les codes:

PHP:

		//send data to java		$PORT = 20222;		$HOST = "85.68.xx.xx"; Mon IP public		$sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket				or die("error: could not create socket\n");		$succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket				or die("error: could not connect to host\n");		$text = "Hello, Java!"; //the text we want to send to the server		socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket				or die("error: failed to write to socket\n");		$reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket				or die("error: failed to read from socket\n");		echo $reply;

JAVA:

	public static void Connection() throws Exception	{		int port = 20222;		ServerSocket listenSock = null; //the listening server socket		Socket sock = null;			 //the socket that will actually be used for communication		try {						listenSock = new ServerSocket(port);			Main.ToEventsLog("Listening on the port:" + port);			while (true) {	   //we want the server to run till the end of times				sock = listenSock.accept();			 //will block until connection recieved				BufferedReader br =	new BufferedReader(new InputStreamReader(sock.getInputStream()));				BufferedWriter bw =	new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));				String line = "";				while ((line = br.readLine()) != null) {					bw.write("PHP said: " + line + "\n");					Main.ToEventsLog("Message from Server: " + line);					bw.flush();				}				//Closing streams and the current socket (not the listening socket!)				bw.close();				br.close();				sock.close();			}		} catch (IOException ex) {			ex.printStackTrace();		}            	}
Link to comment
Share on other sites

Tu utilise ton adresse publique donc j'en deduis que tu cherche a y acceder depuis l'exterieur.

Tu as redirige le port 20222 sur ton routeur ? ouvert le port sur le firewall ?

ca a marche un jours ? en local ?

Ahhh je crois bien que j'ai oublier cette étape !

J'ai pas encore tenter en local

je vais essayer tout ca merci !

(Je suis encore debutant ..)

YES!!

Ca a marcher! merci infiniment !!!!!!!!!!!!!!!!!!!!!!!!!!! :)

Link to comment
Share on other sites

Envoyer le socket sur un 2e host ?

Tu veux (cote client) réutiliser le socket pour te connecter a un 2 serveur ?

ou (cote serveur), tu n'arrive pas a voir un 2e client connecte ?

Je vais assumer que le but c'est d'avoir plusieurs client en même temps.

Actuellement ton implémentation va bloquer l’écoute du serveur et gérer le client actuellement connecte.

le moyen le plus simple d'avoir plusieurs client cote serveur c'est de créer un thread par client.

en gros sur
sock = listenSock.accept();

tu crée un nouveau thread avec sock en argument et tu gère la logique de ce nouveau socket dans le thread.

une fois le thread crée, la boucle logique d’écoute du serveur va pouvoir revenir sur son écoute du réseau.

J'ai une implementation en C++/Qt mais je ne fais pas de java.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...