0
我有問題。爲什麼我的客戶端無法從服務器接收數據?服務器從客戶端接收數據沒有問題。是否因爲我的客戶端沒有連接到我的服務器客戶端上。有人有任何想法?對於我的英語感到抱歉,我來自捷克共和國。 :)UDP服務器和UDP客戶端
這是我的UDP客戶端:
UdpClient client;
public IPAddress serverIP = IPAddress.Parse("127.0.0.1");
public Form1()
{
InitializeComponent();
client = new UdpClient();
}
public void SendData()
{
client.Connect(serverIP, 3000);
byte[] data = Encoding.ASCII.GetBytes("Hi, I'm new client.");
client.Send(data, data.Length);
DoListening();
}
public void DoListening()
{
IPEndPoint adress = new IPEndPoint(serverIP, 3000);
byte[] receivedbytes = client.Receive(ref adress);
string recieved = Encoding.ASCII.GetString(receivedbytes);
MessageBox.Show("Recieved: " + recieved);
}
private void button1_Click(object sender, EventArgs e)
{
SendData();
}
這是我的UDP服務器:
public Form1()
{
InitializeComponent();
Thread listening = new Thread(new ThreadStart(DoListening));
listening.Start();
}
public void ClientThread(Object adress)
{
IPEndPoint ip = adress as IPEndPoint;
UdpClient client = new UdpClient();
client.Connect(ip);
byte[] data = Encoding.ASCII.GetBytes("No nazdar");
client.Send(data, data.Length);
MessageBox.Show("Sending data..");
}
public void DoListening()
{
while (true)
{
UdpClient client = null;
client = new UdpClient(3000);
IPEndPoint host = new IPEndPoint(IPAddress.Any, 0);
MessageBox.Show("Listening");
byte[] receivedbytes = client.Receive(ref host);
string recieved = Encoding.ASCII.GetString(receivedbytes);
MessageBox.Show("Client " + host.Address.ToString() + " conected. Message: " + recieved);
new Thread(new ParameterizedThreadStart(ClientThread)).Start(host);
Console.WriteLine("Doslo k vyjimce z duvodu : {0}", ex.SocketErrorCode);
}
}
可能的重複:http://stackoverflow.com/questions/16919469/c-sharp-application-simply-not-receiving-udp-data#comment24423243_16919469 – C4stor
不,我正在看這個線程,它不是同一個問題。請不要告訴我什麼是可能的重複,只是回答我的線索請求.. – Naxmars
所以沒有人知道? – Naxmars