因此,我对这个网络的所有方案拟订工作都很新,我有几个问题。
在服务器运行、客户与服务器连接、客户向服务器发送电文后,服务器将其转发给所有客户。 服务器是一种黄色应用,客户是Windows Form Application。
我所看到的错误是我的客户,在我表格的顶端,我有一个文字箱,以用户名和一个纽子来“提交”,并与服务器连接。
任何途径,这都是我顿的连接守则:
private void btnConnect_Click(object sender, EventArgs e)
{
readData = "Connecting to chat server...";
msg();
try
{
sck_client.Connect("127.0.0.1", 8888);
sw = new StreamWriter(sck_client.GetStream());
string toSend = txtUsername.Text;
sw.Write(toSend);
sw.Flush();
chatThread = new Thread(GetMessages);
chatThread.Start();
}
catch (Exception ex)
{
readData = ex.ToString();
msg();
}
}
msg() quite simply takes the value in readData and prints it to the screen (in a richtextbox). sw is a streamwriter that has been declared publicly, outside of the method, so is sck_client (a TcpClient) and chatThread (a Thread).
从根本上说,当我管理我的节目并试图连接时,这个问题就成了一种例外,因为它无法连接起来。 它对案文投了新字:
System.NullReferenceException: Object reference not set to an instance of an object. at Chat_Client.Main.btnConnect_Click(Object sender, EventArgs e) in filepathChat_ClientMain.cs:line36
甚至在我的服务器运行和听从8888港时也是如此。 因此,我应该做些什么来纠正?
如果你需要我更多的法典来解决这个问题,那么,让我在评论中知道,我把它放在后面。
2. 显示该守则在什么地方出现:
public partial class Main : Form // new class for the form itself
{
// all of these are declared outside any method:
TcpClient sck_client = default(TcpClient);
Thread chatThread = default(Thread);
string readData = null;
StreamWriter sw = default(StreamWriter);
StreamReader sr = default(StreamReader);
...