【发布时间】:2022-06-08 07:09:05
【问题描述】:
所以这是我用 c# 编写的基于 TCP 的聊天服务器的代码。通过 ipv4 连接两台计算机进行聊天没有问题,但我希望服务器程序监听并接受,然后向加入的计算机发送“成功加入”消息。
我想知道有没有办法改变这个代码来做到这一点?
服务器代码:
IPAddress ipAd = IPAddress.Parse(IPV4.Text); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
MessageBox.Show(\"The server is running at port 8001...\");
MessageBox.Show(\"The local End point is :\" + myList.LocalEndpoint);
MessageBox.Show(\"Looking for other computer\");
Socket s = myList.AcceptSocket();
Console.WriteLine(\"Found buddy \" + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
MessageBox.Show(\"The message \" + satt.Text + \" was sent to the computer with IP address \" + IPV4.Text);
byte[] b = new byte[100];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
/* clean up */
s.Close();
myList.Stop();
客户端代码:
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(RecieveIPAdd.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show(\"Connected\");
Stream stm = tcpclnt.GetStream();
MessageBox.Show(\"Listening for attack information......\");
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
string atk = Encoding.UTF8.GetString(bb.AsSpan(0, k));
谢谢你。
// Code after help
private void Connectnattk_DoWork(object sender, DoWorkEventArgs e)
{
{
{
try
{
IPAddress ipAd = IPAddress.Parse(IPV4.Text); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
MessageBox.Show(\"The server is running at port 8001...\");
MessageBox.Show(\"The local End point is :\" + myList.LocalEndpoint);
MessageBox.Show(\"Looking for other computer\");
Socket s = myList.AcceptSocket();
Console.WriteLine(\"Found buddy \" + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
MessageBox.Show(\"The command \" + satt.Text + \" was sent to the computer with IP address \" + IPV4.Text);
byte[] b = new byte[100];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
void ServerStart()
{
TcpListener listener = new TcpListener(IPAddress.Any, 8001);
listener.Start();
Console.WriteLine(\"Listening on port: 8001\");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(state => HandleConnection(client));
}
}
void HandleConnection(TcpClient client)
{
Socket s = myList.AcceptSocket();
Console.WriteLine(\"Found buddy \" + s.RemoteEndPoint);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(satt.Text));
Console.WriteLine(\"Number of connected buddies: \" + connectedbuddies++);
client.Close();
}
/* clean up */
s.Close();
myList.Stop();
}
-
请注意,当您进行 TCP 接收时,您必须循环直到接收到所有数据。这也意味着您必须知道消息的大小
-
一个字符/一个字母。
-
您的客户端代码在接收中显示 100,您是说您只发送一个字节的消息吗?