Reputation: 59
My problem is that first time i connect Tcp client to server using application Asynchronous it connect's and working but when i disconnect it and connect it again the data send two time like s8 s8
Connection Code
constatus.Text = "Connecting.....";
// newsock = null;
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPEndPoint iep = new IPEndPoint(IPAddress.Any, 20);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(textBox2.Text), Convert.ToInt16(textBox3.Text));
newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
Data send code
if (!InvokeRequired)
{
try
{
byte[] message = Encoding.ASCII.GetBytes(Allinput_Command);
client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);
}
catch (Exception) { }
}
else
{
Invoke(new ThreadStart(data_send));
}
Disconnect Code
client.Close();
timer.Stop();
timer1.Stop();
please Tell me Right Path to solve this .... i can send my all code if anybody tell me solution
Upvotes: 0
Views: 155
Reputation: 182743
Before you start coding anything that uses TCP, make sure you have a specification for the protocol you are layering on top of TCP. Otherwise, it is impossible to fix a bug like this. Once you have a specification, it's easy:
Is the sending program following the specification? If not, it's at fault.
Is the receiving program following the specification? If not, it's at fault.
If both programs follow the specification and communication still doesn't work, the specification is broken.
My bet is that you think you are sending messages but you are actually sending bytes. If you want to send messages, you need a specification that explains where messages begin and end, and both the sender and received must follow that specification.
Upvotes: 1