在C#中使用TcpClient的或一般连接到插座我怎么能先检查如果某个端口是免费在我的机器上?
更多信息: 这是我使用的代码:
TcpClient c;
//I want to check here if port is free.
c = new TcpClient(ip, port);
Run Code Online (Sandbox Code Playgroud) 我试图通过静态类继承非静态类.
public class foo
{ }
public static class bar : foo
{ }
Run Code Online (Sandbox Code Playgroud)
我得到:
静态类不能从类型派生.静态类必须从对象派生.
我怎样才能从对象中派生出来?
代码在C#中.
我没有F#的经验,但在C#中有几行测试代码用于我需要在F#中重写的框架.
任何帮助,将不胜感激.
bar.Ready += new Agent.ReadyHandler(bar_Ready);
static void bar_Ready(string msg)
{
Console.WriteLine(msg.body);
}
Run Code Online (Sandbox Code Playgroud) 我不确定何时应该在C#中使用匿名类型而不是局部变量.
我有:
string fullMessage // This is the full message including sender and recipient names
string sender = GetMessagePart(fullMessage, "from");
string recipient = GetMessagePart(fullMessage, "to");
//do some stuff and deliver the message
Run Code Online (Sandbox Code Playgroud)
我应该使用:
var msg = new {
sender = GetMessagePart(fullMessage, "from")
recipient = GetMessagePart(fullMessage, "to")
};
Run Code Online (Sandbox Code Playgroud)
代替?
在C#中的一些藏品,如ArrayList和HashTable有是通用的替代品List<T>和Dictionary<TKey, TValue>.
Array也有一个通用的替代品吗?
什么是正则表达式
initSock|north|router\r\n
Run Code Online (Sandbox Code Playgroud)
哪里有北方的变化,但其余的总是一样的.所以它可以是:
initSock|foo|router\r\n
initSock|bar|router\r\n
Run Code Online (Sandbox Code Playgroud)
等等
但是在哪里北,foo或者酒吧肯定应该有东西而且不能是空的.我将在C#中使用它.
为了更清楚:任何看起来像这样的东西都很糟糕:
initSock||router\r\n
initSockfoorouter\r\n
|foo|router\r\n
initSock|foo|router
initSock|foo|portal\r\n
outSock|foo|router\r\n
Run Code Online (Sandbox Code Playgroud) 在C#中我尝试通过TcpClient发送一个字符串:
byte[] outputOutStream = new byte[1024];
ASCIIEncoding outputAsciiEncoder
string message //This is the message I want to send
TcpClient outputClient = TcpClient(ip, port);
Stream outputDataStreamWriter
outputDataStreamWriter = outputClient.GetStream();
outputOutStream = outputAsciiEncoder.GetBytes(message);
outputDataStreamWriter.Write(outputOutStream, 0, outputOutStream.Length);
Run Code Online (Sandbox Code Playgroud)
我必须将消息从字符串转换为字节,有没有办法可以直接将其作为字符串发送?
我知道这在Java中是可行的.
如何以DLL的用户无法查看我的源代码的方式将我的C#代码转换为DLL文件?
当我通过创建类库项目,导入我的类并编译它来创建DLL时,仍然可以查看源代码.
你能否将这个代码从C#转换为Python在IronPython上运行?
我对Python没有任何经验.
using System;
using Baz;
namespace ConsoleApplication
{
class Program
{
static void Main()
{
Portal foo = new Portal("Foo");
Agent bar = new Agent("Bar");
foo.Connect("127.0.0.1", 1234);
foo.Add(bar);
bar.Ready += new Agent.ReadyHandler(bar_Ready);
}
static void bar_Ready(object sender, string msg)
{
Console.WriteLine(msg.body);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图重复使用正常的代码
TcpClient oC = new TcpClient(ip, port);
oC = new TcpClient(ip, port);
StreamReader messageReader;
try {
messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII);
reply = messageReader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
同
try {
using (messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII))
{
reply = messageReader.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个InvalidOperationException说法
非连接套接字上不允许该操作.
有什么问题,我该如何解决?
更多:我有oc.Connect在此代码之前,所以我连接,当它想要第一次使用是工作正常,只有在那之后,我得到了这个例外,我玩了一下它现在和现在我明白了:
无法访问已处置的对象.\ r \n对象名称:'System.Net.Sockets.Socket'.