D
darthghandi
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.GetHostEntry("");
IPAddress ipAddress = ipHostInfo.AddressList[1];
//m_tcpEndPoint = new IPEndPoint(IPAddress.Any, m_port);
m_tcpEndPoint = new IPEndPoint(ipAddress, m_port);
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_mainSocket.Bind(m_tcpEndPoint);
m_mainSocket.Listen(100);
m_callBack = new AsyncCallback(OnClientConnect);
m_mainSocket.BeginAccept(m_callBack, m_mainSocket);
}
public void OnClientConnect(IAsyncResult ar)
{
try
{
Socket stateOfRecieveSock = (Socket)ar.AsyncState;
m_workSocks[sockCount] =
stateOfRecieveSock.EndAccept(ar);
OnSend(m_workSocks[sockCount]);
sockCount++;
AsyncCallback recievedData = new
AsyncCallback(OnClientConnect);
stateOfRecieveSock.BeginAccept(recievedData,
stateOfRecieveSock);
}
catch (Exception ex)
{
throw new Exception("There was a problem receiving
data.", ex);
}
}
//SendMessage is called after receiving a packet from the
client and sending one as well
public void SendMessage(IOBuffer stateOfTheSock)
{
if (stateOfTheSock.TheSocket.Connected)
{
//add data to the buffer
AsyncCallback callMe = new
AsyncCallback(GotMoreData);
stateOfTheSock.TheSocket.BeginSend(stateOfTheSock.TheBuffer, 0,
stateOfTheSock.TheBuffer.Length, SocketFlags.None, callMe,
stateOfTheSock);
}
public void GotMoreData(IAsyncResult ar)
{
IOBuffer moreSocks = (IOBuffer)ar.AsyncState;
if (moreSocks.TheSocket.Connected)
{
moreSocks.BytesReceived =
moreSocks.TheSocket.EndReceive(ar);
moreSocks.ClearBuffer();
AsyncCallback more = new AsyncCallback(GotMoreData);
moreSocks.TheSocket.BeginReceive(moreSocks.TheBuffer,
0, moreSocks.TheBuffer.Length, SocketFlags.None, 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.GetHostEntry("");
IPAddress ipAddress = ipHostInfo.AddressList[1];
//m_tcpEndPoint = new IPEndPoint(IPAddress.Any, m_port);
m_tcpEndPoint = new IPEndPoint(ipAddress, m_port);
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_mainSocket.Bind(m_tcpEndPoint);
m_mainSocket.Listen(100);
m_callBack = new AsyncCallback(OnClientConnect);
m_mainSocket.BeginAccept(m_callBack, m_mainSocket);
}
public void OnClientConnect(IAsyncResult ar)
{
try
{
Socket stateOfRecieveSock = (Socket)ar.AsyncState;
m_workSocks[sockCount] =
stateOfRecieveSock.EndAccept(ar);
OnSend(m_workSocks[sockCount]);
sockCount++;
AsyncCallback recievedData = new
AsyncCallback(OnClientConnect);
stateOfRecieveSock.BeginAccept(recievedData,
stateOfRecieveSock);
}
catch (Exception ex)
{
throw new Exception("There was a problem receiving
data.", ex);
}
}
//SendMessage is called after receiving a packet from the
client and sending one as well
public void SendMessage(IOBuffer stateOfTheSock)
{
if (stateOfTheSock.TheSocket.Connected)
{
//add data to the buffer
AsyncCallback callMe = new
AsyncCallback(GotMoreData);
stateOfTheSock.TheSocket.BeginSend(stateOfTheSock.TheBuffer, 0,
stateOfTheSock.TheBuffer.Length, SocketFlags.None, callMe,
stateOfTheSock);
}
public void GotMoreData(IAsyncResult ar)
{
IOBuffer moreSocks = (IOBuffer)ar.AsyncState;
if (moreSocks.TheSocket.Connected)
{
moreSocks.BytesReceived =
moreSocks.TheSocket.EndReceive(ar);
moreSocks.ClearBuffer();
AsyncCallback more = new AsyncCallback(GotMoreData);
moreSocks.TheSocket.BeginReceive(moreSocks.TheBuffer,
0, moreSocks.TheBuffer.Length, SocketFlags.None, more, moreSocks);
}
}