gpt4 book ai didi

c# - 我的服务器/监听器缺少“真正的毅力”

转载 作者:太空宇宙 更新时间:2023-11-03 18:14:36 25 4
gpt4 key购买 nike

我的服务器/监听器具有ADD或没有耐力;解毒剂或产品是什么?

该应用程序充当套接字通信的两端。对于第一条消息,它似乎工作正常(我在textBox1中输入“Bla”,然后label1读取“Bla back atcha”,但在随后的消息中失败。我在我的开发机上运行了该应用程序的一个实例,另一个实例(重命名为包括“服务器”一词)在另一台计算机上。

我粘贴了下面的代码,并且第二次尝试发送消息err msg(“无法建立连接,因为目标计算机主动拒绝了10.24.93.110:51111”)。

当我在另一台计算机上启动“服务器”实例并在命令行中运行“netstat -a”时,它表示服务器计算机正在端口51111监听我的开发计算机。

在传递了第一条消息之后,显然收到并抛回了消息之后,运行“netstat -a”仍然显示与我的开发机的连接,但是该状态不再是LISTENING,而是TIME_WAIT。

然后,我尝试传递另一条消息,然后得到err msg(下面的图表B)

附件A:来源

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;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace testSocketSendAndReceive_Nutshell
{
public partial class Form1 : Form
{
string sJerrysIPAddr = "10.24.31.110";
string sMyIPAddr = "10.24.31.128";
string sThisAppFileName = string.Empty;
bool bThisInstanceFunctionsAsServer = false;

internal static Form1 MainSocketPairForm = null;

public Form1()
{
InitializeComponent();
MainSocketPairForm = this;
}

private void Form1_Load(object sender, EventArgs e)
{
sThisAppFileName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
lblFileName.Text = sThisAppFileName;

// Client and Server code are here combined in one app; however, we want each instance to run as
// just one or the other, so (the .exe functioning as a Server should be renamed with the subString
// "Server" somewhere in the filename):
bThisInstanceFunctionsAsServer = sThisAppFileName.Contains("Server");
if (bThisInstanceFunctionsAsServer)
{
new Thread(Server).Start(); // Run server method concurrently.
Thread.Sleep(500); // Give server time to start.
}
btnSendMsg.Visible = !bThisInstanceFunctionsAsServer;
}

static void Client()
{
using (TcpClient client = new TcpClient(Form1.MainSocketPairForm.sJerrysIPAddr, 51111)) // err here second time
using (NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write(Form1.MainSocketPairForm.textBox1.Text.ToString());
w.Flush();
Form1.MainSocketPairForm.label1.Text = new BinaryReader(n).ReadString();
}
}

static void Server() // Handles a single client request, then exits.
{
TcpListener listener = new TcpListener(IPAddress.Any, 51111);
listener.Start(); //Only one usage of each socket address (protocol/network address/port) is normally permitted
// got the above err msg with an instance running and listening on jerry's machine
// continues to listen even after shut down...
using (TcpClient c = listener.AcceptTcpClient())
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
BinaryWriter w = new BinaryWriter(n);
w.Write(msg + " back atcha!");
w.Flush(); // Must call Flush because we're not disposing the writer.
}
listener.Stop();
}

private void button1_Click(object sender, EventArgs e)
{
Client();
}

private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}

附件B:完整的错误信息

System.Net.Sockets.SocketException was unhandled Message="No connection could be made because the target machine actively refused it 10.24.93.110:51111" Source="System" ErrorCode=10061 NativeErrorCode=10061 StackTrace: at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port) at testSocketSendAndReceive_Nutshell.Form1.Client() in C:\testSocketSendAndReceive_Nutshell\testSocketSendAndReceive_Nutshell\Form1.cs:line 57 at testSocketSendAndReceive_Nutshell.Form1.button1_Click(Object sender, EventArgs e) in C:\testSocketSendAndReceive_Nutshell\testSocketSendAndReceive_Nutshell\Form1.cs:line 90 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at testSocketSendAndReceive_Nutshell.Program.Main() in C:\testSocketSendAndReceive_Nutshell\testSocketSendAndReceive_Nutshell\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

最佳答案

发送响应后,您需要调用client.Close()。您还需要围绕服务器的接受逻辑进行循环:

var shouldExit == false;
while (!shouldExit)
using (TcpClient c = listener.AcceptTcpClient())
{
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
if (msg == "exit")
// Client told us to exit...
shouldExit = true;
BinaryWriter w = new BinaryWriter(n);
w.Write(msg + " back atcha!");
w.Flush(); // Must call Flush because we're not disposing the writer.
}
}

这些都是文档中的示例: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

关于c# - 我的服务器/监听器缺少“真正的毅力”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com