在sockect的接受事件中如果把信息传递给当前的form中的grid中呢

qq_35022134 2017-10-28 12:32:58
下面是代码,也是在网上找的。如下在ReceiveMessage方法中我想从收到的信息返回给当前界面上的grid中去
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.Net;
using System.Net.Sockets;
using System.Threading;

namespace SocketServer
{
public partial class Form1 : Form
{
private static byte[] result = new byte[1024];
private static int myProt = 8885; //端口
static Socket serverSocket;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//服务器IP地址
IPAddress ip = IPAddress.Parse("192.168.1.104");
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, myProt)); //绑定IP地址:端口
serverSocket.Listen(10); //设定最多10个排队连接请求
MessageBox.Show("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
//通过Clientsoket发送数据
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();


}
/// <summary>
/// 监听客户端连接
/// </summary>
private static void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hellodddd"));
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <param name="clientSocket"></param>
private static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);

MessageBox.Show("接收客户端{0}消息{1}" + myClientSocket.RemoteEndPoint.ToString() + Encoding.ASCII.GetString(result, 0, receiveNumber) + DateTime.Now.ToString());

//因为是接收事件中是另一线程中无法中使用this。如果new form1又不是前面界面上那个grid了。
//也无法在此处用Invoke
//如果在此处把信息显示到form界面上的grid中呢?
}
catch //(Exception ex)
{

// MessageBox.Show(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
}
}
}
///如何把信息返回到当前form的grid中去呢?
谢谢!
...全文
150 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大鱼> 2017-10-30
  • 打赏
  • 举报
回复
C#里面是有委托的,你看一下委托就知道怎么做了,靠别人给你写代码不如自己去了解,这样以后碰到问题也能想到方案
desperaso 2017-10-30
  • 打赏
  • 举报
回复
CrossThreadCall.cs

 public static class CrossThreadCall
    {
        public static void CrossThreadCalls(this Control ctl, ThreadStart del)
        {
            if (del == null) return;
            if (ctl.InvokeRequired)
                ctl.Invoke(del, null);
            else
                del();
        }
调用

pic_progress1.CrossThreadCalls(() =>
                    {
                        pic_progress1.text=....................;
                    });
qq_33406739 2017-10-30
  • 打赏
  • 举报
回复
你怕是不知道什么事委托哦
desperaso 2017-10-30
  • 打赏
  • 举报
回复
参考 http://www.haolizi.net/example/view_9813.html 下载:https://pan.baidu.com/s/1slxZwVN (c# winform socket.rar)
njit_77 2017-10-28
  • 打赏
  • 举报
回复
MainWindow gMainWindow = this;//获取当前窗口对象

            gMainWindow.Dispatcher.Invoke((Action)(() =>
            {
                //赋值
            }));
wpf这样写,你修改下应该就可以了

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧