我需要解决异常system.net.sockets.socketexception(0x80004005) [英] i need to solve that exception system.net.sockets.socketexception(0x80004005)
问题描述
system.net.sockets.socketexception(0x80004005)
无法建立连接,因为目标机器主动拒绝了它192.168.1.7:1024
i需要知道什么是问题,如何解决
i尝试ping那个192.168.1.7(这个接入点通过tcp / ip模块取ip)并重播没问题
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();
}
}
}
推荐答案
您的代码连接到主机的端口1024(192.168.1.7)
Your code connects to port 1024 of the host (192.168.1.7)
int port = 1024;
TcpClient client = new TcpClient(server, port);
根据例外情况 - 此主机上没有服务侦听此端口。
您可以使用telnet程序检查此端口是否已打开
According to exception - no service listens on this port at this host.
You can check if this port is open using telnet program
telnet 192.168.1.7 1024
要解决 - 确保在主机上某些应用程序侦听此端口。
To resolve - ensure, that on host some application listens to this port.
这篇关于我需要解决异常system.net.sockets.socketexception(0x80004005)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!