错误:无法找到表0.通过套接字发送数据集时。 [英] Error: cannot find table 0. When sending dataset through socket.

查看:87
本文介绍了错误:无法找到表0.通过套接字发送数据集时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......我真的需要你的帮助这是我的代码。

服务器:

Hello guys... I really need your help this is my code.
Server:

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.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Teachear_s_Evaluation___Server
{
	public partial class Server : Form
	{
		Listener listen;
		public Server()
		{
			InitializeComponent();
			listen = new Listener(8);
			listen.SocketAccepted += new Listener.SocketAcceptedHandler(listen_SocketAccepted);
			Load += new EventHandler(Server_Load);
		}

		void Server_Load(object sender, EventArgs e)
		{
			listen.Start();
		}

		void listen_SocketAccepted(System.Net.Sockets.Socket e)
		{
			Client client = new Client(e);
			client.Received += new Client.ClientReceivedHandler(client_Received);
		}

		void client_Received(Client sender, byte[] data)
		{
			Invoke((MethodInvoker)delegate
			{
				DataSet set = (DataSet)DeserializeData(data);
				DataTable table = set.Tables[0];
				dataGridView1.DataSource = table;

			});
		}

		public object DeserializeData(byte[] theByteArray)
		{
			MemoryStream ms = new MemoryStream(theByteArray);
			BinaryFormatter bf1 = new BinaryFormatter();
			ms.Position = 0;
			return bf1.Deserialize(ms);
		}

	}
}





收听者:



Listener:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Teachear_s_Evaluation___Server
{
	class Listener
	{
		Socket s;

		public bool Listening
		{
			get;
			private set;
		}

		public int Port
		{
			get;
			private set;
		}

		public Listener(int port)
		{
			Port = port;
			s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		}

		public void Start()
		{
			if (Listening)
			return;

			s.Bind(new IPEndPoint(0, Port));
			s.Listen(0);

			s.BeginAccept(callback, null);
			Listening = true;
		}

		public void Stop()
		{
			if (!Listening)
			return;

			s.Close();
			s.Dispose();
			s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		}

		void callback(IAsyncResult ar)
		{
			try
			{
				Socket s = this.s.EndAccept(ar);

				if (SocketAccepted != null)
				{
					SocketAccepted(s);
				}

				this.s.BeginAccept(callback, null);

			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}

		public delegate void SocketAcceptedHandler(Socket e);
		public event SocketAcceptedHandler SocketAccepted;
	}

}



客户:


Client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Teachear_s_Evaluation___Server
{
	class Client
	{
		public string ID
		{
			get;
			private set;
		}

		public IPEndPoint Endpoint
		{
			get;
			private set;
		}

		Socket sck;

		public Client(Socket accepted)
		{
			sck = accepted;
			ID = Guid.NewGuid().ToString();
			Endpoint = (IPEndPoint)sck.RemoteEndPoint;
			sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);

		}

		void callback(IAsyncResult ar)
		{
			try
			{
				sck.EndReceive(ar);
				byte[] buf = new byte[1024];

				int rec = sck.Receive(buf, buf.Length, 0);

				if (rec < buf.Length)
				{
					Array.Resize<byte>(ref buf, rec);
				}

				if (Received != null)
				{
					Received(this, buf);
				}

				sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				Close();

				if (Disconnected != null)
				{
					Disconnected(this);
				}
			}
		}

		public void Close()
		{
			sck.Close();
			sck.Dispose();

		}

		public delegate void ClientReceivedHandler(Client sender, byte[] data);
		public delegate void ClientDisconnectedHandler(Client sender);

		public event ClientReceivedHandler Received;
		public event ClientDisconnectedHandler Disconnected;

	}
}





虽然这是数据集的发送者:



While this is the sender of dataset:

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.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Teacher_s_Evaluation___Student
{
	public partial class FormEvaluation : Form
	{

		DataSet EvaluationDS;
		DataTable EvaluationDT;
		static string eval = "";

		Socket client;

		int index = 0;

		public FormEvaluation()
		{
			InitializeComponent();

			Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8);
			sck.BeginConnect(iep, new AsyncCallback(Connected), sck);
			for (int i = 0; i < 15; i++)
			{
				EvaluationDT.Rows.Add("Q" + (i + 1).ToString(), 0);
			}

		}
		void Connected(IAsyncResult iar)
		{
			client = (Socket)iar.AsyncState;
			try
			{
				client.EndConnect(iar);
				btnSubmit.Enabled = true;
				Console.WriteLine("Connected");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}

		public byte[] SerializeData(Object o)
		{
			MemoryStream ms = new MemoryStream();
			BinaryFormatter bf1 = new BinaryFormatter();
			bf1.Serialize(ms, o);
			return ms.ToArray();
		}

		private DataSet CreateDataSet()
		{
			try
			{
				EvaluationDS = new DataSet("FromClient");
				EvaluationDS.Tables.Add(EvaluationDT);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			return EvaluationDS;
		}

	}





我尝试过:



第一次发送是好的......但是之前发送的是失败并返回错误找不到表0.



What I have tried:

The first sent is fine... but the preceeding sent was fail and return an error cannot find table 0.

推荐答案

首先,请不要发布这么多代码。它只是弄乱了这个问题。我们只需要相关的代码。

其次,当你收到错误时,准确显示错误的位置。

第三,在这种情况下,错误非常简单。您正在尝试访问DataSet中的第一个表,但该表不存在。这意味着你的DataSet是空的,没有表格。



为什么?那是你要弄明白的。我们无法运行您的代码,也无法查看运行时发生的情况。一点点调试,你应该得到它。
First off, please don't post so much code. It just clutters up the question. We only need relevant code.
Second, when you get an error, show exactly where the error was.
Third, in this case, the error is very simple. You are trying to access the first table in a DataSet but that table does not exist. This means your DataSet is empty and has no tables.

Why? That is for you to figure out. We cannot run your code nor see what is happening when it does run. A little bit of debugging and you should get it.


这篇关于错误:无法找到表0.通过套接字发送数据集时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆