C#客户试图发送信息而不是听,为什么会发生这种情况?
所以我正在编写 c# 客户端服务器程序,其中客户端从服务器接收信息,但是客户端似乎在成功连接后尝试发送信息而不是接收信息,
public void SL_Click(object sender, EventArgs e)
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
MessageBox.Show("Listening for information......");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
string toattk = str.ToString();
我认为这是因为客户端和服务器确实成功连接,但是从服务器发送的消息从未被实际接收到。
服务器连接并将消息发送到另一台计算机时执行操作的代码:
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]));
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception)
{
MessageBox.Show("Could not find other computer.");
}
它们连接没有问题,但从服务器发送的消息永远不会出现在客户端上。我推测这是因为客户端试图发回响应而不是接收服务器发送的消息。还是有其他原因导致电脑连接后收不到消息?谢谢。
So I am writing a c# client server program where the client receives information from the server, however the client appears to try to send information rather than receive after the successfully connect
public void SL_Click(object sender, EventArgs e)
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
MessageBox.Show("Listening for information......");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
string toattk = str.ToString();
I think this, as the client and server do successfully connect, but the the message sent from the server is never actually recieved.
Server code for action when it connects and sends the message to the other computer:
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]));
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception)
{
MessageBox.Show("Could not find other computer.");
}
They connect no problem, but the message sent from the server never appears on the client. I surmise this is because the client is trying to send a response back rather than receiving the message sent by the server. Or are there other reasons why the message is not received despite the computers connected? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论