TCP/IP connection crashs on Client computer

  • Thread starter Thread starter gaggs1
  • Start date Start date
G

gaggs1

Guest
I devlopped an desktop application that collect data from an android application and talk with my .net app to get values from data base.

my problem is when I run my Setup file on my computer it works fine but it crashs on my client computer.

This is the piece of code that crashes:

public StockCounter(int m)
{
InitializeComponent();
intlang(m);
}
private void intlang(int m)//Select language
{
if (m == 1)
{
Assembly a = Assembly.Load("DisDemo");
ResourceManager rm = new ResourceManager("DisDemo.lang.eng", a);
button1.Text = rm.GetString("StartCounting");
label1.Text = rm.GetString("SProduct");
button2.Text = rm.GetString("Reset");
}
else
{
Assembly a = Assembly.Load("DisDemo");
ResourceManager rm = new ResourceManager("DisDemo.lang.fr", a);

button1.Text = rm.GetString("StartCounting");
label1.Text = rm.GetString("SProduct");
button2.Text = rm.GetString("Reset");
}
}
bool m_StopThread = false;
private bool testtag(string s)//exist
{
// check if same product

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[Product] WHERE [RFID] = '" + s.Substring(0,16) + "'", con);
DataTable table = new DataTable();
data.Fill(table);
if (table.Rows.Count > 0)
{
return true;
}
else
{
return false;
}


}
private void load1()//instialiser la grid
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[Product]", con);
DataTable table = new DataTable();
data.Fill(table);
dataGridView1.Rows.Clear();
foreach (DataRow item in table.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["RFID"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["name"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["quantityStock"].ToString();
}

}

private void button3_Click(object sender, EventArgs e)
{
m_StopThread = true;
timer1.Stop();
}

private static string chargeN(string rfid)
{
// check if same product
string origine;
//origine = rfid.Substring();
origine = rfid.Substring(0, 16);
string name = "";
using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
string oString = "SELECT * FROM Product WHERE[RFID]='" + origine + "'";
SqlCommand oCmd = new SqlCommand(oString, myConnection);

myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{

name = oReader["name"].ToString();

}

myConnection.Close();

}
}
return name;

}
private static string chargeP(string rfid)
{
// check if same product
string origine;
//origine = rfid.Substring();
origine = rfid.Substring(0, 16);
int prix = 0;
using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
string oString = "SELECT * FROM Product WHERE[RFID]='" + origine + "'";
SqlCommand oCmd = new SqlCommand(oString, myConnection);

myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{

prix = int.Parse(oReader["prix"].ToString());

}

myConnection.Close();

}
}
return prix.ToString();

}

private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;

TcpListener server = null;
while (!m_StopThread)
{
bool test1 = false;
string m = " * ";
var xx = new List<string>();
xx.Clear();
//---listen at the specified IP and port no.---
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
Console.WriteLine("Listening...");
listener.Start();

//---incoming client connected---
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connect...");
//---get the incoming data through a network stream---

NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];

//---read incoming stream---
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

//---convert the data received into a string---
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
if (dataReceived != "")
{
var charsToRemove = new string[] { "{", "}", "Uii", "0=", ":", " " };
foreach (var ca in charsToRemove)
{
dataReceived = dataReceived.Replace(ca, string.Empty);
}
dataReceived = dataReceived.Substring(4);
string dataReceived1 = dataReceived.Substring(0, 24);
Console.WriteLine("Received : " + dataReceived);
var charsToRemove1 = new string[] { " " };
foreach (var ca in charsToRemove1)
{
dataReceived = dataReceived.Replace(ca, string.Empty);
}
if (dataReceived.Length < 30)
{
name1 = chargeN(dataReceived);
prix1 = chargeP(dataReceived);
m = m + "#" + name1 + "*" + prix1;
test1 = testtag(dataReceived);
if (test1)
{
string name = ""; int q = 0;
using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
string oString = "Select * from Product WHERE[RFID] = '" + dataReceived.Substring(0, 16) + "'";
SqlCommand oCmd = new SqlCommand(oString, myConnection);

myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{

q = int.Parse(oReader["quantityStock"].ToString());


}

myConnection.Close();

}

}
if (!existS(dataReceived))
{
q = q + 1;
}
string sqlcode = @"UPDATE [dbo].[Product] SET[quantityStock] = '" + q.ToString() + "' WHERE[RFID] = '" + dataReceived.Substring(0, 16) + "'";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
SqlCommand c = new SqlCommand(sqlcode, con);
c.ExecuteNonQuery();
con.Close();
if (!existS(dataReceived))
{
insert(dataReceived, name1);
}

}


}
else
{

while (dataReceived.Length > 48)
{
xx.Add(dataReceived.Substring(0, 24));
dataReceived = dataReceived.Substring(28);

}
dataReceived = dataReceived.Substring(0, 24);
xx.Add(dataReceived);
int x = xx.Count;
Console.WriteLine("LenghtArray : " + x);
for (int i = 0; i < xx.Count; i++)
{
Console.WriteLine("Array ele : " + xx);
name1 = chargeN(xx);
prix1 = chargeP(xx);
m = m + "#" + name1 + "*" + prix1;
test1 = testtag(xx);
if (test1)
{
string name = ""; int q = 0;
using (SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
string oString = "Select * from Product WHERE[RFID] = '" + xx.Substring(0, 16) + "'";
SqlCommand oCmd = new SqlCommand(oString, myConnection);

myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{

q = int.Parse(oReader["quantityStock"].ToString());


}

myConnection.Close();

}

}
if (!existS(dataReceived))
{
q = q + 1;
}

string sqlcode = @"UPDATE [dbo].[Product] SET[quantityStock] = '" + q.ToString() + "' WHERE[RFID] = '" + xx.Substring(0, 16) + "'";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
SqlCommand c = new SqlCommand(sqlcode, con);
c.ExecuteNonQuery();
con.Close();
if (!existS(dataReceived))
{
insert(xx, name1);
}
}

}
Console.WriteLine("Full array : " + m);


}

//---write back the text to the client---
//nwStream.Write(buffer, 0, bytesRead);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(m);
Console.WriteLine("Sending back : " + m);
// Send back a response.
nwStream.Write(msg, 0, msg.Length);

}


client.Close();
listener.Stop();

}

}).Start();
}

private void insert(string s,string n)
{
if (!exist(s))
{
string sqlcode1 = @"INSERT INTO [dbo].[Stock]
([RFID],[name])
VALUES
('" + s + "','" + n + "')";
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con2.Open();
SqlCommand c1 = new SqlCommand(sqlcode1, con2);
c1.ExecuteNonQuery();
con2.Close();
}

}
private bool exist(string d)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[Product] WHERE [RFID] = '" + d + "'", con);
DataTable table = new DataTable();
data.Fill(table);
if (table.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
private bool existS(string d)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[Stock] WHERE [RFID] = '" + d + "'", con);
DataTable table = new DataTable();
data.Fill(table);
if (table.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
private void button2_Click(object sender, EventArgs e)
{
string sqlcode = @"UPDATE [dbo].[Product] SET[quantityStock] = '" + 0 + "'";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
SqlCommand c = new SqlCommand(sqlcode, con);
c.ExecuteNonQuery();
con.Close();
load1();
using (SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
using (SqlCommand command = new SqlCommand("DELETE FROM Stock", con1))
{
command.ExecuteNonQuery();
}
con1.Close();
}

}

private void timer1_Tick(object sender, EventArgs e)
{
load1();
}

private void StockCounter_FormClosed(object sender, FormClosedEventArgs e)
{
m_StopThread = true;
timer1.Stop();
}


Now I want to know if I have to insert some files or something like that to my Setup file to make TCP/IP works on other computer.

Continue reading...
 
Back
Top