i have code in C# between CLIENT and SERVER to collocates the packet lose and time delay the programming is working good in LAN 127.0.0.1 but in WAN is stopping at first PACKET LOSE it doesn’t complete to END loop,,,,,, How can i use THREAD in this code ..???
THANK YOU .....
SERVER CODE
CLIENT CODE
THANK YOU .....
SERVER CODE
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UPDServer
{ class Program
{ static void Main(string[] args)
{ int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7778);
Socket newsock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tmpRemote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
for (int i = 0; i < 100; i++)
{ data = new byte[1024];
recv = newsock.ReceiveFrom(data, ref tmpRemote);
newsock.SendTo(data,data.Length, SocketFlags.None, tmpRemote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
newsock.Close();
Console.ReadLine();
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{ class Program
{ static void Main(string[] args)
{ byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7778);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tmpRemote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string[] sndt = new string [100];
string[] rcvt = new string [100];
Console.WriteLine( "send time ^^" + "receive time ( m:s:ms:tt)");
data = new byte[1024];
for( int i=0 ; i<100 ; i++ )
{ // save time to sndt[] and send the packet .ToString
sndt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
server.SendTo( Encoding.ASCII.GetBytes ("packet NUM:" + i) , ipep);
//recieve the packet and save the time in rcvt[]...........
recv = server.ReceiveFrom(data, ref tmpRemote );
rcvt[i] = DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond + ":" + DateTime.Now.Ticks.ToString();
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv) + "\r=========" + i + "=========\n");
Console.WriteLine(sndt[i]);
Console.WriteLine(rcvt[i]);
}
//}
Console.WriteLine("Stopping client");
server.Close();
Console.ReadLine();
}
}
}
Comment