I am trying to create a server application using asynchronous
sockets. I run into a problem when I try to connect to my server
using a non-.net program. I can establish the connection, and send
some packets in response to the programs first message, but when I
receive a response back, it's the last packet I sent. After that
packet, I receive the packet I really wanted. This only happens when
I connect with local host. I tried connecting with that same program
from another computer, and there is no problem. Any ideas?
Thanks,
Chris
Here's some of the code:
public void Bind(int l_port)
{;
m_port = l_port;
IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
IPAddress ipAddress = ipHostInfo.Addr essList[1];
//m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
m_mainSocket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
m_mainSocket.Bi nd(m_tcpEndPoin t);
m_mainSocket.Li sten(100);
m_callBack = new AsyncCallback(O nClientConnect) ;
m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
}
public void OnClientConnect (IAsyncResult ar)
{
try
{
Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
m_workSocks[sockCount] =
stateOfRecieveS ock.EndAccept(a r);
OnSend(m_workSo cks[sockCount]);
sockCount++;
AsyncCallback recievedData = new
AsyncCallback(O nClientConnect) ;
stateOfRecieveS ock.BeginAccept (recievedData,
stateOfRecieveS ock);
}
catch (Exception ex)
{
throw new Exception("Ther e was a problem receiving
data.", ex);
}
}
//SendMessage is called after receiving a packet from the
client and sending one as well
public void SendMessage(IOB uffer stateOfTheSock)
{
if (stateOfTheSock .TheSocket.Conn ected)
{
//add data to the buffer
AsyncCallback callMe = new
AsyncCallback(G otMoreData);
stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
stateOfTheSock) ;
}
public void GotMoreData(IAs yncResult ar)
{
IOBuffer moreSocks = (IOBuffer)ar.As yncState;
if (moreSocks.TheS ocket.Connected )
{
moreSocks.Bytes Received =
moreSocks.TheSo cket.EndReceive (ar);
moreSocks.Clear Buffer();
AsyncCallback more = new AsyncCallback(G otMoreData);
moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
}
}
sockets. I run into a problem when I try to connect to my server
using a non-.net program. I can establish the connection, and send
some packets in response to the programs first message, but when I
receive a response back, it's the last packet I sent. After that
packet, I receive the packet I really wanted. This only happens when
I connect with local host. I tried connecting with that same program
from another computer, and there is no problem. Any ideas?
Thanks,
Chris
Here's some of the code:
public void Bind(int l_port)
{;
m_port = l_port;
IPHostEntry ipHostInfo = Dns.GetHostEntr y("");
IPAddress ipAddress = ipHostInfo.Addr essList[1];
//m_tcpEndPoint = new IPEndPoint(IPAd dress.Any, m_port);
m_tcpEndPoint = new IPEndPoint(ipAd dress, m_port);
m_mainSocket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
m_mainSocket.Bi nd(m_tcpEndPoin t);
m_mainSocket.Li sten(100);
m_callBack = new AsyncCallback(O nClientConnect) ;
m_mainSocket.Be ginAccept(m_cal lBack, m_mainSocket);
}
public void OnClientConnect (IAsyncResult ar)
{
try
{
Socket stateOfRecieveS ock = (Socket)ar.Asyn cState;
m_workSocks[sockCount] =
stateOfRecieveS ock.EndAccept(a r);
OnSend(m_workSo cks[sockCount]);
sockCount++;
AsyncCallback recievedData = new
AsyncCallback(O nClientConnect) ;
stateOfRecieveS ock.BeginAccept (recievedData,
stateOfRecieveS ock);
}
catch (Exception ex)
{
throw new Exception("Ther e was a problem receiving
data.", ex);
}
}
//SendMessage is called after receiving a packet from the
client and sending one as well
public void SendMessage(IOB uffer stateOfTheSock)
{
if (stateOfTheSock .TheSocket.Conn ected)
{
//add data to the buffer
AsyncCallback callMe = new
AsyncCallback(G otMoreData);
stateOfTheSock. TheSocket.Begin Send(stateOfThe Sock.TheBuffer, 0,
stateOfTheSock. TheBuffer.Lengt h, SocketFlags.Non e, callMe,
stateOfTheSock) ;
}
public void GotMoreData(IAs yncResult ar)
{
IOBuffer moreSocks = (IOBuffer)ar.As yncState;
if (moreSocks.TheS ocket.Connected )
{
moreSocks.Bytes Received =
moreSocks.TheSo cket.EndReceive (ar);
moreSocks.Clear Buffer();
AsyncCallback more = new AsyncCallback(G otMoreData);
moreSocks.TheSo cket.BeginRecei ve(moreSocks.Th eBuffer,
0, moreSocks.TheBu ffer.Length, SocketFlags.Non e, more, moreSocks);
}
}
Comment