Από εχθές ξεκίνησα να ασχολούμε λίγο με το node js. Έφτιαξα έναν server με τον εξής κώδικα που βρήκα στα διάφορα tutorials
μετά έφτιαξα και ένα client σε c#Κώδικας:net = require('net') // Supports multiple client chat application // Keep a pool of sockets ready for everyone // Avoid dead sockets by responding to the 'end' event\ var sockets = []; // Create a TCP socket listener var s = net.Server(function (socket) { // Add the new client socket connection to the array of // sockets sockets.push(socket); // 'data' is an event that means that a message was just sent by the // client application socket.on('data', function (msg_sent) { // Loop through all of our sockets and send the data for (var i = 0; i < sockets.length; i++) { // Don't send the data back to the original sender if (sockets[i] == socket) // don't send the message to yourself continue; // Write the msg sent by chat client sockets[i].write(msg_sent); } }); // Use splice to get rid of the socket that is ending. // The 'end' event means tcp client has disconnected. socket.on('end', function () { var i = sockets.indexOf(socket); sockets.splice(i, 1); }); }); s.listen(9000); console.log('System waiting at http://localhost:9000');
Όταν τρέχω στον υπολογιστή μου τον σέρβερ μέσω της γραμμής εργαλείων "node index.js" και μετά εκτελώ τον client τότε αυτός συνδέεται κανονικά στο 127.0.0.1:9000. Όλα καλά μέχρι εδώ όμως τώρα θέλω να βάλω τον server online. Χρησιμοποιώντας αυτή μια δωρεάν σελίδα, την heroku.com, κατάφερα να ανεβάσω τον server και υπάρχει σε ένα συγκεκριμένο url ας πούμε το https://myapplication.herokuapp.com/. Το πρόβλημα ξεκινάει τώρα στον client όπου δεν μπορώ να συνδεθώ με τον server, καθώς άμα αλλάξω το HOST που έχω στον κώδικα (όταν ήταν local o server τότε το είχα στο 127.0.0.1) και βάλω το url της σελίδας μου μου βγάζει error, το οποίο λέει "Άγνωστος κεντρικός υπολογιστής"Κώδικας:using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace CSharpConsoleTcpClient { class Program { static string HOST = "127.0.0.1"; static int PORT = 9000; static TcpClient client; static void OpenConnection() { if (client != null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("--Connection is already open---"); } else { try { client = new TcpClient(); client.Connect(HOST, PORT); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Connection establised successfully.."); } catch (Exception ex) { client = null; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: Connection could not be established. Msg: " + ex.Message); } } } static void CloseConnection() { if (client == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("--Connection is not open or already closed---"); return; } try { client.Close(); } catch (Exception ex) { } finally { client = null; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Connection closed successfully"); } static void SendData(string data) { if (client == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("--Connection is not open or closed--"); return; } //send NetworkStream nwStream = client.GetStream(); byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(data); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Sending: " + data); nwStream.Write(bytesToSend, 0, bytesToSend.Length); //receive byte[] bytesToRead = new byte[client.ReceiveBufferSize]; int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Received: " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead)); } static void Main(string[] args) { Console.Clear(); string lineRead; do { Console.ResetColor(); Console.Write("\n\nEnter option (1-Open, 2-Send, 3-Close, 4-Quit): "); lineRead = Console.ReadLine(); switch (lineRead) { case "1": OpenConnection(); break; case "2": Console.Write("Enter data to send: "); var data = Console.ReadLine(); SendData(data); break; case "3": CloseConnection(); break; } } while (!lineRead.Equals("4")); } } }
Αν μπορεί κάποιος ας βοηθήσει.
Εμφάνιση 1-8 από 8
Θέμα: Node js και c# client
-
20-04-17, 13:13 Node js και c# client #1
-
20-04-17, 13:44 Απάντηση: Node js και c# client #2
Δοκίμασες να ορίσεις στον server την ip που ακούει ?
Κώδικας:s.listen(9000, '0.0.0.0');
-
20-04-17, 13:51 Απάντηση: Node js και c# client #3
Για δοκίμασε στον server:
Κώδικας:s.listen(process.env.PORT || 9000);
Κώδικας:static int PORT = 80;
Τελευταία επεξεργασία από το μέλος pan05 : 20-04-17 στις 14:12.
-
20-04-17, 15:35 Απάντηση: Node js και c# client #4
Δοκίμασα και τα δύο παραπάνω σχόλια, αλλά χωρίς αποτέλεσμα. Το λάθος που μου δίνει συνεχίζει να είναι το ίδιο. Κάτι άλλο που πρόσεξα είναι πως έχω ένα αρχείο "Procfile" και μέσα έχω γράψει
Κώδικας:web: node index.js
-
20-04-17, 15:45 Απάντηση: Node js και c# client #5
-
20-04-17, 16:18 Απάντηση: Node js και c# client #6
Σε ευχαριστώ πολύ. Τελικά το πρόβλημα λύθηκε ως εξής:
Στον server έβαλα αυτό που μου είπες
Κώδικας:s.listen(process.env.PORT || 9000);
Αλλά απ'ότι φαίνεται το μεγάλο πρόβλημα βρισκόταν στην μεταβλητή HOST αφού εγώ το είχα
Κώδικας:static int HOST = "https://mychatty.herokuapp.com/"
Κώδικας:static int HOST = "www.mychatty.herokuapp.com"
-
20-04-17, 16:54 Απάντηση: Node js και c# client #7
Στα heroku dynos γίνεται επιπλέον routing του request στο παρασκήνιο μέχρι να φτάσει στην εφαρμογή σου που ακούει είτε στην 80 (HTTP) είτε στην 443 (HTTPS). Θα πρέπει οπωσδήποτε για λόγους ασφαλείας να σιγουρευτείς ότι κάνεις HTTPS (aka encrypted) requests. Για να καταλάβω, τώρα από τον client χτυπάς στην 443 ενώ έχεις HOST http ή https; Αν βάλεις https τι γίνεται;
Αν δεν παίζει με https, τότε μάλλον θα πρέπει να κάνεις αλλαγές στον client ώστε να το υποστηρίζει. Όσο παίζεις χωρίς https να ξέρεις ότι η επικοινωνία client - server δεν είναι κρυπτογραφημένη και συνεπώς τα πακέτα ταξιδεύουν σε plain text.Τελευταία επεξεργασία από το μέλος pan05 : 20-04-17 στις 17:06.
-
21-04-17, 11:23 Απάντηση: Node js και c# client #8
Να σου πω την αλήθεια δεν ξέρω τι επικοινωνία υπάρχει, άμα βάλω στο HOST "https://mychatty.herokuapp.com" ή "https://www.mychatty.herokuapp.com" δεν τα αναγνωρίζει και δεν συνδέεται στον server. Επειδή πρόσφατα ξεκίνησα να ασχολούμαι με server-client δεν γνωρίζω και πολλά, αν κάποια στιγμή βγάλω άκρη θα σε ενημερώσω
Bookmarks