I'm trying to connect a server using socket, and i got this exeption:
System.Net.Sockets.SocketException: 'No connection could be made because the target machine actively refused it 127.0.0.1:49778'
this is the code:
private static bool IsActive(string port)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress iPAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, int.Parse(port));
try
{
var TryConnect = Task.Run(() => socket.Connect(iPEndPoint));
if (TryConnect.Wait(TimeSpan.FromSeconds(1)))
{
byte[] buffer = ASCIIEncoding.ASCII.GetBytes("exit");
socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
return true;
}
}catch (Exception e) { }
return false;
}
connect to server:
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Server_Session(socket);
if(socket.Connected)
Listen(socket);
}
private static void Server_Session(Socket socket)
{
IPAddress sreverIPAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint iPEndPoint = new IPEndPoint(sreverIPAddress, 8080);
Console.WriteLine("connecting...");
socket.Connect(iPEndPoint);
Console.WriteLine("hello client : " + socket.LocalEndPoint);
string msg = "client";
byte[] buffer = aSCII.GetBytes(msg);
socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
Upload_Conversation(socket);
if(socket.Connected)
Listen(socket);
}
i don't know how to solve it... please help
What I have tried:
i don't even know what to do...