עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

איך בדיוק מיישמים TcpListener.?

Featured Replies

פורסם
  • מחבר

הרצתי את הקוד, רק שמשהו בכתובת שנתתי או בפורט, בצורת כתיבה לא נכונה לדעתי

כי זה ישר זורק לי אקספשן





if ((args.Length < 2) || (args.Length > 3))
{ // Test for correct # of args
throw new ArgumentException("192.168.1.104,450");
}

String server = args[0]; // Server name or IP address

// Convert input String to bytes
byte[] byteBuffer = Encoding.ASCII.GetBytes(args[1]);

// Use port argument if supplied, otherwise default to 7
int servPort = (args.Length == 3) ? Int32.Parse(args[2]) : 7;

TcpClient client = null;
NetworkStream netStream = null;

try {
// Create socket that is connected to server on specified port
client = new TcpClient(server, servPort);

Console.WriteLine("Connected to server... sending echo string");

netStream = client.GetStream();

// Send the encoded string to the server
netStream.Write(byteBuffer, 0, byteBuffer.Length);

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

// Receive the same string back from the server
while (totalBytesRcvd < byteBuffer.Length) {
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();
}
}

פורסם

אז תקרא איזו שגיאה הוא זורק, תנסה להבין מה היא אומרת, תחפש באינטרנט מה זה אומר...

מה עם השרת? הרצת אותו בכלל?

אגב, פורטים מתחת ל-1024 הם פורטים שמורים. תשתמש בפורט מעל 1024.

פורסם
  • מחבר

הרצתי כן, השרת הכל בסדר איתו

עכשיו הוא זרק לי שגיאה על השורה הזאת

  throw new ArgumentException("192.168.1.104,450");

לא אמר עליה כלום, כניראה אני לא רושם כמו שצריך את הכתובת IP

פורסם

אתה צוחק, נכון? השורה הזו בעצמה זורקת שגיאה. קראת את הקוד לפני שהרצת אותו? איך אתה מריץ את הקוד בכלל?

פורסם
  • מחבר

קראתי כן

אני מריץ אותו בVS לוחץ F5 מה זאת אומרת, אני העתיק לך את כל הספריה של הסרבר ואז תראה מה עשיתי בדיוק

- - - תגובה אוחדה: - - -

זה התוכנה של השרת:



using System; // For Console, Int32, ArgumentException, Environment
using System.Net; // For IPAddress
using System.Net.Sockets;


namespace ConsoleApplication2
{
class TcpEchoServer
{
private const int BUFSIZE = 32;
static void Main(string[] args)
{


if (args.Length > 1) // Test for correct # of args
throw new ArgumentException("1500");

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();
}
}








}
}
}




וזה התוכנה של הקליינט



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; // For IOException
using System.Net.Sockets;
namespace TcpEchoClient
{


class TcpEchoClient {

static void Main(string[] args) {

if ((args.Length < 2) || (args.Length > 3))
{ // Test for correct # of args
throw new ArgumentException("192.168.1.104,450");
}

String server = args[0]; // Server name or IP address

// Convert input String to bytes
byte[] byteBuffer = Encoding.ASCII.GetBytes(args[1]);

// Use port argument if supplied, otherwise default to 7
int servPort = (args.Length == 3) ? Int32.Parse(args[2]) : 7;

TcpClient client = null;
NetworkStream netStream = null;

try {
// Create socket that is connected to server on specified port
client = new TcpClient(server, servPort);

Console.WriteLine("Connected to server... sending echo string");

netStream = client.GetStream();

// Send the encoded string to the server
netStream.Write(byteBuffer, 0, byteBuffer.Length);

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

// Receive the same string back from the server
while (totalBytesRcvd < byteBuffer.Length) {
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();
}
}
}
}



פורסם
  • מחבר

האאאאא אתה רוצה להגיד לי שבעצם שלחתי ארגומנטים ריקים? ואני פשוט צריך להוסיף אותם בפרופרטיס

נכון?

:)

פורסם
  • מחבר

חח זה עובד, הוא לא רושם שגיאה, אבל הוא רושם שאין לי host

אז אני כבר יודע שהכתובת שרשמתי לא תקינה, יש לך מושג איזה כתובת הוא צריך לקבל? עשיתי ipconfig

וראיתי את הכתובת שלי, אבל משום מה הוא לא מתייחס לזה

פורסם

תלמד קצת על רשתות...

הכי פשוט להעביר לו את הכתובת 127.0.0.1. זו כתובת שתמיד מייצגת את המחשב של עצמך.

פורסם
  • מחבר

אני יודע , אני עושה קורס בדוט נט עכשיו ובקורס שפתחנו web project למדו אותנו שהמחשב מתייחס לכתובת של עצמו ב127.0.0.1 וניסיתי אבל הוא לא מתייחס

מוזר לא יודע למה

הכתובות בproperties צריכות להיות מרכאות? :)

- - - תגובה אוחדה: - - -

סידרתי!!! :))))

הסתכלתי באון ליין בארגיומנטים וראיתי שבארגומנט 0 הוא שם 127.0.0.1 ועשיתי אחרי זה פסיק אז הוא לקח גם את הפסיק החמור

קיצר סידרתי עכשיו מה שאני צריך זה להבין איך להעביר מידע

ונקווה לטוב חח

:)

עכשיו נתחיל לנתח את הקוד ולהבין באמת איך דברים עובדים אחרי ניסוי וטעיה

:)

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.