c# TCP IP 客户端和服务器无响应错误

c# TCP IP client and server no respond error

我有一个简单的 TCP/IP 客户端和服务器不工作: 我想用它在一些客户端和服务器之间传输数据 enter image description here enter image description here

在服务器端我有:

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

namespace TCP_IP_Server
{
    public partial class frmMain : Form
    {
        private ArrayList nSockets;
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            IPHostEntry IPhost = Dns.GetHostByName(Dns.GetHostName());
            lblStatus.Text = "IP Address: " + IPhost.AddressList[0].ToString();
            nSockets = new ArrayList();
            Thread thdListner = new Thread(new ThreadStart(listnerThread));
        }

        public void listnerThread()
        {
            TcpListener tcpListener = new TcpListener(8080);
            tcpListener.Start();
            while(true)
            {
                Socket handlerSocket = tcpListener.AcceptSocket();
                if(handlerSocket.Connected)
                {
                    Control.CheckForIllegalCrossThreadCalls = false;
                    lbConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " Connected.");
                    lock (this)
                    {
                        nSockets.Add(handlerSocket);
                    }
                    ThreadStart thdstHandler = new ThreadStart(handlerThread);
                    Thread thdHnadler = new Thread(thdstHandler);
                    thdHnadler.Start();
                }
            }
        }

        public void handlerThread()
        {
            Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
            NetworkStream networkStream = new NetworkStream(handlerSocket);
            int thisRead = 0;
            int BlockSize = 1024;
            byte[] dataByte = new byte[BlockSize];
            lock (this)
            {
                Stream fileStream = File.OpenWrite(@"%userprofile%\desktop\SubmitedFile.txt");
                while(true)
                {
                    thisRead = networkStream.Read(dataByte, 0, BlockSize);
                    fileStream.Write(dataByte, 0, thisRead);
                    if (thisRead == 0)
                        break;
                }
                fileStream.Close();
            }
            lbConnections.Items.Add("File Written.");
            handlerSocket = null;
        }
    }
}

在 Clinet 方面:

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

namespace TCP_IP_Client
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            ofdBrowse.ShowDialog();
            txtFile.Text = ofdBrowse.FileName;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Stream fileStream = File.OpenRead(txtFile.Text);
            byte[] fileBuffer = new byte[fileStream.Length];
            fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
            TcpClient tcp = new TcpClient(txtServer.Text, 8080);
            NetworkStream networkStream = tcp.GetStream();
            networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
            networkStream.Close();
        }
    }
}

我是 运行 VPS 上的服务器,它有静态 IP 地址,客户端在我自己的电脑上,但在点击发送按钮后发生异常:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond **.**.**.**:8080

1.程序化问题

创建后不要忘记启动线程

private void frmMain_Load(object sender, EventArgs e)
{
    IPHostEntry IPhost = Dns.GetHostByName(Dns.GetHostName());
    lblStatus.Text = "IP Address: " + IPhost.AddressList[0].ToString();
    nSockets = new ArrayList();
    Thread thdListner = new Thread(new ThreadStart(listnerThread));
    thdListner.start();
}

2。网络问题

  • 获取代码使用的 IP 地址来托管服务器并在客户端 Ping 该地址。
  • 如果成功,请检查服务器、客户端的防火墙设置(可以临时切换防火墙看看是否是这个问题)
  • 如果您使用的是 dns 名称,请在客户端执行 nslookup