473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

asynchronous socket problem when connecting to localhost

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.TheBuff er,
0, moreSocks.TheBuffer.Length, SocketFlags.None, more, moreSocks);
}
}

Feb 3 '07 #1
9 4274
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

<da*********@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
>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.TheBuff er,
0, moreSocks.TheBuffer.Length, SocketFlags.None, more, moreSocks);
}
}

Feb 4 '07 #2
On Feb 4, 6:43 am, "Michael Rubinstein"
<mSPAM_REMOVEr@mŽubinstein.comwrote:
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

<darthgha...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...
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.TheBuff er,
0, moreSocks.TheBuffer.Length, SocketFlags.None, more, moreSocks);
}
}
That was my first thought as well. I am trying to create a server for
an already built client (weird I know, but it's for school and I just
do what I'm told). I tried making the server complete the TCP
connection and then wait for the client to send what it's supposed to
send, but I just keep waiting for it. That's when I tried to send the
info I'm supposed to send first, then listen for the response. That
works at first, then I run into the problem listed above. Maybe I'll
try sending one then waiting for another before I send one again.
We'll see if that works.
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure. Don't know
much about that. If anyone has any info an that, please share.
Thanks,
Chris

Feb 7 '07 #3
Chris, if your client is not correctly written, then there is so much
you can do. The TCP connection is established once the client receives the
CallBack from BeginConnect() or whatever Winsock code it uses. At that point
the client can issue Send() or BeginSend().
>
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure.
>
If you are concerned about security then the firewall is a better place to
address it.

Good luck, Michael
<da*********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Feb 4, 6:43 am, "Michael Rubinstein"
<mSPAM_REMOVEr@mŽubinstein.comwrote:
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

<darthgha...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...
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.TheBuff er,
0, moreSocks.TheBuffer.Length, SocketFlags.None, more, moreSocks);
}
}
That was my first thought as well. I am trying to create a server for
an already built client (weird I know, but it's for school and I just
do what I'm told). I tried making the server complete the TCP
connection and then wait for the client to send what it's supposed to
send, but I just keep waiting for it. That's when I tried to send the
info I'm supposed to send first, then listen for the response. That
works at first, then I run into the problem listed above. Maybe I'll
try sending one then waiting for another before I send one again.
We'll see if that works.
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure. Don't know
much about that. If anyone has any info an that, please share.
Thanks,
Chris
Feb 7 '07 #4
On Tue, 06 Feb 2007 20:19:55 -0800, <da*********@gmail.comwrote:
[...]
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure. Don't know
much about that. If anyone has any info an that, please share.
I don't see why using "any" for your IP address is inherently any less
secure than using a specific IP address. And it's the only way for you to
allow a single socket to accept connections on more than one IP address.
Feb 7 '07 #5
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 06 Feb 2007 20:19:55 -0800, <da*********@gmail.comwrote:
>[...]
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure. Don't know
much about that. If anyone has any info an that, please share.

I don't see why using "any" for your IP address is inherently any less
secure than using a specific IP address. And it's the only way for you to
allow a single socket to accept connections on more than one IP address.
I agree - the only time using "Any" (0.0.0.0) would be insecure is when one
of your IP Addresses is considered insecure. For example, often I want to
start a service process up (say, IIS for testing) and have it ONLY listen on
127.0.0.1 - this helps mitigate the threat surface.

If I have IIS listen on my main ip (192.168.100.100, say), then I'm now
allowing anyone to come attack it.

If the goal is to have my socket app listen on all sockets, then there's no
security difference that I can see between binding to the "any" address, or
enumerating the addresses and binding to each invividually.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins
Feb 8 '07 #6
On Feb 8, 12:19 pm, "Chris Mullins [MVP]" <cmull...@yahoo.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...
On Tue, 06 Feb 2007 20:19:55 -0800, <darthgha...@gmail.comwrote:
[...]
On another note, does anyone know how to listen on localhost and all
the given IP's all at once? The only way I have found to do this is
to use IPAdress.Any, but I heard that this wasn't secure. Don't know
much about that. If anyone has any info an that, please share.
I don't see why using "any" for your IP address is inherently any less
secure than using a specific IP address. And it's the only way for you to
allow a single socket to accept connections on more than one IP address.

I agree - the only time using "Any" (0.0.0.0) would be insecure is when one
of your IP Addresses is considered insecure. For example, often I want to
start a service process up (say, IIS for testing) and have it ONLY listen on
127.0.0.1 - this helps mitigate the threat surface.

If I have IIS listen on my main ip (192.168.100.100, say), then I'm now
allowing anyone to come attack it.

If the goal is to have my socket app listen on all sockets, then there's no
security difference that I can see between binding to the "any" address, or
enumerating the addresses and binding to each invividually.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#http://www.coversant.net/blogs/cmullins
Thanks for the help. I solved the problem by sending ignored data
inbetween recieves. I was just wondering if anyone knew of anything I
should look out for when doing this.
Thanks,

Feb 10 '07 #7
On Sat, 10 Feb 2007 10:08:20 -0800, <da*********@gmail.comwrote:
Thanks for the help. I solved the problem by sending ignored data
inbetween recieves. I was just wondering if anyone knew of anything I
should look out for when doing this.
Yes. Sending extra "ignored data" is a hack and there's no reason you
should need to do it. If you do, it means you have an unsolved bug in
your own code. Adding a new bug to your code in order to avoid a
different bug already in your code is not a good programming practice.

Pete
Feb 10 '07 #8
On Feb 10, 12:44 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 10 Feb 2007 10:08:20 -0800, <darthgha...@gmail.comwrote:
Thanks for the help. I solved the problem by sending ignored data
inbetween recieves. I was just wondering if anyone knew of anything I
should look out for when doing this.

Yes. Sending extra "ignored data" is a hack and there's no reason you
should need to do it. If you do, it means you have an unsolved bug in
your own code. Adding a new bug to your code in order to avoid a
different bug already in your code is not a good programming practice.

Pete
After many moons, I found my unsolved bug. It seems the socket had
already given me the packet I was looking for combined with the
earlier packet. I had (wrongly) assumed that when I did a
BeginRecieve(), I would just get one message in the buffer instead of
two. Now I am rewriting the code to support multiple messages in one
buffer.
Thanks again for your time,
Chris

Feb 17 '07 #9
JR
Sockets send and receive bytes, not messages. This is a common mistake. The
receiver needs to split the byte stream into messages.

In practive, for shorter messages with some delay between them, you will
often receive complete messages each time.

JR

<da*********@gmail.com???
??????:11**********************@l53g2000cwa.google groups.com...
On Feb 10, 12:44 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>On Sat, 10 Feb 2007 10:08:20 -0800, <darthgha...@gmail.comwrote:
Thanks for the help. I solved the problem by sending ignored data
inbetween recieves. I was just wondering if anyone knew of anything I
should look out for when doing this.

Yes. Sending extra "ignored data" is a hack and there's no reason you
should need to do it. If you do, it means you have an unsolved bug in
your own code. Adding a new bug to your code in order to avoid a
different bug already in your code is not a good programming practice.

Pete

After many moons, I found my unsolved bug. It seems the socket had
already given me the packet I was looking for combined with the
earlier packet. I had (wrongly) assumed that when I did a
BeginRecieve(), I would just get one message in the buffer instead of
two. Now I am rewriting the code to support multiple messages in one
buffer.
Thanks again for your time,
Chris

Feb 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: philippe Tressard | last post by:
Hi, I've 2 instances of apache 1.3.27 with php 4.2.3 and mysql 3.23.42 running on the same server. The 1st apache listens the port number 80 and communicates with the 1st mysql with the...
2
by: news.microsoft.com | last post by:
Hi I write dll library which one of it component will be Net socket communication. Communication is working very good, but i've got problem when client is connecting. When server is started,...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
8
by: panko | last post by:
Hello, I can't manage with asynchronous socket communication. :( I wrote a class CSocket.cs. This class is taking care of sending strings to LED display. This display is actually communicating...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
2
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when...
2
by: SvenV | last post by:
I based my program on the asynchronous client/server example on msdn. There weren't too many modifications to the original code. I just created some extra code to response to different messages. F.e....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.