在论坛里看见diaodiaop高人回复的关于socket一个服务器端连多个客户端的控制台代码,经测试很好。现在要改成窗体程序,本人初学,始终不能把接收到的数据传给listbox控件,已经搞了3天,还是不行。现请教各位高人,应该怎样做,谢谢!代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;namespace socket一对多通信
{
class Program
{
static byte[] buffer = new byte[1024];
static void Main(string[] args)
{
try
{
Dictionary<string, int> server = new Dictionary<string, int>();
server.Add("192.168.53.53", 4999);
server.Add("192.168.53.54", 4998);
server.ToList().AsParallel().ForAll(item =>
{
Console.WriteLine("con");
var client = new TcpClient();
client.Connect(item.Key, item.Value);
client.Client.BeginReceive(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(ReceiveMessage), client);
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
} static void ReceiveMessage(IAsyncResult ar)
{
var socket = ar.AsyncState as TcpClient;
try
{
var length = socket.Client.EndReceive(ar);
byte[] reallData = new byte[length];
Array.Copy(buffer, reallData, length);
ThreadPool.QueueUserWorkItem(new WaitCallback((state) => { doWork(new objectState() { s = socket, data = reallData }); }));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
socket.Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
}
}
static void doWork(object o)
{
DateTime dt = DateTime.Now; var rs = o as objectState;
var a = string.Join("-", rs.data.Select(d => d.ToString("X2")).ToArray());
Console.WriteLine(dt + " " + rs.s.Client.RemoteEndPoint.ToString() + " 当前线程ID: " + Thread.CurrentThread.ManagedThreadId);//获取连接的服务器端的IP和PORT 获取当前线程的标识符ID
Console.WriteLine(a);
//Console.WriteLine(Thread.CurrentThread.ThreadState);//获取当前线程的类型,后台线程,还是其他
}
} public class objectState
{
public TcpClient s { get; set; }
public byte[] data { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;namespace socket一对多通信
{
class Program
{
static byte[] buffer = new byte[1024];
static void Main(string[] args)
{
try
{
Dictionary<string, int> server = new Dictionary<string, int>();
server.Add("192.168.53.53", 4999);
server.Add("192.168.53.54", 4998);
server.ToList().AsParallel().ForAll(item =>
{
Console.WriteLine("con");
var client = new TcpClient();
client.Connect(item.Key, item.Value);
client.Client.BeginReceive(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(ReceiveMessage), client);
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
} static void ReceiveMessage(IAsyncResult ar)
{
var socket = ar.AsyncState as TcpClient;
try
{
var length = socket.Client.EndReceive(ar);
byte[] reallData = new byte[length];
Array.Copy(buffer, reallData, length);
ThreadPool.QueueUserWorkItem(new WaitCallback((state) => { doWork(new objectState() { s = socket, data = reallData }); }));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
socket.Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
}
}
static void doWork(object o)
{
DateTime dt = DateTime.Now; var rs = o as objectState;
var a = string.Join("-", rs.data.Select(d => d.ToString("X2")).ToArray());
Console.WriteLine(dt + " " + rs.s.Client.RemoteEndPoint.ToString() + " 当前线程ID: " + Thread.CurrentThread.ManagedThreadId);//获取连接的服务器端的IP和PORT 获取当前线程的标识符ID
Console.WriteLine(a);
//Console.WriteLine(Thread.CurrentThread.ThreadState);//获取当前线程的类型,后台线程,还是其他
}
} public class objectState
{
public TcpClient s { get; set; }
public byte[] data { get; set; }
}
}
解决方案 »
- 通过DataSet遍历出xml文档中的两个节点【急】
- DataGridView如何做到左键或者右键只要是单击(双击)DataGridView以外区域就取消行的选中(设置为未选中状态)
- GDI+ 如何保存文件
- C# 委托处理状态栏的现实
- 关于dataset的一个问题
- winform窗体里 Anchor属性失效 写了top left right 无效 仍旧原始大小不变 请问是什么原因?
- 请教个貌似是整型溢出的问题
- 请教一个关于反射的问题
- 两个完全一样的listview,(lvQuery,LvReady),怎样将第一个中的全部数据复制到第二个中去?
- 关于远程 Remoting 的问题
- 求助,获得cmd的返回的内容,我只想要运行后获得的内容要怎么写?
- 理解不了C#里的实型变量,而且右图是怎么得出的?
{
private MyServer Server = new MyServer("211.122.134.78:3388"); public Form1()
{
InitializeComponent();
Server.ReceiveMessage += ......
}
这类的代码,每一个窗体都分别连到服务器,并且监听服务器传来的完整消息事件。也可能是使用一个 static 的变量来保存全局的进程使用一个连接到服务器的处理对象。事件处理方法中,如何访问窗体,是你自己需要编程设计的。比如说上述代码中就可以直接以匿名委托方式访问 this.BeginInvoke 方式访问当前窗体。因为通讯处理过程通常都是在设计编写各种窗体之前设计开发的,所以通讯模块并不依赖于窗体,窗体对象应该监听通讯对象抛出的事件、然后把消息显示在窗体内部。你了解这个设计模式,才刚刚对设计有所理解。不懂设计的人,总也不能理解依赖倒置的基本思想!
如果写在单独的类中,就需要通过事件与 Form 沟通
如果写在 Form 类中,那就已经可以直接赋值了(Form 都有相应的方法)
如果你连架构都没有确定,那么给你代码又有什么意义呢
你贴的代码里,大致看了下
Dictionary<string, int> server = new Dictionary<string, int>();
server.Add("192.168.53.53", 4999);
server.Add("192.168.53.54", 4998);
这段,是用来模拟服务器端“监听”到的数据的你得从根本上学习socket编程,不是什么UI控件的问题每台客户端需要和服务端建立连接,服务器不断监听客户端发来的信息,并做处理,显示等