Hi guys,

I'm in desperate need of help with my code so if anyone could help me out it would be most appreciated:

I have built a asynchronous client / server application which runs 100% perfectly. I now want to have the client connect to multiple servers at a time so I created this class to fire off new instances of my client (passing it the target address). The class program queries my database for a list of servers to connect to periodically, it then passes the target to the worker class which actually does the work. I know my code for the worker class is correct as I've tested it (i.e. not using the class program)

I'm seeing a few errors each time this is run like for instance unable to access a disposed socket: Object name: System.Net.Sockets.Socket, at System.Net.Sockets.EndRecieve.
And also "A request to send or recieve data was disallowed because the socket is not connected" which when i check is connected.

I assume these errors occur due to interference but I'm totally stumped.

Would someone be able to show me how to get this model working please?

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Data;
using System.Timers;
using System.Collections;
using System.Threading; 

namespace DeployeeSrv

{

class Program

{

static SqlConnection connection = new SqlConnection(Properties.Settings.Default.dbConnection);

static SqlCommand command = new SqlCommand();

static SqlDataReader Reader = null;

static ArrayList al = new ArrayList(); 

delegate String test(String target);

 

 


static void Main(string[] args)

{

System.Timers.Timer timer = new System.Timers.Timer();

//ad 1: handle Elapsed event

timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

timer.Interval = 60000;

timer.Enabled = true;

timer.Start();

DoWork();

Console.Read();

}

 

public static void DoWork()

{

al.Clear();

try

{

connection.Open();

command = connection.CreateCommand();

command.CommandText = "SELECT [target] from [controller].[dbo].[pending_jobs] where [Scheduled] = 0 GROUP BY [target]";

Reader = command.ExecuteReader();

while (Reader.Read())

{ 

//add to the array list

al.Add(Reader["target"].ToString());

}

Reader.Close();

for (int i = 0; i < al.Count; i++)

{

command.CommandText = @"UPDATE [dbo].[Pending_Jobs]

SET [Scheduled] = 1

WHERE [target] = '" + al[i].ToString() + "' and [Scheduled] = 0 ";

command.ExecuteNonQuery();

worker wk = new worker();

// create the delegate

test t = new test(wk.dostuff);

// call the delegate asynchronously

IAsyncResult result = t.BeginInvoke(al[i].ToString(), new AsyncCallback(CallbackMethod), t);

}

}

catch (SqlException ex)

{

Console.WriteLine(ex.ToString());

}

finally

{

if (connection.State == ConnectionState.Open)

{

connection.Close();

}

}

}

 

static void CallbackMethod(IAsyncResult result)

{

// get the delegate that was used to call that

// method

test t = (test)result.AsyncState;

// get the return value from that method call

//String returnValue = t.EndInvoke(result);

t.EndInvoke(result);

//Console.WriteLine("The result was " + returnValue.ToString());

}

 

private static void OnElapsedTime(object source, ElapsedEventArgs e)

{

 

DoWork();


}

 

}

}

P.S. ive uploaded my full client connecting code here:

if anyone needs to take a look.

Please help if you can

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.