CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2006
    Posts
    3

    Problem with Socket :(

    Hi,

    i'm very new to C# i started programming with #Develop and it's very nice

    My goal is to develop a little application in C# that handles a Joystick inputs, with DirectInput from DirectX API i got what i need, i can detect the different inputs from the player (UP, DOWN, Fire, etc), now i need to send those inputs to a Socket Server that i've developped, this server is just a simple echo server, so the server is supposed to echo "LEFT", "RIGHT", "UP" etc it works BUT the bytes are sent to the socket ONLY when i close the application!!! , not when the user press the buttons from the joystick, so it's not very nice for the gameplay hahah

    So if i press, LEFT, RIGHT, DOWN, nothing is sent, then i close the application, and at this moment the byes are sent and i see the server echoing the inputs.

    I've put my Joystick listening stuff in a Thread, maybe there is something i didn't get as 'im pretty new to this language.

    Thanks a lot for your help, i really appreciate

  2. #2
    Join Date
    Nov 2005
    Posts
    63

    Re: Problem with Socket :(

    Are you using a Stream object to write to the socket? If so, you should call Flush() after each write.

  3. #3
    Join Date
    Apr 2006
    Posts
    3

    Re: Problem with Socket :(

    hi, thanks a lot for your answer, here is how i send the bytes ? is that correct ?

    i call the changeTextValue method with a string as parameter, like "LEFT", "RIGHT" etc

    private void changeTextValue ( string str )

    {

    byte[] byData = System.Text.Encoding.ASCII.GetBytes(str);

    if ( byData.Length > 0 ) {

    m_sock.BeginSend (byData, 0, byData.Length, 0, new AsyncCallback(this.SendCallback), m_sock);

    }

    myText.Text = str;

    System.Console.WriteLine (str);

    }

    protected void SendCallback(IAsyncResult ar)

    {
    // Retrieve the socket from the async state object.
    Socket handler = (Socket) ar.AsyncState;
    try
    {
    // Complete sending the data to the remote device.
    int bytesSent = handler.EndSend(ar);
    }
    catch (Exception e)
    {
    }

    }

  4. #4
    Join Date
    Apr 2006
    Posts
    3

    Re: Problem with Socket :(

    ok, i see what you mean, instead of using the Socket class i can use the TCPClient class, yes i use a stream object to send the message, and i also call the Flush() method on it, but still the same problem, my calls are queued and all sent one the application closed

    here is my code :

    Byte[] data = System.Text.Encoding.ASCII.GetBytes("LEFT");

    NetworkStream stream = myTCP.GetStream();

    stream.Write(data, 0, data.Length);

    stream.Flush();

  5. #5
    Join Date
    Nov 2005
    Posts
    63

    Re: Problem with Socket :(

    How are you reading the data in?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured