Bonjour,
j'aimerai savoir: comment utilise-t-on l'équivalent de Winsock (en VB) en Java? J'ai tenté d'utiliser des sockets sans succès voici le code:
je ne parviens pas à lire le serveur alors que le code suivant en VBA fonctionne
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 import java.net.*; import java.io.*; class LMSTelnet { public static void main( String[] args ) { int port = Integer.parseInt(args[1]); ClientTelnet cltlnt= new ClientTelnet(args[0],port); } } class ClientTelnet extends Thread{ protected Socket s; protected BufferedReader in; protected BufferedReader inKey; protected PrintStream out; public ClientTelnet(String host,int port){ //constructeur lance méthode "écoute serv" et "écoute clavier" try{ s=new Socket(host,port); in=new BufferedReader(new InputStreamReader(s.getInputStream())); }catch(IOException e){ System.out.println("Prob Socket"); System.exit(1); } start(); KeyListener(); } public void KeyListener(){ //lit le cliavier et l'envoi au serv String ligne; try{ inKey = new BufferedReader( new InputStreamReader( System.in ) ); out = new PrintStream( s.getOutputStream() ); while( !(ligne = inKey.readLine()).equals("xxxxxx") ) { ligne = HexToString( ligne ); out.println( ligne ); } System.exit(1); } catch( IOException e ) { System.out.println( "probleme d'entree/sortie" ); } } public static String HexToString(String hexa) { // On vérifie la longeur de la chaine : if (hexa.length() % 2 != 0) { throw new RuntimeException("Taille incorrecte"); } // Création du buffer de lecture : StringBuffer buf = new StringBuffer(); // On parcours la chaine par bloc de 2 caractères : for (int pos = 0; pos<hexa.length(); pos+=2) { // On récupère la chaine courante : String substring = hexa.substring(pos, pos+2); // Que l'on convertit en int puis en char : char c = (char) Integer.parseInt(substring, 16); // Et on ajoute le char au buffer : buf.append(c); pos++; } return buf.toString(); } public void run(){ //lit le coté serv et l'affiche String ligne; try{ while(true){ sleep(1000); ligne=in.readLine(); if(ligne==null)break; System.out.println(ligne); } }catch(Exception e){ System.out.println("connexion perdue"); } } }
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 Option Compare Database Option Explicit Dim winsock As New MSWinsockLib.winsock 'Ce code est un code VBA acess pour un formulaire simple contenant 2 texte box : Texte1 et Text2 Private Sub boutonConnect_Click() If winsock.State <> sckConnected Then winsock.RemoteHost = "192.168.0.1" winsock.RemotePort = 2111 winsock.Connect Else MsgBox "Vous êtes déjà connecté" End If End Sub Private Sub winsock_DataArrival(ByVal bytesTotal As Long) Dim strData winsock.GetData strData, vbString Me.Text2.Text = strData End Sub Private Sub Envoyer_Click() Dim chaine_a_transmettre As String chaine_a_transmettre = convertion_hexa_vers_string(Me.Texte1) If Me.Texte1 <> "" Then If winsock.State = sckConnected Then winsock.SendData chaine_a_transmettre Else MsgBox "Non connecté au serveur" End If Else MsgBox "Veuillez tapez le nom!" End If End Sub Function convertion_hexa_vers_string(ByVal chaine As String) As String Dim a As Long Dim reponse As String For a = 1 To Len(chaine) Step 3 reponse = reponse + Chr$(Val("&h" + Mid$(chaine, a, 2))) Next convertion_hexa_vers_string = reponse End Function
Partager