locked
Socket Receive problem RRS feed

  • Question

  • Hi!

    I want to make a file sharing program between pc and winmo. But the client doesn't working. The client program throws an exception if i try to connect, but if i try it between 2 computers, it works well. The exception code is 10054, and i marked the place, where it goes wrong.

    Server code:

    .....
            private void Connect()
            {
                try
                {
                    sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    IPHostEntry IPHost = Dns.Resolve(Dns.GetHostName());
                    string[] aList = IPHost.Aliases;
                    IPAddress[] addresses = IPHost.AddressList;

                    IPEndPoint ipServer = new IPEndPoint(addresses[0], 5000);
                    MessageBox.Show("Server IP:\n" + addresses[0].ToString());
                    sSocket.Bind(ipServer);
                    sSocket.Listen(1);
                    sSocket.Blocking = true;


                    while (true)
                    {
                        cSocket = sSocket.Accept();
                        if (cSocket != null && cSocket.Connected)
                        {
                            tc = new Thread(new ThreadStart(ClientScan));
                            tc.Start();
                        }                  
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                  
                }
            }

            private void ClientScan()
            {
                string command = "FileList";
                byte[] s = Encoding.Default.GetBytes(command);
                cSocket.Send(s, s.Length, 0);
              
                try
                {
                    while (cSocket != null)
                    {

                        byte[] received = new byte[32767];
                        int count = cSocket.Receive(received, received.Length, 0);
                        string m = Encoding.Default.GetString(received);
                        m = m.Substring(0, count);
                        commandList = m.Split('?');
    .......




    Client code:
    ......

            private void ConnectingButton_Click(object sender, EventArgs e)
            {
                if (connected == false)
                {
                    try
                    {
                        sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        sSocket.Blocking = true;

                        IPAddress[] addresses = new IPAddress[1];
              //manually added IPaddress
                        addresses[0] = IPAddress.Parse(textBox1.Text);
                        IPEndPoint ipServer = new IPEndPoint(addresses[0], 5000);
                        sSocket.Connect(ipServer);
                        cSocket = sSocket;

                        th = new Thread(new ThreadStart(ServerScan));
                        th.Start();
                        return;
                    }
                    catch
                    {
                        MessageBox.Show("Connect Error");
                        Application.Exit();
                    }
                }
                if (connected == true)
                {
            ...
                }
            }
            private void ServerScan()
            {
               try
                { 
                   while (cSocket != null)
                    {
                        byte[] received = new byte[1024];

        //////No problem so far
        ////////////////////here is the problem

                        count = sSocket.Receive(received, 0, received.Length, SocketFlags.None);

        /////////////////here is the problem


                       string m = Encoding.Default.GetString(received, 0, count);
                        m = m.Substring(0, count);

                        commandList = m.Split('?');
                        connected = true;

                        if (commandList[0] == "FileList")
                        {
                            FileList();
                            continue;
                        }
    .......

     

    Thanks for helping!

    Robert

    Friday, December 10, 2010 8:36 PM

Answers

  •  

    Hi,

     

    I think it cause by thread safe control. What the type control did you create? If .NET compact Framework you can use Control.InvokeRequired  and Thread-Safe Calls to a Windows Forms Control.

     

    If native c++ you can use WaitForSingleObject

     

    Hope these information can help you to solve this problem.

     

    Have a nice day,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Vrobert Monday, December 20, 2010 10:17 AM
    Monday, December 20, 2010 8:24 AM
  • Hello!

    Yes, you are right. I used MethodInvoker:

    if (commandList[0] == "FileList")
    {
      MethodInvoker mi = new MethodInvoker(FileList);
      BeginInvoke(mi);
      Thread.Sleep(50);
    }
    
    
    Now it works perfect, thanks.

    Best regards

    Robert

    • Marked as answer by Jesse Jiang Monday, December 20, 2010 11:18 AM
    Monday, December 20, 2010 10:17 AM

All replies

  •  

    Hi Vrobert,

     

    Welcome to the MSDN Forum, I'm Jesse and I will do my best to solve your issue.

     

    I think your problem is at here th = new Thread(new ThreadStart(ServerScan));

     

    Once you start a new thread the value sSocket will be limit in function ServerScan(), so you should set sSocket as parameter when call ServerScan() function.

     

    For more information you can check Thread.Thread(ParameterizedThreadStart) Constructor

     

    I hope this information will be useful.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 13, 2010 8:24 AM
  •  

    Hi,

     

    I'm very interested in hearing more about your issue, did you solve your problem?

    If so please share the solution with us and mark the right answers.

    If not please let me know and I will tray to help you find further solutions.

     

    Have a nice day!

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 15, 2010 11:30 AM
  • Hi Jesse!

    I'm sorry, but i haven't got much time in the past few days.
    I figured out, this wasn't the real problem. The sockets and connections are perfect, the two programs can communicate.
    The problem is in the server code, when the client sends back the file-list:
    InvalidOperationException: Cross-thread operation not valid: Control  „listView4” accessed from a thread other than the thread it was created on.

    Server crashes, and the client throw SocketException.
    So now i try to find a way, how can i give values to the controls in "tc" thread.

    Thank you for your help!
    Róbert
    Thursday, December 16, 2010 2:10 PM
  •  

    Hi,

     

    I think it cause by thread safe control. What the type control did you create? If .NET compact Framework you can use Control.InvokeRequired  and Thread-Safe Calls to a Windows Forms Control.

     

    If native c++ you can use WaitForSingleObject

     

    Hope these information can help you to solve this problem.

     

    Have a nice day,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Vrobert Monday, December 20, 2010 10:17 AM
    Monday, December 20, 2010 8:24 AM
  • Hello!

    Yes, you are right. I used MethodInvoker:

    if (commandList[0] == "FileList")
    {
      MethodInvoker mi = new MethodInvoker(FileList);
      BeginInvoke(mi);
      Thread.Sleep(50);
    }
    
    
    Now it works perfect, thanks.

    Best regards

    Robert

    • Marked as answer by Jesse Jiang Monday, December 20, 2010 11:18 AM
    Monday, December 20, 2010 10:17 AM