开发者

C# Network Programming Error

开发者 https://www.devze.com 2023-02-04 20:50 出处:网络
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.

I'm trying to do a very simple Server/Client with sockets. Going along with this. Any help is greatly appreciated http://www.devarticles.com/c/a/C-Sharp/Socket-Programming-in-C-Part-I/1/

Client Code

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.Net.Sockets;
using System.Net;
using System.Threading;

namespace ClientServer
{
    public partial class Form1 : Form
    {
        private byte[] m_Data = new byte[10];
        private Socket connection;
        private IAsyncResult m_asyncResult;
        private AsyncCallback pfnCallBack;
    public Form1()
    {
        InitializeComponent();
        connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    }

    private void btn_connect_Click(object sender, EventArgs e)
    {
        makeConnection();
    }

    private void makeConnection()
    {
        IPAddress remoteMachineIP = IPAddress.Parse(ipAddress.Text);
        IPEndPoint remoteMachine = new IPEndPoint(remoteMachineIP, int.Parse(portNumber.Text));
        connection.Connect(remoteMachine);
        //WaitForData();
    }
    private void OnDataReceived(IAsyncResult asyn)
    {
        // Receive Ends
        int iRx = 0;
        iRx = connection.EndReceive(asyn);
        char[] chars = new char[iRx + 1];
        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
        int charLen = d.GetChars(m_Data, 0, iRx, chars, 0);
        System.String szData = new System.String(chars);
    }

    private void WaitForData()
    {
        if (pfnCallBack == null)
            pfnCallBack = new AsyncCallback(OnDataReceived);
        m_asyncResult = connection.BeginReceive(m_Data, 0, m_Data.Length, SocketFlags.None, pfnCallBack, null);
    }

    private void send_Click(object sender, EventArgs e)
    {
        try
        {
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(message.Text);
            connection.Send(byData);
        }
        catch (SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }
}

Server Code

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.Net.Sockets;
using System.Net;

namespace Server
{
    public partial class Form1 : Form
    {
        public Socket connection;
        public Socket m_socWorker;
        public IAsyncResult m_asynResult;
        public AsyncCallback pfnCallBack;
        byte[] m_DataBuffer = new byte[10];
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_listen_Click(object sender, EventArgs e)
        {
            try
            {
            connection = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);
            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, int.Parse(portNumber.Text));
            connection.Bind(ipLocal);
            //Listening now
            connection.Listen(4);
            connection.BeginAccept(new AsyncCallback(OnClientConnect),null);
            }
            catch (SocketException se)
            {

            }
            txt_status.Text = "Listening";           
        }

        private void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                m_socWorker = connection.EndAccept(asyn);
                WaitForData(m_socWorker);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }

            txt_status.Text = "User Connected";
        }

        private void WaitForData(Socket m_socWorker)
        {
            if (pfnCallBack == null)
                pfnCallBack = new AsyncCallback(OnDataReceived);
            // now start to listen for any data...
            m_asynResult =
            connection.BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);
    开发者_如何学编程    }

        public void OnDataReceived(IAsyncResult asyn)
        {
            txt_status.Text = "Data Received";
            //end receive...
            int iRx = 0;
            iRx = connection.EndReceive(asyn);
            char[] chars = new char[iRx + 1];
            System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
            int charLen = d.GetChars(m_DataBuffer, 0, iRx, chars, 0);
            System.String szData = new System.String(chars);
            dataReceived.Text = szData;
            WaitForData(m_socWorker);


        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消