Note that there are some explanatory texts on larger screens.

plurals
  1. POUDP Socket ReceiveFrom()
    primarykey
    data
    text
    <p>After much failed research I turn to this forum for help.</p> <p>I am planning to make an multiple client udp server and my thirst thoughts was to have 1 thread per client with a blocking method: reciveFrom(). In order to not miss any data sent to the server.</p> <p>I made this test server that would add data receiven to a list and receive data again. This would be a test to see if data would be lost.</p> <p>The test client consist of 6 sockets with diffrent data. Sending data to the server when user click a "send all" button. The button links to a foreach that iterates clients and their senddata method. I press this button about 6 times and the server list do not miss a single message.</p> <p>My question is the reciveFrom() method so fast that I require to have multiple threads in my client in order to see that It does miss data or Is this reciveFrom() method flawless and will pick up all packets? </p> <pre><code>namespace TestServer { class Server { List&lt;Data&gt; storage = new List&lt;Data&gt;(); public Server() { byte[] data = new byte[1024]; int recv; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); socket.Bind(ipep); IPEndPoint sender = new IPEndPoint(IPAddress.Any,0); EndPoint remote = (EndPoint)sender; while (1==1) { data = new byte[1024]; recv = socket.ReceiveFrom(data, ref remote); storage.Add(new Data(recv,data)); } } } struct Data { int recv; byte[] data; public Data(int recv, byte[] data) { this.recv = recv; this.data = data; } } } </code></pre> <p>///////////////////////////////////CLIENT//////////////////////////////////////</p> <pre><code>public partial class Form1 : Form { Client[] clients = new Client[6]; TextBox[] txtServerPort = new TextBox[6]; TextBox[] txtServerIP = new TextBox[6]; public Form1() { InitializeComponent(); clients[0] = new Client(); clients[1] = new Client(); clients[2] = new Client(); clients[3] = new Client(); clients[4] = new Client(); clients[5] = new Client(); txtServerPort[0] = txtServerPortClient1; txtServerPort[1] = txtServerPortClient2; txtServerPort[2] = txtServerPortClient3; txtServerPort[3] = txtServerPortClient4; txtServerPort[4] = txtServerPortClient5; txtServerPort[5] = txtServerPortClient6; txtServerIP[0] = txtServerIPClient1; txtServerIP[1] = txtServerIPClient2; txtServerIP[2] = txtServerIPClient3; txtServerIP[3] = txtServerIPClient4; txtServerIP[4] = txtServerIPClient5; txtServerIP[5] = txtServerIPClient6; } private void btnSendDataClient1_Click(object sender, EventArgs e) { clients[0].SendData(); } private void btnSendDataClient2_Click(object sender, EventArgs e) { clients[1].SendData(); } private void btnSendDataClient3_Click(object sender, EventArgs e) { clients[2].SendData(); } private void btnSendDataClient4_Click(object sender, EventArgs e) { clients[3].SendData(); } private void btnSendDataClient5_Click(object sender, EventArgs e) { clients[4].SendData(); } private void btnSendDataClient6_Click(object sender, EventArgs e) { clients[5].SendData(); } private void btnClientsSendData_Click(object sender, EventArgs e) { foreach (Client c in clients) { c.SendData(); } } private void txtServerIPClient1_TextChanged(object sender, EventArgs e) { UpdateServerInformation(0); } private void txtServerIPClient2_TextChanged(object sender, EventArgs e) { UpdateServerInformation(1); } private void txtServerIPClient3_TextChanged(object sender, EventArgs e) { UpdateServerInformation(2); } private void txtServerIPClient4_TextChanged(object sender, EventArgs e) { UpdateServerInformation(3); } private void txtServerIPClient5_TextChanged(object sender, EventArgs e) { UpdateServerInformation(4); } private void txtServerIPClient6_TextChanged(object sender, EventArgs e) { UpdateServerInformation(5); } private void txtServerPortClient1_TextChanged(object sender, EventArgs e) { UpdateServerInformation(0); } private void txtServerPortClient2_TextChanged(object sender, EventArgs e) { UpdateServerInformation(1); } private void txtServerPortClient3_TextChanged(object sender, EventArgs e) { UpdateServerInformation(2); } private void txtServerPortClient4_TextChanged(object sender, EventArgs e) { UpdateServerInformation(3); } private void txtServerPortClient5_TextChanged(object sender, EventArgs e) { UpdateServerInformation(4); } private void txtServerPortClient6_TextChanged(object sender, EventArgs e) { UpdateServerInformation(5); } private void UpdateServerInformation(int index) { if (txtServerPort[index].Text.Length &gt; 0) { if (txtServerIP[index].Text.Length &gt; 0) { clients[index].SetServer(txtServerIP[index].Text, int.Parse(txtServerPort[index].Text)); } } } private void txtDataClient1_TextChanged(object sender, EventArgs e) { clients[0].SetData(txtDataClient1.Text); } private void txtDataClient2_TextChanged(object sender, EventArgs e) { clients[1].SetData(txtDataClient2.Text); } private void txtDataClient3_TextChanged(object sender, EventArgs e) { clients[2].SetData(txtDataClient3.Text); } private void txtDataClient4_TextChanged(object sender, EventArgs e) { clients[3].SetData(txtDataClient4.Text); } private void txtDataClient5_TextChanged(object sender, EventArgs e) { clients[4].SetData(txtDataClient5.Text); } private void txtDataClient6_TextChanged(object sender, EventArgs e) { clients[5].SetData(txtDataClient6.Text); } } struct ServerInformation { IPEndPoint ipep; public ServerInformation(string ip, int port) { ipep = new IPEndPoint(IPAddress.Parse(ip), port); } public IPEndPoint GetIPEndPoint() { return ipep; } } class Client { ServerInformation server; Socket socket; byte[] data; public Client() { this.data = new byte[1024]; socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } public void SetServer(string ip, int port) { server = new ServerInformation(ip, port); } public void SetData(string data) { this.data = new byte[1024]; this.data = Encoding.ASCII.GetBytes(data); } public void SendData() { socket.SendTo(data,data.Length,SocketFlags.None, server.GetIPEndPoint()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload