Hi there.
I created 3 buttons which have different tasks. the first one is “connect”, the second one “send msg” and the third one “Disconnect”. Here i display the first two buttons.
My problems is that the SendMSg Button always tells me IOException:Not connected.
This only happens if i split the functions on two buttons. If the buttons are combined, and the Connect button connects and sends the massage after connecting, i receive the message on my server (which is a different programm).
What do i do wrong or how can i call the connect button in my message button?
Connect
public class ConnectToServer : MonoBehaviour
{
TcpClient client = new TcpClient();
public void onClick()
{
client.Connect("127.0.0.1", 30005);
NetworkStream stream = client.GetStream();
client.GetStream();
}
}
SendMsg
public class SendMsgToPlant : MonoBehaviour
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes("asdasd message");
TcpClient client = new TcpClient();
public void SendMsg()
{
NetworkStream stream = client.GetStream();
if (client.Connected)
stream.Write(data, 0, data.Length);
}
}