פורסם 2012 בינואר 1913 שנים מחבר כרגע יש לי את הקוד של ה-CLIENT:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Net.Sockets;namespace TcpEchoServer{ class TcpEchoClient { static void Main(string[] args) { if ((args.Length < 2) || (args.Length > 3)) { // Test for correct # of args throw new ArgumentException("Parameters: <Server> <Word> [<Port>]"); } String server = args[0]; // Server name or IP address byte[] byteBuffer = Encoding.ASCII.GetBytes(args[1]); // Convert input String to bytes int servPort = (args.Length == 3) ? Int32.Parse(args[2]) : 7; // Use port argument if supplied, otherwise default to 7 TcpClient client = null; NetworkStream netStream = null; try { client = new TcpClient(server, servPort); // Create socket that is connected to server on specified port Console.WriteLine("Connected to server... sending echo string"); netStream = client.GetStream(); netStream.Write(byteBuffer, 0, byteBuffer.Length); // Send the encoded string to the server Console.WriteLine("Sent {0} bytes to server...", byteBuffer.Length); int totalBytesRcvd = 0; // Total bytes received so far int bytesRcvd = 0; // Bytes received in last read while (totalBytesRcvd < byteBuffer.Length) // Receive the same string back from the server { if ((bytesRcvd = netStream.Read(byteBuffer, totalBytesRcvd, byteBuffer.Length - totalBytesRcvd)) == 0) { Console.WriteLine("Connection closed prematurely."); break; } totalBytesRcvd += bytesRcvd; Console.WriteLine("Received {0} bytes from server: {1}", totalBytesRcvd, Encoding.ASCII.GetString(byteBuffer, 0, totalBytesRcvd)); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { netStream.Close(); client.Close(); } Console.ReadLine(); } }}ושל ה-SERVER:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace TcpEchoServer{ class Program { private const int BUFSIZE = 32; // Size of receive buffer static void Main(string[] args) { if (args.Length > 1) // Test for correct # of args { throw new ArgumentException("Parameters: [<Port>]"); } int servPort = (args.Length == 1) ? Int32.Parse(args[0]) : 7; TcpListener listener = null; try { //Create a TCPListener to accept client connections listener = new TcpListener(IPAddress.Any, servPort); listener.Start(); } catch (SocketException se) { Console.WriteLine(se.ErrorCode + ": " + se.Message); Environment.Exit(se.ErrorCode); } byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer int bytesRcvd; // Received byte count for (; ; ) // Run forever, accepting and servicing connections { TcpClient client = null; NetworkStream netStream = null; try { client = listener.AcceptTcpClient(); // Get client connection netStream = client.GetStream(); Console.Write("Handling client - "); // Receive until client closes connection, indicated by 0 return value int totalBytesEchoed = 0; while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) { netStream.Write(rcvBuffer, 0, bytesRcvd); totalBytesEchoed += bytesRcvd; } Console.WriteLine("echoed {0} bytes.", totalBytesEchoed); // Close the stream and socket. We are done with this client! netStream.Close(); client.Close(); } catch (Exception e) { Console.WriteLine(e.Message); netStream.Close(); ; } } } }}אחרי כל קוד מוסבר בדיוק מה כל פקודה עושה ולפני הקוד של ה-CLIENT מוסבר:Our first TCP application, called TcpEchoClient.cs, is a client that communicates with anecho server using TCP. An echo server simply repeats whatever it receives back to theclient. The string to be echoed is provided as a command-line argument to our client.Many systems include an echo server for debugging and testing purposes. To test if thestandard echo server is running, try telnetting to port 7 (the default echo port) on theserver (e.g., at command line “telnet server.example.com 7” or use your telnet applicationof choice). If not, you can run this client against the TcpEchoServer.cs server fromthe next section.אני מריץ את הקוד של הסרבר וכשאני מריץ את הקוד של הקליינט אבל הוא נכשל ורק רשום בחלון קונסול No such host is known ובויז'ואל הוא נכשל בשורה netStream.Close() ורשום Object reference not set to an instance of an object.כשאני מריץ את הקליינט נתתי לו 2 פרמטרים ב-Command line argument, הראשון args]0[ והודעה מסויימת.אשמח לעזרה היות ואני די תקוע.
פורסם 2012 בינואר 2013 שנים הכנסת בתור פרמטר ראשון args[0] ... ברצינות ?!אמרתי לך קודם, חסר לך ידע בסיסי בכל מיני דברים חסר לך ידע בסיסי בתכנות, ובפרט בקריאה והבנה של קודחסר לך ידע בסיסי בתקשורת מחשבים.הנה מדריך (אומנם בסי, אבל המדריך מסביר ברמה בסיסית תקשורת מחשבים) http://beej.us/guide/bgnet/אחרי זה תחזור על הקוד שוב ותקרא מה הפרמטר הראשון שהתוכנית מצפה לקבל זה רשום לך בהערה בקוד, תבין מה המשמעות שלו ואז תוכל להכניס אותו.אני מצטער על הבוטות אבל הכל רשום בקוד.
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.