0

I have trouble in sending an object through socket in c#, my client can send to server but server can't send to client, i think there is something wrong with the client.

Server

    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);
        }
    }

client

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);
        }
    }
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • What is _"server can't send to client"_? What happens? Also, see [Why does my client socket not receive what my server socket sends?](http://stackoverflow.com/questions/23713664/why-does-my-client-socket-not-receive-what-my-server-socket-sends). – CodeCaster Jun 01 '14 at 11:16
  • yeah my project is to draw an shape like a line then send it through the socket, the client can send the shape to the server but i cant send the shape from the server to the client, i also read your link but i dont understand much, can you show me more clearly? – user3696492 Jun 01 '14 at 13:45
  • What do you mean with _"can't send the shape from the server to the client"_? What happens? Do you get an exception? The link I provided mentions that your `client.Receive(data);` does not necessarily receive all data your server sent. – CodeCaster Jun 01 '14 at 14:49

2 Answers2

2
CheckForIllegalCrossThreadCalls = false;

Remove this and fix the errors that pop up. If you suppress the errors, it's even harder to fix them.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
2
client = server.Accept();

Is in one thread:

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

Is in the UI thread.

That's not threadsafe.

You have your client receiving data in a loop, when do you want to send? I'd suggest adding to a threadsafe queue in the click (http://msdn.microsoft.com/en-us/library/dd267265(v=vs.110).aspx) and when it's suitable send in the loop:

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(...);
    }
}

Fuller example

//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);
    }
}
weston
  • 54,145
  • 21
  • 145
  • 203
  • i dont understand much, i am a newbie in c#, how can i do to fix this? can you show me more clearly please? – user3696492 Jun 01 '14 at 12:40
  • There you go, it's not guaranteed to work. Personally I avoid going so low level as sockets now a days. But it certainly is threadsafe. – weston Jun 01 '14 at 18:21
  • 1
    @user3696492 If you are a C# newbie I would agree with weston too, instead of using sockets, go with a higher layer of abstraction like [WCF](http://msdn.microsoft.com/en-us/library/ms731082%28v=vs.110%29.aspx) or some other communication library. – Scott Chamberlain Jun 01 '14 at 18:25