0

I have this method in my client it sends to the model an integer whenever i click a button in my clientform.

   public void SetInput(int index)
    {
        String go = index.ToString();
        //String string = "YES";
        MemoryStream memory = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memory);

        writer.Write(go);
       // writer.Write(string);
        serverSocket.Send(memory.GetBuffer());
    }

This is the receive method. //go is the index from the first method.

public  bool Receive(Socket player)
    {
        byte[] buffer = new byte[4096];
        int length = player.Receive(buffer);

        MemoryStream memory = new MemoryStream(buffer, 0, length);
        BinaryReader reader = new BinaryReader(memory);

        String go = reader.ReadString();
        String answer = reader.ReadString(); //this doesnt work
        if (IsTurnValid(Convert.ToInt32(go)) == true)
        {

            PlacePiece(GetActivePlayer(), Convert.ToInt32(go));

            if (IsWon() == true)
            {
                try
                {
..... etc

}
}
}
}

I've made another method that should send an string, although i get an eeror.

public void SetAnswer(string response)
    {
        String answer = response;
        MemoryStream memory = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memory);

        writer.Write(answer);
        serverSocket.Send(memory.GetBuffer());
    }

How could i get both the integer(go) and string(answer) in one stream in the receive method.

user3645080
  • 21
  • 1
  • 5
  • what is the error you are getting? – Zach Spencer Oct 30 '14 at 21:53
  • 1
    I think what you want to do is `memory.ToArray()` not `memory.GetBuffer()` – L.B Oct 30 '14 at 21:55
  • In addition to the ToArray/GetBuffer issue (which is a real problem), I see at least two other major issues: you assume that `player.Receive(buffer)` will read all of the data at once; and while you try to receive a second string into `answer`, I don't see any code that actually sends that second string (other than the `SetAnswer()` method which you say gives you an error...though unfortunately, you're not specific about the error you get). – Peter Duniho Oct 30 '14 at 21:59
  • The error Im getting is System.FormatException Input string was not in a correct format. In my client I've got several buttons which when i click on them send the index. The setanswer one is something like. If i respond yes to a messagebox then setanswer("yes"). The error is at if (IsTurnValid(Convert.ToInt32(go)) == true) . when it goes to the messagebox and send the string, the receive method will store it to the go var. – user3645080 Oct 30 '14 at 22:05

0 Answers0