Reputation: 172
I'm quite new to sockets programming. I hope the problem is presented understandable.
The problem is that when I use Client's button1_click
to send textbox
's
content - the server only gets the first message. I have no idea what's going wrong.
What might be the problem?
Here is Server:
public partial class Form1 : Form
{
Socket server;
byte[] byteData = new byte[1024];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 20000);
server.Bind(endPoint);
server.Listen(1);
server.BeginAccept(new AsyncCallback(Accept), null);
textBox1.Text = "Server started...";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Accept(IAsyncResult ar)
{
try
{
Socket client = server.EndAccept(ar);
server.BeginAccept(new AsyncCallback(Accept), null);
client.BeginReceive(byteData, 0, byteData.Length,
SocketFlags.None, new AsyncCallback(Receive), client);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndReceive(ar);
textBox1.Invoke(new Action(delegate ()
{
textBox1.AppendText(Environment.NewLine
+ Encoding.ASCII.GetString(byteData));
}));
byteData = null;
}
}
And here is client:
public partial class Form1 : Form
{
Socket client;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == null)
button1.Enabled = false;
else
button1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ip, 20000);
client.Connect(ipEndPoint);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] byteData = Encoding.ASCII.GetBytes(textBox1.Text);
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(Send), null);
textBox1.Text = String.Empty;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Send(IAsyncResult ar)
{
try
{
client.EndSend(ar);
}
catch (ObjectDisposedException)
{ }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Upvotes: 0
Views: 1591
Reputation: 30813
In Async
, you got to re-init the BeginReceive
in the server side whenever you want to listen to a message.
Therefore, in your Receive
callback, you should re-init your BeginReceive
after EndReceive
. Otherwise you cannot get the next message:
private void Receive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndReceive(ar);
client.BeginReceive(byteData, 0, byteData.Length, //add BeginReceive again
SocketFlags.None, new AsyncCallback(Receive), client);
textBox1.Invoke(new Action(delegate ()
{
textBox1.AppendText(Environment.NewLine
+ Encoding.ASCII.GetString(byteData));
}));
byteData = null;
}
For more of working example with Async
, check this out: Sending a value from server to client with sockets
Upvotes: 1