我在C#中有以下客户端/服务器代码,我在两台计算机上测试过,它们可以成功连接并且连接没有任何问题
问题:当我尝试在它们之间发送消息时,有时在目标计算机上不会收到消息,直到它们发送回消息。
任何帮助都是适当的..
服务器:::
replace
客户端:::
public partial class Form1 : Form
{
private byte[] data = new byte[1024];
private int size = 1024;
private Socket server;
private Socket client ;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
stopListining.Enabled = false;
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}
private void startListining_Click(object sender, EventArgs e)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 20916);
server.Bind(iep);
server.Listen(5);
server.BeginAccept(new AsyncCallback(AcceptConn), server);
startListining.Enabled = false;
stopListining.Enabled = true;
}
private void stopListining_Click(object sender, EventArgs e)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
stopListining.Enabled = false;
startListining.Enabled = true;
}
void AcceptConn(IAsyncResult iar)
{
Socket server = (Socket)iar.AsyncState;
client = server.EndAccept(iar);
conStatuss("Connected to: " + client.RemoteEndPoint.ToString());
string stringData = "Server:: Welcome to my server";
byte[] message1 = Encoding.ASCII.GetBytes(stringData);
client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
new AsyncCallback(SendData), client);
}
void SendData(IAsyncResult iar)
{
Socket client = (Socket)iar.AsyncState;
int sent = client.EndSend(iar);
client.BeginReceive(data, 0, size, SocketFlags.None,
new AsyncCallback(ReceiveData), client);
}
void ReceiveData(IAsyncResult iar)
{
try
{
Socket client = (Socket)iar.AsyncState;
int recv = client.EndReceive(iar);
if (recv == 0)
{
client.Close();
conStatuss("Waiting for client...");
server.BeginAccept(new AsyncCallback(AcceptConn), server);
return;
}
string receivedData = Encoding.ASCII.GetString(data, 0, recv);
addResult(receivedData+"\"R("+DateTime.Now.ToString("h:mm:ss tt")+")\"");
//to send back received text
/*
byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
client.BeginSend(message2, 0, message2.Length, SocketFlags.None,
new AsyncCallback(SendData), client);
*/
}
catch (Exception eee)
{
conStatuss(eee.ToString());
}
}
delegate void SetTextCallback(string text);
private void conStatuss(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statetxt.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(conStatuss);
this.Invoke(d, new object[] { text });
}
else
{
this.statetxt.Text = text;
}
}
public delegate void AddListBoxItem(string message);
public void addResult(string message)
{
//TODO: find out whats going on here
if (resultss.InvokeRequired)
{
//TODO: Which works better?
//AddListBoxItem albi = AddString;
//listBox.Invoke( albi, message );
resultss.Invoke(new AddListBoxItem(addResult), message);
}
else
this.resultss.Items.Add(message);
}
private void sendButton_Click(object sender, EventArgs e)
{
if (messageTXT.Text.Trim().ToString().Length == 0)
{
conStatuss("Write something to send!");
return;
}
/*
byte[] byData = System.Text.Encoding.ASCII.GetBytes(messageTXT.Text);
client.Send(byData);
resultss.Items.Add("Server:: "+messageTXT.Text);
*/
string stringData = "Server:: " + messageTXT.Text+"\"S("+DateTime.Now.ToString("h:mm:ss tt")+")\"";
addResult(stringData);
byte[] message1 = Encoding.ASCII.GetBytes(stringData);
client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
new AsyncCallback(SendData), client);
messageTXT.Clear();
}
}