IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Mobile en Java Discussion :

serveur / client en java


Sujet :

Développement Mobile en Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Janvier 2008
    Messages
    57
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations forums :
    Inscription : Janvier 2008
    Messages : 57
    Par défaut serveur / client en java
    salut ;

    j' ai installé un serveur dans mon pc. mais comment je peux connecter directement à partir de java au serveur en utilsant l'adress URL http://127.0.0.1:4223
    je suis debutant en java svp je besoin de vous aide et merci

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Par défaut
    Quel type de serveur ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Janvier 2008
    Messages
    57
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations forums :
    Inscription : Janvier 2008
    Messages : 57
    Par défaut
    c'est un serveur avec la language JAVA.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    package principale;
     
    import java.io.*;
    import java.net.*;
    import java.util.Vector;
     
    import javax.swing.JTextArea;
     
    public class SocketApp {
     
    	public String CurrentDir; //Dossier racine a afficher
    	public Vector<Socket> vectSocket; //Les clients
    	public ServerSocket servSocket; //Mon serveur
    	protected JTextArea texteOut; //Sortie
     
    	public SocketApp(JTextArea texte) {
    		vectSocket = new Vector<Socket>();
    		texteOut = texte;
     
    		try {
    			Print("Ouverture sur le port 4850...");
    			servSocket = new ServerSocket(4850);
    			Print("Serveur en écoute...");
    		} catch (IOException e) {
    			Print("Echec de l'ouverture...");
    			return;
    		}
    		ListenServerSocket listen = new ListenServerSocket();
    		listen.start();
     
     
     
    	}
     
    	public void SetCurrentDir(String dir)
    	{
    		CurrentDir = dir;
    		if (CurrentDir.isEmpty() || CurrentDir.charAt(CurrentDir.length() - 1) != '/') CurrentDir += "/";
    		Print("Répertoire changé : " + dir);
     
    	}
     
    	private String TailleToString(long taille)
    	{
    		String retour = new String();
    		if (taille < 1024) retour = new String(taille + " o");
    		else if (taille < 1024 * 1024) retour = new String(String.valueOf((double) taille / 1024.0)+ " Ko");
    		else if (taille < 1024 * 1024 * 1024) retour = new String(String.valueOf((double)taille / (1024.0 * 1024.0))  + " Mo");
    		else if (taille < 1024 * 1024 * 1024 * 1024) retour = new String(String.valueOf((double)taille / (1024.0 * 1024.0 * 1024.0)) + " Go");
    		if (retour.length() > 8 ) retour = retour.substring(0, 5) + retour.substring(retour.length() - 3);
     
     
            return retour;
    	}
     
    	private synchronized void Print(String monTexte)
    	{
    		texteOut.append(monTexte + "\n");
    		texteOut.setCaretPosition(texteOut.getText().length());
    	}
     
     
     
    	class ListenServerSocket extends Thread
    	{	
     
    		public void run()
    		{
    			while(true)
    			{
    				try {
    					Print("En attente de clients...");
    					Socket sock = servSocket.accept();
    					Print("Client détécté. Ip : " + sock.getInetAddress().getHostAddress() + " - Ajout à la liste.");
    					vectSocket.add(sock);
    					sock = null;
    					ActionSocket action = new ActionSocket(vectSocket.size() - 1);
    					action.start();
    				} catch (IOException e) {
    					Print("L'application n'a pas pu attendre, un problème est survenu...");
    				}
    			}
    		}
    	}
     
     
    	class ActionSocket extends Thread
    	{
    		InputStream inputS;
    		OutputStream outputS;
    		int n;
    		Socket s;
    		public ActionSocket (int numSocket) throws IOException
    		{
    			n = numSocket;
    			s = vectSocket.get(n);
    			inputS = s.getInputStream();
    			outputS = s.getOutputStream();
    		}
     
    		public void run() 
    		{
    			String adressS =  s.getInetAddress().getHostAddress();
     
    			try {
    				Print("Client " +  adressS + " : en attente d'une requête...");
    				while(inputS.available() == 0) //On attends des données
    				{	
    				}
     
    				//Extraction du corps de la requete
    				byte[] tblBytes  = new byte[inputS.available() + 1000];
    				int lus = inputS.read(tblBytes);
    				String requete =  new String(tblBytes,0, lus);
     
    				//Extraction du lien demandé
    				//int posDeb = 4;
    			   // int posFin = requete.indexOf("HTTP") - 1;
    			    //String lien = requete.substring(posDeb, posFin);
     
    				String lien ;
                    lien ="/7777.txt";
    				Print("Client " +  adressS + " : demande : " + lien);
     
     
    				//Transformation Url->String (on enleve les %20 et autres)
    				java.net.URI url = null;
    				try {
    					url = new java.net.URI (lien);
    				} catch (URISyntaxException e) {
    				}
     
    				//On enleve le '/' du début
    				lien =   url.getPath().substring(1);
     
    				//Chemin d'acces demandé
    				File file = new File(CurrentDir + lien);
    				String pathfile = CurrentDir + lien;
     
    				if (file.isDirectory())
    				{
    					//Si c'est un répertoire on affiche le contenu sous format html
     
    					if (pathfile.isEmpty() || pathfile.charAt(pathfile.length() - 1) != '/')
    					{
    						pathfile += "/";
    						lien += "/";
    						file = new File(pathfile);
    					}
     
     
    					Print("Client " +  adressS + " : envoie une vue sur : " + file.getPath());
     
    					String toSendSocket = new String("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>Vue repertoire</title>");
    					toSendSocket += "<BODY BGCOLOR=\"#000000\" text=#00FF00></BODY><b>Repertoire : " + lien + "</b>" + "<br><br>";
     
    					toSendSocket +=  "Dossier : &nbsp;" + "<i>" + "<a href=\"" + "/" + lien +  ".."  + "\">" + ".." + "</a>" + "</i>" + "<br>";
     
    					String[] liste = file.list(new DossierFiltre(pathfile)); //Liste les dossiers
    					for (String elem : liste)
    					{
    						File f = new File(pathfile + elem);
    						System.out.println(lien);
    						toSendSocket +=  "Dossier : &nbsp;" + "<i>" + "<a href=\"" + "/" + lien + f.getName() + "\">" + f.getName() + "</a>" + "</i>" + "<br>";
    					}
    					toSendSocket += "<br>";
     
    					liste = file.list(new FichierFiltre(pathfile)); //Liste les dossiers
    					for (String elem : liste)
    					{
    						File f = new File(pathfile + elem);
    						toSendSocket +=  "Fichier : &nbsp;" + "<i>" + "<a href=\"" +  "/" + lien + f.getName() + "\">" + f.getName() + "</a>" + "</i> - " + TailleToString(f.length())  + "<br>";
    					}
     
    					toSendSocket += "<br> Programme par Zives</head></html>";
    					outputS.write(toSendSocket.getBytes());		//On envoie
    				}
    				else if (file.isFile())
    				{
    					//C'est un fichier on transmet l'entete HTTP de la réponse + les données du fichier
    					Print("Client " +  adressS + " : envoie fichier : " + file.getPath());
     
    					String toSendSocket = new String("HTTP/1.1 200 OK" + "\n" + "Accept-Ranges: " + "bytes" + "\n" + "Content-Length: " + file.length() + "\n" + "Connection: Close" + "\n" + "Content-Type: " + "application/octet-stream" + "\n\n");
    					outputS.write(toSendSocket.getBytes())	;
    					FileInputStream fileStream = new FileInputStream(file);
     
    					byte[] data = new byte[10240];
    					int longueur = 10240;
     
    					while (longueur == 10240)
    					{
    						longueur = fileStream.read(data);
    						outputS.write(data, 0, longueur);
    					}
    					fileStream.close();
    					Print("Client " +  adressS + " : envoie fichier terminé : " + file.getPath());
    				}
    				else
    				{
    					//On sait pas ce que c'est
    					Print("Client " +  adressS + " : requete non valable");
    					String toSendSocket = new String("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>Vue répertoire</title>");
    					toSendSocket += "<BODY BGCOLOR=\"#000000\" text=#00FF00></BODY>La source demandée n'existe pas <br>";
    					toSendSocket += "</head></html>";
    					outputS.write(toSendSocket.getBytes())	;
    				}
     
    				//Le client devient mort
    				outputS.close();
    				inputS.close();
    				s.close();
     
    			} catch (IOException e) {
    				Print("Client " +  adressS + " : l'envoi des données n'a pu être terminé correctement.");
    			}
     
    			vectSocket.remove(n);
    			Print("Client " +  adressS + " : client supprimé.");
     
     
    		}
    	}
     
    	class DossierFiltre implements FilenameFilter
    	{
    		String acces;
    		public DossierFiltre(String chem)
    		{
    			acces = chem;
    		}
    		public boolean accept(File dir, String name) {
    			if (!new File(acces + name).isFile()) return true;
    			return false;
    		}
     
    	}
     
    	class FichierFiltre implements FilenameFilter
    	{
    		String acces;
    		public FichierFiltre(String chem)
    		{
    			acces = chem;
    		}
    		public boolean accept(File dir, String name) {
    			if (new File(acces + name).isFile()) return true;
    			return false;
    		}
     
    	}
     
    }
    mais maintenant je veux telecharger un fichier .txt à partir de ce serveur en utilsant la language JAVA ME (pour les mobiles)*
    et je ne sais pas comment ????

  4. #4
    Membre Expert Avatar de Uther
    Homme Profil pro
    Tourneur Fraiseur
    Inscrit en
    Avril 2002
    Messages
    4 690
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Tourneur Fraiseur

    Informations forums :
    Inscription : Avril 2002
    Messages : 4 690
    Par défaut
    Su qu'apparement ce n'est pas un sevreur Web mais un serveur qui fait une connection directe via un socket, il te faut regarder du coté de la classe:javax.microedition.io.SocketConnection

Discussions similaires

  1. Serveur / Client FTP Java
    Par Benj' dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 02/03/2010, 15h13
  2. Réponses: 3
    Dernier message: 29/08/2009, 00h35
  3. Client serveur UDP en JAVA
    Par caro_caro dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 29/09/2008, 22h16
  4. Serveur codé en Java et client en C++ possible ?
    Par adn013 dans le forum Langage
    Réponses: 3
    Dernier message: 14/05/2007, 23h09
  5. Serveur d'applications Java
    Par foxrol dans le forum Java EE
    Réponses: 3
    Dernier message: 17/05/2003, 00h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo