我有tcpclient对象,我想确定它是否已连接.我使用tcpclient的连接属性,但它返回上一次操作的状态.所以它没用.
那我用这个代码:
bool flag;
flag = (tcp.Client.Poll(10000, SelectMode.SelectWrite));
Run Code Online (Sandbox Code Playgroud)
和
if( tcp.Client.Poll( 0, SelectMode.SelectRead ) )
{
byte[] buff = new byte[1];
if( tcp.Client.Receive( buff, SocketFlags.Peek ) == 0 )
{
flag = false;
}
}
Run Code Online (Sandbox Code Playgroud)
但它无法正常工作.
任何的想法?
这是我在服务器端的代码:
private ArrayList _ClientList = new ArrayList();
public ClsServer(int port)
{
_TCPListener = new TcpListener(IPAddress.Any, port);
_TCPListener.Start();
Thread ListenThread = new Thread(new ThreadStart(ListenForClients));
ListenThread.IsBackground = true;
ListenThread.Start();
}
private void ListenForClients()
{
while (true)
{
//blocks until a client has connected to the …Run Code Online (Sandbox Code Playgroud) 我在数据表中有一个很大的 IP 地址列表,我必须如此快速地对它们执行 ping 操作,我使用了以下代码:
public bool PingIP(string IP)
{
bool result = false;
try
{
Ping ping = new Ping();
PingReply pingReply = ping.Send(IP);
if (pingReply.Status == IPStatus.Success)
result = true;
}
catch
{
result = false;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
然后我在 while 循环中调用它:
private void CheckNetworkState()
{
while (rowindexping > -1)
{
if (rowindexping == tbl_ClientInfo.Rows.Count)
{
rowindexping = -1;
return;
}
string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
if (!PingIP(ip))
{
do something
}
rowindexping++;
Thread.Sleep(100);
}
}
Run Code Online (Sandbox Code Playgroud)
因为我想在项目的后台完成这项工作,所以我在线程中调用该类:
threadping …Run Code Online (Sandbox Code Playgroud) c# multithreading thread-safety visual-studio-2010 visual-studio
我想在线程列表中运行具有不同值的类.像这样 :
int index = 0;
foreach (TreeNode nd in tvew.Nodes[0].Nodes)
{
threadping[index] = new Thread(delegate()
{ this.Invoke(new DelegateClientState(InvokeCheckNetworkState), new object[] {nd}); });
threadping[index].Name = nd.Name;
threadping[index].IsBackground = true;
threadping[index].Start();
index++;
}
Run Code Online (Sandbox Code Playgroud)
但是当我调试代码时,我看到class参数只是最后一个值.我的意思是当我通过线程类时,我看到每次该类运行输入参数的值是最后一个线程的最后一个值.
任何人都可以告诉我为什么?
我有一个这样的类列表:
public class CityCodeInfo
{
public string CityName;
public string Province;
public string Code;
}
List<CityCodeInfo> lstCityt = new List<CityCodeInfo>();
Run Code Online (Sandbox Code Playgroud)
我如何按其任何变量(cityname,province和code)对此列表进行排序
我试过这段代码:
lstCityt.Sort((x, y) => string.Compare(x.CityName, y.CityName));
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
任何的想法?!
我创建了一个DNN模块并将其添加到模块列表中.现在我想打电话给我,我试过这个:
Response.Redirect(Globals.NavigateURL(this.TabId, "Control Key", "parameter"), true);
Run Code Online (Sandbox Code Playgroud)
但它会打开默认页面,这是空的!
我的模块在哪里?!?!或者我的错误在哪里?!