I tested this code and worked fine with my laptop (host). So, I tried to run my laptop as host and get file with my desktop but always get a timeout error.
here is the code:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.IO;
namespace TFTP_Server
{
class CRRQ
{
EndPoint m_PointDistantRRQ;
string m_strFichierRRQ;
public void SetPointDistant(EndPoint PointDistant)
{
m_PointDistantRRQ = PointDistant;
}
public void SetFichier(string NomFichier)
{
m_strFichierRRQ = NomFichier;
}
public void MonThreadRRQ()
{
Socket socket;
EndPoint PointLocal = new IPEndPoint(0,0);
FileStream m_fs;
int nTimeout = 0;
int NbNack =0;
int test = 0; //
bool bRead;
int nOctetsLu;
int NBloc = 0;
byte[] bTrame;
byte[] bNack = { 0,5, 0, 0 };
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(PointLocal);
if (!File.Exists(m_strFichierRRQ))
{
socket.SendTo(bNack, 4, SocketFlags.None, m_PointDistantRRQ);
return;
}
m_fs = File.Open(m_strFichierRRQ, FileMode.Open, FileAccess.Read, FileShare.Read);
do
{
bTrame = new byte[516];
nOctetsLu = m_fs.Read(bTrame, 4, 512);
NBloc++;
if (NBloc > 65535)
NBloc = 0;
byte[] bNBock = new byte[] { (byte)(NBloc >> 8), (byte)(NBloc & 0xFF) };
bTrame[0] = 0;
bTrame[1] = 3;
bTrame[2] = bNBock[0];
bTrame[3] = bNBock[1];
do
{
socket.SendTo(bTrame, nOctetsLu+4, SocketFlags.None, m_PointDistantRRQ);
bRead = socket.Poll(5000000, SelectMode.SelectRead);
if (!bRead) [COLOR=green]//I'm stuck at this[/COLOR]
nTimeout++; [COLOR=green]//timeout using my desktop[/COLOR]
else
{
socket.ReceiveFrom(bTrame, ref m_PointDistantRRQ);
if (bTrame[0] != 0 && bTrame[1] != 4)
NbNack++;
}
}
while (bRead == false && nTimeout < 10 && NbNack < 3);
}
while (nOctetsLu == 512 && nTimeout < 10 && NbNack < 3);
if (NbNack == 3)
socket.SendTo(bNack, 4, SocketFlags.None, m_PointDistantRRQ);
test = bTrame[3];
m_fs.Close();
socket.Close();
}
}
}