编辑
40+ 的观点,只有一个能够提供帮助,这个问题不值得点赞吗?;)
/编辑
再次,我在 c# 中的套接字编程有一些问题。
我用自制控制台(richTextBox)设置了一个小服务器,它正在监听多个连接。
一切正常,除了如果我关闭一个连接的客户端,程序会给我一个异步错误,并且在控制台上它不断地从客户端写入一条空白消息(事件:ClientReceivedHandler)
所以任何人都可以帮我找到问题吗?
我的代码:
服务器 - Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace GameServer {
public partial class Form1 : Form {
// ### MAIN
public bool serverRunning = false;
public int serverListenPort = 6666;
static Listener l;
public Form1() {
InitializeComponent();
l = new Listener( serverListenPort );
l.SocketAccepted += new Listener.SocketAcceptedHandler( l_SocketAccepted );
}
void l_SocketAccepted( Socket e ) {
Client client = new Client( e );
client.Received += new Client.ClientReceivedHandler( client_Received );
client.Disconnected += new Client.ClientDisconnectedHandler( client_Disconnected );
Invoke( ( MethodInvoker )delegate {
dataGridViewConnections.Rows.Add( client.ID, client.EndPoint.ToString() );
consoleWrite( DateTime.Now + " - " + client.EndPoint + " connected to server.\n\n", Color.Lime );
} );
}
void client_Received( Client sender, byte[] data ) {
Invoke( ( MethodInvoker )delegate {
consoleWrite( DateTime.Now + "-" + sender.EndPoint + " :\n" + Encoding.Default.GetString( data ) + "\n\n", Color.White );;
} );
}
void client_Disconnected( Client sender ) {
Invoke( ( MethodInvoker )delegate {
for( int i = 0; i < dataGridViewConnections.Rows.Count; i++ ) {
if( dataGridViewConnections.Rows[i].Cells[0].Value.ToString() == sender.ID.ToString() ) {
dataGridViewConnections.Rows.RemoveAt( i );
consoleWrite( DateTime.Now + " - " + sender.EndPoint + " disconnected from server.\n\n", Color.OrangeRed );
break;
}
}
} );
}
private void checkBox1_Click(object sender, EventArgs e) {
checkBox1.Enabled = false;
if( !serverRunning ) {
consoleWrite( DateTime.Now + " " + markerSystem + "start\n", Color.White );
ServerStart();
checkBox1.Text = "Stop";
} else {
consoleWrite( DateTime.Now + " " + markerSystem + "stop\n", Color.White );
ServerStop();
checkBox1.Text = "Start";
}
checkBox1.Enabled = true;
}
private void ServerStart() {
if( !serverRunning ) {
consoleWrite( "* Starting server . . .\n", Color.Orange );
// Start Server
l.Start();
serverRunning = true;
consoleWrite( "* Server started !\n", Color.Lime );
consoleWrite("Listening on port " + serverListenPort + ".\n\n", Color.White );
} else {
consoleWrite( "* ERROR: Server already started !\n\n", Color.Red );
}
}
private void ServerStop() {
if( serverRunning ) {
consoleWrite( "* Stopping server . . .\n", Color.Orange );
// Stop Server
l.Stop();
serverRunning = false;
consoleWrite( "* Server stopped !\n\n", Color.Lime );
} else {
consoleWrite( "* ERROR: Server already stopped !\n\n", Color.Red );
}
}
private string markerSystem = "@System -> ";
private string marker = "-> ";
private void consoleWrite( string text, Color color ) {
consoleText.SelectionStart = consoleText.Text.Length;
consoleText.SelectionLength = 0;
consoleText.SelectionColor = color;
consoleText.AppendText( text );
}
}
}
服务器 - Listener.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace GameServer {
class Listener {
Socket s;
public bool Listening {
get;
private set;
}
public int Port {
get;
private set;
}
public Listener( int port ) {
Port = port;
s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
}
public void Start() {
if( Listening ) {
return;
}
s.Bind( new IPEndPoint( 0, Port ) );
s.Listen(0);
s.BeginAccept( callback, null );
Listening = true;
}
public void Stop() {
if( !Listening ) {
return;
}
s.Close();
s.Dispose();
s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
}
void callback( IAsyncResult ar ) {
try {
Socket s = this.s.EndAccept( ar );
if( SocketAccepted != null ) {
SocketAccepted( s );
}
this.s.BeginAccept( callback, null );
} catch( Exception ex ) {
MessageBox.Show( ex.Message );
}
}
public delegate void SocketAcceptedHandler( Socket e );
public event SocketAcceptedHandler SocketAccepted;
}
}
服务器-Client.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace GameServer {
class Client {
public string ID {
get;
private set;
}
public IPEndPoint EndPoint {
get;
private set;
}
Socket sck;
public Client( Socket accepted ) {
sck = accepted;
ID = Guid.NewGuid().ToString();
EndPoint = ( IPEndPoint )sck.RemoteEndPoint;
sck.BeginReceive( new byte[] { 0 }, 0, 0, 0, callback, null );
}
void callback( IAsyncResult ar ) {
try {
sck.EndReceive( ar );
byte[] buf = new byte[8192];
int rec = sck.Receive( buf, buf.Length, 0 );
if( rec < buf.Length ) {
Array.Resize<byte>( ref buf, rec );
}
if( Received != null ) {
Received( this, buf );
}
sck.BeginReceive( new byte[] { 0 }, 0, 0, 0, callback, null );
} catch( Exception ex ) {
MessageBox.Show( ex.Message );
Close();
if( Disconnected != null ) {
Disconnected( this );
}
}
}
public void Close() {
sck.Close();
sck.Dispose();
}
public delegate void ClientReceivedHandler( Client sender, byte[] data );
public delegate void ClientDisconnectedHandler( Client sender );
public event ClientReceivedHandler Received;
public event ClientDisconnectedHandler Disconnected;
}
}
客户 - Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace GameClient {
public partial class Form1:Form {
Socket sck;
public Form1() {
InitializeComponent();
sck = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
sck.Connect( "127.0.0.1", 6666 );
}
private void button1_Click( object sender, EventArgs e ) {
int s = sck.Send( Encoding.Default.GetBytes( textBox1.Text ) );
if( s > 0 ) {
textBox1.Text = "";
}
}
private void button2_Click( object sender, EventArgs e ) {
sck.Close();
sck.Dispose();
}
}
}
我真的很高兴能让这个服务器稳定,这样我就可以开始实现多人游戏服务器逻辑了:D
感谢所有可以帮助我的人。
PS 如果有人需要查看实际问题,我可以上传 VS 项目文件或服务器和客户端的 .exe 文件。
更新 :
第一个问题(异步错误)出现在 try-catch 的 Listener.cs 文件中。catch 部分中的消息框给出了以下文本:
“IAsyncResult 对象未从相应的异步方法返回。参数:asyncResult”
这在调用“ServerStop()”方法时出现。
第二个问题是 SERVER-Form1.cs 中的 client_Received() 方法中的任何地方。
好像是不断收到空白数据,所以在console/richTextBox输出空白信息
我不熟悉 c# 中的套接字逻辑,所以我无法弄清楚代码中发生位置错误的位置。
希望有人找到它。
编辑 :
zip 文件中的项目文件 http://ace-acid.no-ip.org/GameServer/
(C)