Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不允许发送或接收数据的请求,因为套接字未连接到C#_C#_Connect_Server - Fatal编程技术网

不允许发送或接收数据的请求,因为套接字未连接到C#

不允许发送或接收数据的请求,因为套接字未连接到C#,c#,connect,server,C#,Connect,Server,我正在尝试向计算机发送用户名和密码身份验证响应,但出现以下错误 不允许发送或接收数据的请求,因为套接字未连接,并且(使用sendto调用在数据报套接字上发送时)未提供地址 static void Main(string[] args) { Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sck.Bind(new IPEndPoint(IPAddre

我正在尝试向计算机发送用户名和密码身份验证响应,但出现以下错误

不允许发送或接收数据的请求,因为套接字未连接,并且(使用sendto调用在数据报套接字上发送时)未提供地址

static void Main(string[] args)
{
    Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    sck.Bind(new IPEndPoint(IPAddress.Parse("192.168.5.40"), 10022));

    sck.Listen(1);

    Socket accepted = sck.Accept();
    byte[] buffer = Encoding.Default.GetBytes("Test post");
    accepted.Send(buffer, 0, buffer.Length, 0);

    buffer = new byte[255];

    int rec = accepted.Receive(buffer, 0, buffer.Length, 0);
    Array.Resize(ref buffer, rec);
    Console.WriteLine("Recieved: {0}", Encoding.Default.GetString(buffer,24,buffer.Length-24));
    Console.WriteLine("Recieved: {0}", BitConverter.ToString(buffer, 0, 24));

    string hexValues = "00-00-00-01-f8-a0-3d-48-0b-6d-00-00-3d-0a-bf-09-00-00-00-57-00-10-00-00-00";

    byte[] hex = StringToByteArray(hexValues);

    sck.Send(hex, 0, hex.Length, 0);
    sck.Close();
    accepted.Close();
    Console.Read();
}

您尝试在侦听传入连接的套接字上发送数据:

sck.Send(hex, 0, hex.Length, 0);
您应该改为通过
accepted
发送:

accepted.Send(hex, 0, hex.Length, 0);

您尝试在侦听传入连接的套接字上发送数据:

sck.Send(hex, 0, hex.Length, 0);
您应该改为通过
accepted
发送:

accepted.Send(hex, 0, hex.Length, 0);