- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的服务器/监听器具有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();
}
}
}
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.
}
}
关于c# - 我的服务器/监听器缺少“真正的毅力”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761737/
我正在尝试编写一个程序,以表格的形式计算我的结果。我有一个液压升降机,可以显示作用在直径 (D1) 的活塞上的小力(我称之为 F1)可以乘以作用在直径 (D2) 的活塞上的大力 (F2) ,电梯的运行
我有一个 fiddle :https://jsfiddle.net/mvLf579a/284/ 此 fiddle 允许您使用 D3 径向布局来布置力网络,并将布局重置为默认力布局。但是,重置时链接强度
我想实现表格 View 单元格重叠,因为我想实现这种效果: 所以基本上细胞会一个接一个地堆叠。我的目标是 iOS7+(目前正在测试 iOS8)。 我目前正在做一个 CAGradientLayer,它被
尽管在 box2d 前花了几个小时,但我仍然不明白 applyforce 和 applyimpulse 是如何工作的。我尝试使用一些视觉效果来更好地理解正在发生的事情(通过在 body 位置和应用点之
首先,我是 D3 的新手。我正在尝试使用这些示例在单个 D3 图中实现不同的行为: Drag + Zoom 力导向图 但我的图表在几秒钟后卡住,我不明白为什么... 这是我的代码:http://jsf
所以,我在 Xcode 上用 Swift 玩了这个游戏,我有一个跳跃的 SKShapeNode。不过,我希望这种跳跃与玩家的触感相称。因此,小水龙头会跳得小,而轻快的水龙头会跳得更高。怎么可能做到这一
在 ARKit/SceneKit 中,当用户点击按钮时,我想对我的节点施加一个脉冲。我希望冲动来自当前用户的角度。这意味着节点将远离用户的视角。多亏了这段代码,我能够获得当前的方向/方向: func
我正在使用 Angular2 并拥有父子组件。我的子组件正在使用 DatePipe 提供程序,因为父单元测试失败并给出以下错误, 错误:非法状态:无法加载管道 DatePipe 的摘要。 如何解决这个
我是一名优秀的程序员,十分优秀!