关于socket异步的问题,帮忙看看

zlxc501 2009-04-21 01:49:11
服务端: private IPEndPoint MyServer;
private Socket sock;
private Socket handler;
private bool bb = true;

private void Form1_Load(object sender, EventArgs e)
{
try
{
MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
toolStripStatusLabel1.Text = "开始监听 ...";
sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
}
catch (Exception ee) { toolStripStatusLabel1.Text = ee.Message; }

Control.CheckForIllegalCrossThreadCalls = false;

}

private void targett()
{
if (handler.Connected)
{
toolStripStatusLabel1.Text = "与客户建立连接。";
while (bb)
{
Byte[] bbb = new Byte[64];
handler.Receive(bbb, bbb.Length, 0);
string ccc = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
string x = "w";
int i = ccc.IndexOf(x);
if (i > 0)
{
saveUser(ccc);
}
richTextBox1.AppendText(ccc + "\r\n");
}
}
}
private void saveUser(string str)
{
...
}
private void AcceptCallback(IAsyncResult ar)
{
toolStripStatusLabel1.Text = "与客户建立连接!";
handler = ((Socket)ar.AsyncState).EndAccept(ar);
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
}

客户端:
private IPEndPoint MyServer;
private Socket sock;
private bool bb = true;

private void Form1_Load(object sender, EventArgs e)
{
try
{
MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(MyServer);
toolStripStatusLabel1.Text = "连接成功!";
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message); }

Control.CheckForIllegalCrossThreadCalls = false;
}

private void targett()
{
while (bb)
{
Byte[] bbb = new Byte[640];
sock.Receive(bbb, bbb.Length, 0);
string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
richTextBox1.AppendText(aaaaa);
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
Byte[] bytee = new Byte[640];
string send = richTextBox2.Text + "\r\n";
bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
sock.Send(bytee, bytee.Length, 0);
}
catch { MessageBox.Show("连接尚未建立! 无法发送!"); }
}

我想在客户端发送一串字符到服务端,服务端判断如果字符中有"w",就执行saveUser(),为什么我发送例如"weeeeee"他还是不执行呢,请大家帮忙看看代码有什么问题。
...全文
40 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝海D鱼 2009-04-21
  • 打赏
  • 举报
回复
还有 服务端 Byte[] bbb = new Byte[64];
客户端 Byte[] bbb = new Byte[640];
蓝海D鱼 2009-04-21
  • 打赏
  • 举报
回复
int i = ccc.IndexOf(x);
if (i > 0)

weeeeee的 i=0
ericzhangbo1982111 2009-04-21
  • 打赏
  • 举报
回复
你代码有问题
看看这个

private Socket client = null;
const int nPortListen = 399;
try
{
TcpListener listener = new TcpListener( nPortListen );
Console.WriteLine( "Listening as {0}", listener.LocalEndpoint );
listener.Start();
do
{
byte [] m_byBuff = new byte[127];
if( listener.Pending() )
{
client = listener.AcceptSocket();
// Get current date and time.
DateTime now = DateTime.Now;
string strDateLine = "Welcome " + now.ToString("G") + "\n\r";

// Convert to byte array and send.
Byte[] byteDateLine =
System.Text.Encoding.ASCII.GetBytes(
strDateLine.ToCharArray() );
client.Send( byteDateLine, byteDateLine.Length, 0 );
}
else
{
Thread.Sleep( 100 );
}
} while( true ); // Don't use this.
}
catch( Exception ex )
{
Console.WriteLine ( ex.Message );
}



http://www.codeproject.com/KB/IP/socketsincs.aspx

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧