在socket中发送和接收

时间:2014-06-01 09:58:18

标签: c# sockets send

我在c#中通过套接字发送对象时遇到问题,我的客户端可以发送到服务器但服务器无法发送给客户端,我认为客户端有问题。

服务器

    private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        Thread a = new Thread(connect);
        a.Start();
    }

    private void sendButton_Click(object sender, EventArgs e)
    {
        client.Send(SerializeData(ShapeList[ShapeList.Count - 1]));
    }

    void connect()
    {
        try
        {               
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
            server.Bind(iep);
            server.Listen(10);
            client = server.Accept();
            while (true)
            {
                byte[] data = new byte[1024];
                client.Receive(data);
                PaintObject a = (PaintObject)DeserializeData(data);
                ShapeList.Add(a);
                Invalidate();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

客户端

private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        Thread a = new Thread(connect);
        a.Start();
    }

    private void SendButton_Click(object sender, EventArgs e)
    {
        client.Send(SerializeData(ShapeList[ShapeList.Count - 1]));
    }

    void connect()
    {
        try
        {            
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
            client.Connect(iep);
            while (true)
            {
                byte[] data = new byte[1024];
                client.Receive(data);
                PaintObject a = (PaintObject)DeserializeData(data);
                ShapeList.Add(a);
                Invalidate();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:2)

CheckForIllegalCrossThreadCalls = false;

删除它并修复弹出的错误。如果你压制错误,就更难修复它们。

答案 1 :(得分:2)

client = server.Accept();

在一个帖子中:

client.Send(SerializeData(ShapeList[ShapeList.Count - 1]));

在UI线程中。

这不是线程安全的。

您的客户端是否在循环中接收数据,您希望何时发送?我建议在点击(http://msdn.microsoft.com/en-us/library/dd267265(v=vs.110).aspx)中添加一个线程安全队列,当它适合在循环中发送时:

while (true)
{
    byte[] data = new byte[1024];
    var received = client.Receive(data);
    if(received > 0)
    { //careful, how do you know you have it all?
      PaintObject a = (PaintObject)DeserializeData(data);
      ShapeList.Add(a);
      Invalidate();
    }
    if(!queue.IsEmpty)
    {
       //queue dequeue and send
       client.Send(...);
    }
}

富勒示例

//threadsafe queue
private readonly ConcurrentQueue<byte[]> queue = new ConcurrentQueue<byte[]>();

private void SendButton_Click(object sender, EventArgs e)
{
    //queue it up
    queue.Enqueue(SerializeData(ShapeList[ShapeList.Count - 1]));
}

void connect()
{
    try
    {            
        client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
        client.Connect(iep);
        while (true)
        {
            byte[] data = new byte[1024];
            var received = client.Receive(data);
            if(received > 0)
            { //careful, how do you know you have it all?
              PaintObject a = (PaintObject)DeserializeData(data);
              ShapeList.Add(a);
              Invalidate();
            }
            while(!queue.IsEmpty)
            {
               //queue dequeue and send
               client.Send(queue.Dequeue());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}