C# 聊天程序 Lidgren
我编写了以下程序,该程序应该启动、显示表单并连接到服务器并获取消息。但是当我启动时什么也没有发生?
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 Lidgren.Network;
using System.Threading;
namespace WindowsGame2
{
public partial class Form1 : Form
{
private NetClient client;
private NetBuffer buffer;
public Form1()
{
InitializeComponent();
}
private void Connect()
{
NetConfiguration config = new NetConfiguration("xesh");
NetClient client = new NetClient(config);
client.Connect("75.127.105.216", 14242);
NetBuffer buffer = client.CreateBuffer();
}
private void ReceiveMessages()
{
Connect();
bool keepGoing = true;
while (keepGoing)
{
NetMessageType type;
while (client.ReadMessage(buffer, out type))
{
switch (type)
{
case NetMessageType.DebugMessage:
Console.WriteLine(buffer.ReadString());
break;
case NetMessageType.StatusChanged:
Console.WriteLine("New status: " + client.Status + " Reason: " + buffer.ReadString());
break;
case NetMessageType.Data:
break;
}
}
}
}
private void Update(string str)
{
ReceiveMessages();
textBox1.AppendText(str + "\r\n");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
static void Main(string[] args)
{
Form1 form = new Form1();
}
}
}
I wrote the following program that is suppose to start up, show the form and connect to the server and get an messages. However when I start it nothing happens?
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 Lidgren.Network;
using System.Threading;
namespace WindowsGame2
{
public partial class Form1 : Form
{
private NetClient client;
private NetBuffer buffer;
public Form1()
{
InitializeComponent();
}
private void Connect()
{
NetConfiguration config = new NetConfiguration("xesh");
NetClient client = new NetClient(config);
client.Connect("75.127.105.216", 14242);
NetBuffer buffer = client.CreateBuffer();
}
private void ReceiveMessages()
{
Connect();
bool keepGoing = true;
while (keepGoing)
{
NetMessageType type;
while (client.ReadMessage(buffer, out type))
{
switch (type)
{
case NetMessageType.DebugMessage:
Console.WriteLine(buffer.ReadString());
break;
case NetMessageType.StatusChanged:
Console.WriteLine("New status: " + client.Status + " Reason: " + buffer.ReadString());
break;
case NetMessageType.Data:
break;
}
}
}
}
private void Update(string str)
{
ReceiveMessages();
textBox1.AppendText(str + "\r\n");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
static void Main(string[] args)
{
Form1 form = new Form1();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已声明:
但是您的 Connect() 方法构造了新的本地客户端和缓冲区,当该方法返回时,它们将超出范围。实例的客户端和缓冲区永远不会被初始化,因此在 ReceiveMessages() 方法中使用时没有任何意义。
You have declared:
However your Connect() method constructs new local client and buffer which will be out of scope when the method returns. The instance's client and buffer never get initialized and there fore will not be meaningful when used in the ReceiveMessages() method.
你没有调用你的方法。
尝试:
您的表单上有按钮吗?有点需要更多信息
You aren't calling your methods.
try:
Do you have any buttons on your form? Kinda need a bit more information