通过网络发送数据 C#

发布于 2024-10-06 23:28:01 字数 428 浏览 1 评论 0原文

我尝试通过网络发送一个字符串,这是我的代码:

 IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25);

    TcpClient client = new TcpClient(serverEndPoint);
    Socket socket = client.Client;

    byte[] data = Encoding.ASCII.GetBytes(response);

    socket.Send(data, data.Length, SocketFlags.None);

    socket.Close();
    client.Close();

当我运行它时,我得到 System.Net.Sockets.SocketException

I try to send a string over the network, this is my code:

 IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25);

    TcpClient client = new TcpClient(serverEndPoint);
    Socket socket = client.Client;

    byte[] data = Encoding.ASCII.GetBytes(response);

    socket.Send(data, data.Length, SocketFlags.None);

    socket.Close();
    client.Close();

When I run it I got System.Net.Sockets.SocketException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一枫情书 2024-10-13 23:28:01

如果使用无连接协议,则必须在调用 Send 之前调用 Connect,否则 Send 将抛出 SocketException。如果您使用面向连接的协议,则必须使用 Connect 建立远程主机连接,或使用 Accept 接受传入连接。
请参阅 Socket.Send 方法(Byte[]、Int32、SocketFlags)

假设如果您使用的是无连接协议,代码应该是这样的,

string response = "Hello";
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

if (ipAddress != null)
{
    IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, 25);
    byte[] receiveBuffer = new byte[100];

    try
    {
        using (TcpClient client = new TcpClient(serverEndPoint))
        {
            using (Socket socket = client.Client)
            {
                socket.Connect(serverEndPoint);

                byte[] data = Encoding.ASCII.GetBytes(response);

                socket.Send(data, data.Length, SocketFlags.None);

                socket.Receive(receiveBuffer);

                Console.WriteLine(Encoding.ASCII.GetString(receiveBuffer));
            }
        }
    }
    catch (SocketException socketException)
    {
        Console.WriteLine("Socket Exception : ", socketException.Message);
        throw;
    }
}

下次,尝试包含异常消息来解释实际出了什么问题。

If you are using a connectionless protocol, you must call Connect before calling Send, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.
Refer Socket.Send Method (Byte[], Int32, SocketFlags)

Assuming you are using a connectionless protocol the code should be like this,

string response = "Hello";
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

if (ipAddress != null)
{
    IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, 25);
    byte[] receiveBuffer = new byte[100];

    try
    {
        using (TcpClient client = new TcpClient(serverEndPoint))
        {
            using (Socket socket = client.Client)
            {
                socket.Connect(serverEndPoint);

                byte[] data = Encoding.ASCII.GetBytes(response);

                socket.Send(data, data.Length, SocketFlags.None);

                socket.Receive(receiveBuffer);

                Console.WriteLine(Encoding.ASCII.GetString(receiveBuffer));
            }
        }
    }
    catch (SocketException socketException)
    {
        Console.WriteLine("Socket Exception : ", socketException.Message);
        throw;
    }
}

Next time, try including the exception message to explain what actually went wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文