Bonjour,
est il possible de se connecter avec les asp.net a du tcp ?
Merci
Bonjour,
est il possible de se connecter avec les asp.net a du tcp ?
Merci
Salut,
Se connecter avec les asp.net a du tcp? Ca veut dire quoi exactement? Tu as TcpClient mais faudrait que tu donnes plus de détails.
A+
"Winter is coming" (ma nouvelle page d'accueil)
Je veux recuperer des datas qui se trouvent sur un server dont le protocole est du tcp.
Le meme code fonctionne en application Console mais impossible avec les asp.net :
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 public string SetServerConnection() { try { server = new TcpClient("00.00.00.00", 1000); networkStream = server.GetStream(); } catch (SocketException) { return "Unable to connect to server"; } return server.Connected.ToString(); } public string SetLogin() { try { if (server.Connected) { input = "Command de login - pwd"; ASCIIEncoding aSCIIEncoding = new ASCIIEncoding(); byte[] message = aSCIIEncoding.GetBytes(input); networkStream.Write(message, 0, message.Length); byte[] bb = new byte[256]; int k = networkStream.Read(bb, 0, bb.Length); for (int i = 0; i < k; i++) { if (bb[i] != 16 && bb[i] != 17) { result += Convert.ToChar(bb[i]); } } return result; } return "You are not connected"; } catch (Exception e) { return e.Message; } }
J'ai l'impression que je recupere uniquement que le stream de la SetServerConnection et non de SetLogin.
Je comprends plus rien !!!!
Alors en commençant par ici: http://www.developpez.net/forums/d71...t/#post4185461
Le serveur:
Le client ASP.Net:
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using Neptune.Model; using System.Xml.Serialization; using System.Xml; using System.IO; namespace Neptune.TcpListner { class CustomTcpListner { static void Main(string[] args) { TcpListener server = null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing using (NetworkStream stream = client.GetStream()) { int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); HelloEntity hello = null; XmlSerializer serializer = new XmlSerializer(typeof(HelloEntity)); XmlReader xr = XmlReader.Create(new StringReader(data)); if (serializer.CanDeserialize(xr)) hello = (HelloEntity)serializer.Deserialize(xr); // Process the data sent by the client. data = string.Format("Hello {0} you are born on {1}. You are {2}m tall.", hello.Name, hello.Birth.ToShortDateString(), hello.Height); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } } }
Et ça marche
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 using System; using System.IO; using System.Net.Sockets; using System.Text; using System.Xml.Serialization; using Neptune.Model; namespace WebApplication { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HelloEntity hello = new HelloEntity() { Birth = new DateTime(1918, 11, 11), Height = 1.72, Name = "Toto" }; XmlSerializer serializer = new XmlSerializer(typeof(HelloEntity)); MemoryStream mem = new MemoryStream(); serializer.Serialize(mem, hello); string str = Encoding.UTF8.GetString(mem.ToArray()); Byte[] data = System.Text.Encoding.ASCII.GetBytes(str); Connect("127.0.0.1", 13000, data); } private void Connect(String server, Int32 port, Byte[] data) { TcpClient client = null; try { client = new TcpClient(); client.SendTimeout = 3000; client.Connect(server, port); if (client.Connected) { using (NetworkStream stream = client.GetStream()) { stream.Write(data, 0, data.Length); data = new Byte[256]; String responseData = String.Empty; Int32 bytes = stream.Read(data, 0, data.Length); responseData = Encoding.ASCII.GetString(data, 0, bytes); Response.Write(string.Format("Received: {0}", responseData)); } } } catch (ArgumentNullException e) { Response.Write(string.Format("ArgumentNullException: {0}", e)); } catch (SocketException e) { Response.Write(string.Format("SocketException: {0}", e)); } finally { client.Close(); } } } }
"Winter is coming" (ma nouvelle page d'accueil)
Partager