system.net.sockets.socketexception(0x80004005)
no connection could be made because the target machine actively refused it 192.168.1.7:1024
i need to know what is the problem and how can solve
i try to ping that 192.168.1.7 (this access point take ip through tcp/ ip module )and that replay no problem

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.Remoting.Channels;

namespace simple_tcpclient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Connect("192.168.1.7", "1");


        }
          public static void Connect(String server, String message) 
{
  try 
  {
    // Create a TcpClient. 
    // Note, for this client to work you need to have a TcpServer  
    // connected to the same address as specified by the server, port 
    // combination.
    int port = 1024;
    TcpClient client = new TcpClient(server, port);

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

    // Get a client stream for reading and writing. 
   //  Stream stream = client.GetStream();

    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);

    System.Windows.Forms.MessageBox.Show("Sent: {0}" + message); ;         

    // Receive the TcpServer.response. 

    // Buffer to store the response bytes.
    data = new Byte[256];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

    System.Windows.Forms.MessageBox.Show("recieved: {0}"+ responseData); 

    // Close everything.
   // stream.Close();         
   // client.Close();         
  } 
  catch (ArgumentNullException e) 
  {
      System.Windows.Forms.MessageBox.Show("ArgumentNullException: {0}"+ e);
  } 
  catch (SocketException e) 
  {
      System.Windows.Forms.MessageBox.Show("SocketException: {0}"+ e);
  }

  System.Windows.Forms.MessageBox.Show("\n Press Enter to continue...");
  Console.Read();


}
















        }
    }

9

Dani AI

Generated

"Actively refused" means your SYN reached 192.168.1.7 but the host (or something on the path) immediately RSTed the connection because nothing was accepting TCP on that port. That is why ping looks fine: ICMP reachability says the IP is alive, not that a TCP service exists. Building on , , and : the usual causes are (1) no process listening on 192.168.1.7:1024, (2) a firewall/ACL denying that port, or (3) you are using the wrong protocol/port for the device.

Quick checks you can run before touching your client code:

  • On 192.168.1.7, confirm a listener: netstat -ano | findstr :1024 or PowerShell Get-NetTCPConnection -LocalPort 1024. If nothing shows, start or configure the server.
  • From your client machine, test the port, not ICMP: Test-NetConnection 192.168.1.7 -Port 1024 (PowerShell) or telnet 192.168.1.7 1024. If it says Refused, there is no listener or a firewall is blocking.
  • Verify an inbound rule for TCP 1024 on the target OS and any upstream firewall/router. If the client and target are on different subnets/VLANs, check ACLs.

If you control 192.168.1.7 and just need a quick sanity check, run a tiny TCP server there and try your client again:

using System.Net;
using System.Net.Sockets;

var listener = new TcpListener(IPAddress.Any, 1024);
listener.Start();
Console.WriteLine("Listening on 0.0.0.0:1024...");
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
{
    var data = System.Text.Encoding.ASCII.GetBytes("OK");
    stream.Write(data, 0, data.Length);
}

If that works, the network is fine and the issue is that the real service is not listening on 1024. If it still fails, fix the firewall or the route before troubleshooting your C# client.

Recommended Answers

All 3 Replies

Port 1024 is blocked on the target machine (or by the router, or the firewall).

commented: Deep knowledge +14

Can you telnet to that IP and port? Ping is not telling you much here. The earlier post is probably correct.

Also could be that nothing is listening at 192.168.1.7:1024

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.