Ok well I've tested this a few different ways but haven't gotten anywhere. I don't think it's the router though because I turned off its firewall (and the computer's again) to test this. I also tested it on different computers both inside and outside the router with the same result. To me it seems like VB is purposely rejecting it. Is it possible for that to occur?
Show some of your code and maybe someone can help. At the moment you are not giving us much to go on. If you could show the part of your client code that attempts the connection and the part of your server code that accepts the connection then we might have an idea of how to direct you. As is for all we know your code is not accepting the connection.
I thought I should have posted this earlier. Alright here's the code.
First the client side.
Code:
Private Sub transferButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles transferButton.Click
Const BYTE_SIZE As Integer = 1
Dim stream As NetworkStream
Dim fileToSend As String = fileToSendTextBox.Text
Dim saveAs As String = saveAsTextBox.Text
Dim IPAddress As String = "24.129.26.34"
Dim filestream As System.IO.FileStream = Nothing
Dim buffer(BYTE_SIZE - 1) As Byte
Dim bytesRead As Integer = 0
Dim port As Int32 = 13000
Dim client As New TcpClient
client.Connect(IPAddress, port)
If fileToSend = "" Then
MsgBox("You must choose a file", MsgBoxStyle.Critical, "Error")
End If
stream = client.GetStream()
filestream = New System.IO.FileStream(fileToSend, _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Try
Do
bytesRead = filestream.Read(buffer, 0, BYTE_SIZE)
If (bytesRead > 0) Then
stream.Write(buffer, 0, BYTE_SIZE)
End If
Loop While bytesRead > 0
Catch ex As Exception
MsgBox("ERROR", MsgBoxStyle.Critical, "ERROR")
Me.Hide()
End Try
End Sub
And now the server
Code:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim stream As NetworkStream
Dim server As TcpListener
Dim port As Int32 = 80
Dim IP As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(IP, port)
server.Start()
While True
Dim i As Int32
Dim client As TcpClient = server.AcceptTcpClient()
stream = client.GetStream()
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
Write()
i = stream.Read(bytes, 0, bytes.Length)
End While
client.Close()
End While
SocketHandler = New TcpListener(hIPAddress, hPort) ' Set the IP AND Port Number
SocketHandler.Start()
I have a timer running which checks to see if there are any pending connections every 100ms.
Code:
If SocketHandler.Pending() Then
CurThreadStart = New ThreadStart(AddressOf ConnectionBegin) ' Set info for the new thread
CurThread = New Thread(CurThreadStart) ' Initialize the new thread variable
CurThread.SetApartmentState(ApartmentState.STA)
CurThread.Start() ' Start the thread
SyncLock CurThread ' Lock the thread for update
VAActiveThreads += 1 ' increment active threads
cInfo.ActiveClients += 1
CurThread.Name = "ClientCon" + VAActiveThreads.ToString ' Name the thread
Console.WriteLine(CurThread.Name & " Thread Started")
End SyncLock
End If
Then in my ConnectionBegin which is launched by the code above in a new thread I have.
Code:
Dim ClientSocket As Socket = SocketHandler.AcceptSocket
Followed by a loop that will read from the port until there is a idle timeout condition.
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
server = New TcpListener(IP, port)
server.Start()
End Sub
Private Sub FTTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FTTimer.Tick
If server.Pending() Then
threadStart = New ThreadStart(AddressOf ConnectionBegin)
thread = New Thread(threadStart)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End If
End Sub
Private Sub ConnectionBegin()
While True
Dim i As Int32
Dim client As TcpClient = server.AcceptTcpClient()
stream = client.GetStream()
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
Write()
i = stream.Read(bytes, 0, bytes.Length)
End While
client.Close()
End While
End Sub
Is this slightly better? Even with the addition of a pending check, the listener still can't seem to "hear" that remote connection.
Also looks like there is a problem with your IP address.
If your server is on IP 123.456.789.0 and is Listening on Port 24000 then your client would need to try to connect to IP 123.456.789.0 on Port 24000.
If your client trys to connect to IP 456.123.789.0 It will not connect because that is not where your server is at. If your client tries to connect on port 1234 if will not connect because that is not the port you are listening to.
I'm a little confused now. I fixed the port mistake, but I don't quite get what you mean when you say there's something wrong with my IP. I AM supposed to be listening on the localhost correct? The way I have it setup my server listens on the localhost and the client connects to it via the server's remote IP.
I'm a little confused now. I fixed the port mistake, but I don't quite get what you mean when you say there's something wrong with my IP. I AM supposed to be listening on the localhost correct? The way I have it setup my server listens on the localhost and the client connects to it via the server's remote IP.
The client would use the address of the server. If your server address is 127.0.0.1 as in your example above then your client would have to use 127.0.0.1
For example If you want to connect to Google you will not get there by typing in Yahoo.
Btw you can also use the machine name rather than a hard coded IP address. Works much better especially when the IP is DHCP controlled.
Code:
Dim hInfo As IPHostEntry = Nothing
Dim hIPAddress As IPAddress = Nothing
hInfo = Dns.GetHostEntry("Machine Name Here")
hIPAddress = hInfo.AddressList(0)
Last edited by DataMiser; July 27th, 2010 at 07:35 PM.
That's how I originally thought the connection was supposed to work. The problem (as I stated some posts back) is that when I have the server listen on any IP other than 127.0.0.1 a SocketException is thrown saying that "The requested address is not valid in its context".
You must use an address that is valid on the machine where the server is being executed. This is your Local Host. The client must use that same IP address and port as the parameters for the connection.
Here is a bit of code from my server. Note I do not hard code any IP address.
Code:
Dim hInfo As System.Net.IPHostEntry = Dns.GetHostEntry(Dns.GetHostName)
Dim hIPAddress As IPAddress = Nothing
hIPAddress = hInfo.AddressList(0)
SocketHandler = New TcpListener(hIPAddress, hPort) ' Set the IP AND Port Number
SocketHandler.Start()
Bookmarks