Declare your method to return Socket
public static Socket socketConnect()
and instead break
return sck;
Your sck object is on the heap so it will be still accessable after returning it.
Pregunta
updated code:
public static Socket socketConnect()
{
while (true)
{
try
{
Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(sck.Connected.ToString());
IPEndPoint connectAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000); // Server IP & PORT
sck.Connect(connectAddress);
Console.WriteLine(sck.Connected.ToString());
if (sck.Connected == true)
{
return sck
}
}
catch (Exception ex)
{
System.Threading.Thread.Sleep(1000); // Sleep time before reconnect
}
}
how can i use sck outside this method?
for example i want to check in main if sck.Connected is true or false
yes i'm new in c# ..
now im having new problem everytime i want to send or recv i write socketConnect().send or recv and then the program execute the socketConnect() method again but i only want to use sck to send and recv what im doing wrong?
Solución
Declare your method to return Socket
public static Socket socketConnect()
and instead break
return sck;
Your sck object is on the heap so it will be still accessable after returning it.