【问题标题】:my send function(socket librarys:system.net & system.net.sockets) has some errors我的发送函数(套接字库:system.net & system.net.sockets)有一些错误
【发布时间】:2019-06-11 18:52:45
【问题描述】:

我正在用 C# 编写客户端/服务器应用程序

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {   }

        private void button1_Click(object sender, EventArgs e)
        {
            ///openfile
            OpenFileDialog file = new OpenFileDialog();
            if (file.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = file.FileName;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {   }

        private void label3_Click(object sender, EventArgs e)
        {   }

        private void button2_Click(object sender, EventArgs e)
        {
            //Socket send = new Socket(addressFamily.internetwork,                                    
            socketType.stream, protocolType.tcp);
            Socket send = new Socket(AddressFamily.InterNetwork,                                    
            SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint port = new 
            IPEndPoint(IPAddress.Parse(ip.Text),int.Parse(Port.Text));
            send.Send(port);
        }

        private void Form1_Load(object sender, EventArgs e)
        {   }

        private void label4_Click(object sender, EventArgs e)
        {   }

        private void label3_Click_1(object sender, EventArgs e)
        {   }
    }
}

我的错误是:

错误 2 最佳重载方法匹配 System.Net.Sockets.Socket.Send(System.Collections.Generic.IList>)' 有一些无效参数 C:\Users\Acer\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 53 13 WindowsFormsApplication1

和错误 3 参数 1:无法从“System.Net.IPEndPoint”转换 至 'System.Collections.Generic.IList>' C:\Users\Acer\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 53 23 WindowsFormsApplication1

【问题讨论】:

标签: c# sockets


【解决方案1】:

IpEndPoint 不属于Send 方法。此方法希望传输数据。 EndPoint 属于 Connect 方法。

因此您的错误消息。

您肯定想先连接,然后再尝试发送数据:

send.Connect(port);

图片或每个文件

如果你想通过套接字发送图片,你应该看看this post

【讨论】:

  • @Compo_corp 您是否更改了这一行:send.Send(port);?如果没有,那当然没有变化……
  • 怎么改?改什么?
  • @Compo_corp 你看过我发布的链接了吗?你需要做更多的工作。基本上加载文件,获取所有字节,然后发送字节数组
猜你喜欢
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 2014-10-31
  • 2014-04-15
相关资源
最近更新 更多