473,396 Members | 2,057 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,396 software developers and data experts.

Keep getting this dang socket exception...

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
4 4559
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Clarence Gardner | last post by:
I've got a problem that I don't see how to program around. A socket server that was running fine, today started getting an exception from the bind() call (errno 22, Invalid argument) and yet the...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
11
by: seb | last post by:
Hello, **************** What I need : **************** I need to write a scanner that test all the IP adresses that repond on a given port. The Ip list is of roughly of length 200. I need...
0
by: mmcgee00 | last post by:
Hi, Currently, I am trying to get different service banner by connecting to different ports using python (code below). The versions I am working with are python 4.2.1 and fedora core 4. I am...
2
by: cmrhema | last post by:
Hi, We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice. The whole code is produced below....
2
by: Scott | last post by:
I'm debugging an xmlrpc client/server application. Often when an exception occurs in the server, I receive only a very short error message on the client. For example: xmlrpclib.Fault: <Fault 1:...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.