Socket接了一次数据后,不能再接了,怎么回事?
服务端:
private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(get));
thread.Start();
}
private void get()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(getip()), 10);
socket.Bind(ipe);
isStart = true;
byte[] buff = new byte[1024];
while (isStart)
{
socket.Listen(10);
Socket conSocket = socket.Accept();
conSocket.Receive(buff);
string s = Encoding.Default.GetString(buff);
updateui(s); //在textbox显示
Thread.Sleep(200);
}
}
客户端:
private void button1_Click(object sender, EventArgs e)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe=new IPEndPoint(IPAddress.Parse( "192.168.0.3 "),10);
socket.Connect(ipe);
if (socket.Connected)
{
MessageBox.Show( "链接上了! ");
}
else
{
MessageBox.Show( "没有链接! ");
}
}
private void button2_Click(object sender, EventArgs e)
{
string s = this.richTextBox1.Text.Trim();
byte[] buff = Encoding.Default.GetBytes(s);
socket.Send(buff);
}
第一次点击发送后,正常,可以显示信息,
但第二次后就不能正常显示了,
我在updateui那设断点,
发现第二次点击后都不能执行到那,这是为什么啊?
怎么解决啊?大侠们帮忙啊!急!
分不够可以加!
[解决办法]
有点乱建意楼主重新整理一下代码
你这是Socket异步传输吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace YBFWQ
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse( "127.0.0.1 ");
private IPEndPoint MyServer;
private Socket mySocket;
private Socket handler;
private static ManualResetEvent myReset = new ManualResetEvent(false);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
IPHostEntry myHost = new IPHostEntry();
myHost = Dns.GetHostByName(textBox1.Text);
string IPsting = myHost.AddressList[0].ToString();
myIP = IPAddress.Parse(IPsting);
}
catch { MessageBox.Show( "您输入的IP地址格式不正确,请重新输入! "); }
try
{
MyServer = new IPEndPoint(myIP, Int32.Parse(textBox2.Text));
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(MyServer);
mySocket.Listen(50);
textBox3.AppendText( "主机 " + textBox1.Text + "端口 " + textBox2.Text + "开始监听...\r\n ");
Thread thread = new Thread(new ThreadStart(target));//为什么要启动个线程呢????
thread.Start();
}
catch (Exception ee) { textBox3.AppendText(ee.Message + "\r\n "); }
}
private void target()
{
while (true)
{
//设为终止
myReset.Reset();
mySocket.BeginAccept(new AsyncCallback(AcceptCallback), mySocket);
//阻塞当前线程
myReset.WaitOne();
}
}
private void AcceptCallback(IAsyncResult ar)
{
//将事件设为终止
myReset.Set();
Socket listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
textBox3.AppendText( "与客户建立连接。\r\n ");
try
{
byte[] byteDate = System.Text.Encoding.BigEndianUnicode.GetBytes( "已经准备好,请通话!\r\n ");
handler.BeginSend(byteDate, 0, byteDate.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
Thread thread = new Thread(new ThreadStart(begReceive));
thread.Start();
}
private void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception eee)
{
MessageBox.Show(eee.Message);
}
}
private void begReceive()
{
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket tt = state.workSocket;
int bytesRead = handler.EndReceive(ar);
state.sb.Append(System.Text.Encoding.BigEndianUnicode.GetString(state.buffer, 0, bytesRead));
string content = state.sb.ToString();
state.sb.Remove(0, content.Length);
richTextBox1.AppendText(content + "\r\n ");
tt.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes(richTextBox2.Text + "\n\r ");
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void button3_Click(object sender, EventArgs e)
{
try
{
mySocket.Close();
textBox3.AppendText( "主机 " + textBox1.Text + "端口 " + textBox2.Text + "监听停止!\r\n ");
}
catch { MessageBox.Show( "监听尚未开始,关闭无效! "); }
}
}
}
[解决办法]
private void target()
{
while (true)
{
//设为终止
myReset.Reset();
mySocket.BeginAccept(new AsyncCallback(AcceptCallback), mySocket);
//阻塞当前线程
myReset.WaitOne();
}
}
线程回调,和异步回调的。这是我照书上的例子,你看看吧?