Re: asynchronous socket problem when connecting to localhost
- From: "Michael Rubinstein" <mSPAM_REMOVEr@mŽubinstein.com>
- Date: Sun, 4 Feb 2007 08:43:53 -0500
Chris, server talking to itself?
You server starts sending right after accepting connection. It is
unusual.Normally a client would send something upon connection to indicate
it is ready, the server would respond after receiving some data. This will
also ensure the packet goes to a correct client.
Michael
<darthghandi@xxxxxxxxx> wrote in message
news:1170531749.150385.275240@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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);
}
}
.
- References:
- asynchronous socket problem when connecting to localhost
- From: darthghandi
- asynchronous socket problem when connecting to localhost
- Prev by Date: Re: Unable to get line-breaks for a JavaScript 'alert' message using ClientScript.RegisterStartUpScript
- Next by Date: OnPaste event from the clipboard
- Previous by thread: asynchronous socket problem when connecting to localhost
- Next by thread: Re: asynchronous socket problem when connecting to localhost
- Index(es):
Relevant Pages
|
|