Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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:
C#
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...
Posted
Updated 21-May-22 7:33am
v2
Comments
Richard MacCutchan 21-May-22 13:09pm    
Make sure that your server is running on the local machine, and listening on port 49778.

1 solution

127.0.0.1 is your own machine, so the reason is almost certainly that (a) you haven't started a server which has invoked listen (TCP) or recvfrom (UDP) on port 49778, or (b) more likely, your firewall rejected the connection for security reasons, in which case you have to tell your firewall to allow it.
 
Share this answer
 
Comments
Richard Deeming 25-May-22 5:57am    
NB: The "target machine actively refused" usually means the connection was blocked by a firewall; if there was nothing listening on the port, you'd be more likely to get a connection timeout error. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900