Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys... I really need your help this is my code.
Server:
C#
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:
C#
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:
C#
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:
C#
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;
		}

	}


What I have tried:

The first sent is fine... but the preceeding sent was fail and return an error cannot find table 0.
Posted
Updated 21-Mar-16 14:30pm
v3
Comments
Patrice T 21-Mar-16 20:05pm    
What can be the sent preceding the first one ?
Sascha Lefèvre 21-Mar-16 20:18pm    
If you didn't observe yet where the exception is being thrown, run it again and when the exception occurs, look at which line it is. Then set a debug breakpoint on that line or at the start of that method and run it again. Observe the values of all variables / contents of the DataSet / etc. while you step through the execution line by line and compare that to your expectation of what it should be. Then "go backwards" with breakpoints if neccessary to find the first line where things differ from what you intended. If you then still need help edit your question (with the "Improve question" link below the quesion) and explain what's going wrong and where.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900