By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,832 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,832 IT Pros & Developers. It's quick & easy.

Keep getting this dang socket exception...

P: n/a
Hey guys. I'm creating a file transfer app. Anyways, I'm using this
code...

SERVER
<code>Dim Hostname As String = Dns.GetHostName
Dim IP As String =
Dns.GetHostByName(Hostname).AddressList(0).ToStrin g

Public Sub RunServer()
Dim listener As TcpListener

Try
listener = New TcpListener(IPAddress.Parse(IP), 5001)
'wait for connection request
listener.Start()

'extablish connection upon client request

lblStatus.Text = "Waiting for connection..."

'accept an incoming connection
connection = listener.AcceptSocket

'create networkstream object associated with socket
socketStream = New NetworkStream(connection)</code>...

CLIENT
<code>
Dim client As TcpClient

Try

'create TCPClient and connect to server
client = New TcpClient
client.Connect(txtIP.Text, 5001)

'Get network stream associated with TCPClient
socketStream = client.GetStream

'create objects for writing and reading across stream
fileR = New BinaryReader(socketStream)</code>

There's the connection code. txtIP.text, as you may have guessed,
contains a string IP address (xx.xx.xx.xx). When I run this code, (yes
the server is running already) I guess this error when trying to
connect:

system.net.sockets.socketexception: No connection could be made
because the target machine actively refused it
It errors out at this line in the code:

<code>client.Connect(txtIP.Text, 5001)</code>

When I connect just through localhost, it connects perfectly, no
problems. Also, when I change IP (the string that contains the local ip
address) to:

<code>listener = New TcpListener(IPAddress.Parse("my.ip.goes.here"),
5001)</code>

I get some error:

SocketException: The requested address is not valid in it's
context
And the error points to:

<code>listener.start</code>

I am on a NAT but port 5001 is fowarded. I also tried with my firewall
disabled. Is this a code problem or a network problem?

I would probably know how to fix it, except I'm going on knowledge from
about 10 pages I read in my VB.NET book (that was the whole chapter)
and they only provided examples on connecting to localhost.

Please help! :)

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies

P: n/a
I have found this issue often related to domain resolve problems.

"anonymous_c" <mc****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com:
Hey guys. I'm creating a file transfer app. Anyways, I'm using this
code...

SERVER
<code>Dim Hostname As String = Dns.GetHostName
Dim IP As String =
Dns.GetHostByName(Hostname).AddressList(0).ToStrin g

Public Sub RunServer()
Dim listener As TcpListener

Try
listener = New TcpListener(IPAddress.Parse(IP), 5001)
'wait for connection request
listener.Start()

'extablish connection upon client request

lblStatus.Text = "Waiting for connection..."

'accept an incoming connection
connection = listener.AcceptSocket

'create networkstream object associated with socket
socketStream = New NetworkStream(connection)</code>...

CLIENT
<code>
Dim client As TcpClient

Try

'create TCPClient and connect to server
client = New TcpClient
client.Connect(txtIP.Text, 5001)

'Get network stream associated with TCPClient
socketStream = client.GetStream

'create objects for writing and reading across stream
fileR = New BinaryReader(socketStream)</code>

There's the connection code. txtIP.text, as you may have guessed,
contains a string IP address (xx.xx.xx.xx). When I run this code, (yes
the server is running already) I guess this error when trying to
connect:

system.net.sockets.socketexception: No connection could be made
because the target machine actively refused it

It errors out at this line in the code:

<code>client.Connect(txtIP.Text, 5001)</code>

When I connect just through localhost, it connects perfectly, no
problems. Also, when I change IP (the string that contains the local ip
address) to:

<code>listener = New TcpListener(IPAddress.Parse("my.ip.goes.here"),
5001)</code>

I get some error:

SocketException: The requested address is not valid in it's
context

And the error points to:

<code>listener.start</code>

I am on a NAT but port 5001 is fowarded. I also tried with my firewall
disabled. Is this a code problem or a network problem?

I would probably know how to fix it, except I'm going on knowledge from
about 10 pages I read in my VB.NET book (that was the whole chapter)
and they only provided examples on connecting to localhost.

Please help! :)


Nov 21 '05 #2

P: n/a
On 2005-07-25, anonymous_c <mc****@gmail.com> wrote:
Hey guys. I'm creating a file transfer app. Anyways, I'm using this
code...

SERVER
<code>Dim Hostname As String = Dns.GetHostName
Dim IP As String =
Dns.GetHostByName(Hostname).AddressList(0).ToStrin g

Public Sub RunServer()
Dim listener As TcpListener

Try
listener = New TcpListener(IPAddress.Parse(IP), 5001)
'wait for connection request
listener.Start()

'extablish connection upon client request

lblStatus.Text = "Waiting for connection..."

'accept an incoming connection
connection = listener.AcceptSocket

'create networkstream object associated with socket
socketStream = New NetworkStream(connection)</code>...

CLIENT
<code>
Dim client As TcpClient

Try

'create TCPClient and connect to server
client = New TcpClient
client.Connect(txtIP.Text, 5001)

'Get network stream associated with TCPClient
socketStream = client.GetStream

'create objects for writing and reading across stream
fileR = New BinaryReader(socketStream)</code>

There's the connection code. txtIP.text, as you may have guessed,
contains a string IP address (xx.xx.xx.xx). When I run this code, (yes
the server is running already) I guess this error when trying to
connect:

system.net.sockets.socketexception: No connection could be made
because the target machine actively refused it

It errors out at this line in the code:

<code>client.Connect(txtIP.Text, 5001)</code>

When I connect just through localhost, it connects perfectly, no
problems. Also, when I change IP (the string that contains the local ip
address) to:

<code>listener = New TcpListener(IPAddress.Parse("my.ip.goes.here"),
5001)</code>

I get some error:

SocketException: The requested address is not valid in it's
context

And the error points to:

<code>listener.start</code>

I am on a NAT but port 5001 is fowarded. I also tried with my firewall
disabled. Is this a code problem or a network problem?

I would probably know how to fix it, except I'm going on knowledge from
about 10 pages I read in my VB.NET book (that was the whole chapter)
and they only provided examples on connecting to localhost.

Please help! :)


Well... Are you on XPSP2? Is the firewall blocking your application?

--
Tom Shelton [MVP]
Nov 21 '05 #3

P: n/a
Yes, I'm on SP2, and no the firewall is disabled.

--
"I have found this issue often related to domain resolve problems. "
--

So what should I do?

Nov 21 '05 #4

P: n/a
try

listener = New TcpListener(IPAddress.Any, 5001)
"anonymous_c" wrote:
Hey guys. I'm creating a file transfer app. Anyways, I'm using this
code...

SERVER
<code>Dim Hostname As String = Dns.GetHostName
Dim IP As String =
Dns.GetHostByName(Hostname).AddressList(0).ToStrin g

Public Sub RunServer()
Dim listener As TcpListener

Try
listener = New TcpListener(IPAddress.Parse(IP), 5001)
'wait for connection request
listener.Start()

'extablish connection upon client request

lblStatus.Text = "Waiting for connection..."

'accept an incoming connection
connection = listener.AcceptSocket

'create networkstream object associated with socket
socketStream = New NetworkStream(connection)</code>...

CLIENT
<code>
Dim client As TcpClient

Try

'create TCPClient and connect to server
client = New TcpClient
client.Connect(txtIP.Text, 5001)

'Get network stream associated with TCPClient
socketStream = client.GetStream

'create objects for writing and reading across stream
fileR = New BinaryReader(socketStream)</code>

There's the connection code. txtIP.text, as you may have guessed,
contains a string IP address (xx.xx.xx.xx). When I run this code, (yes
the server is running already) I guess this error when trying to
connect:

system.net.sockets.socketexception: No connection could be made
because the target machine actively refused it

It errors out at this line in the code:

<code>client.Connect(txtIP.Text, 5001)</code>

When I connect just through localhost, it connects perfectly, no
problems. Also, when I change IP (the string that contains the local ip
address) to:

<code>listener = New TcpListener(IPAddress.Parse("my.ip.goes.here"),
5001)</code>

I get some error:

SocketException: The requested address is not valid in it's
context

And the error points to:

<code>listener.start</code>

I am on a NAT but port 5001 is fowarded. I also tried with my firewall
disabled. Is this a code problem or a network problem?

I would probably know how to fix it, except I'm going on knowledge from
about 10 pages I read in my VB.NET book (that was the whole chapter)
and they only provided examples on connecting to localhost.

Please help! :)

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.