Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I made small vb.net project that send some text message to another PC 💻 using TCP/IP protocol.
My project worked probably with Ethernet connection but failed with internet connection.

Can anyone help me?
Thankyou anyway for your time...😊
Posted
Comments
Sergey Alexandrovich Kryukov 24-May-15 11:12am    
You did not provide any information on your "failure".
—SA

In addition to the answer of Sergey, if you have already tested code working on your intranet it should work with no problems also on the internet, but same small details have to be checked.
First of all check that the windows firewall is not filtering your access to internet. If so insert your app in the list of exceptions and give it permit ti access internet.
Check the address of your partner on the other side: i.e. if you have an intranet address of, say, 192.168.1.10 consider that this is not the address with which you are visible on the internet... For the same reason your partner on the other side is not known to you with its intranet address.
To estabilish the communication there a re a lot of means, and it is not so easy to synthetize here, but the main 2 ways are:
1. Create a VPN on which resides your and the remote machine sharing same subnet. In this case the access is made simply using the intranet address.
2. To estabilish an internet connection on public net requires that at least one partner, the server, has a public address (internet public address) to be contacted by the partner. This is very important because the TCP/IP stack can understand that the address is outside the local subnet and start the message routing through the internet. On the server side you can set the router to direct such internet incoming connection from a specific address and port to a specific local machine (intranet address and port). The local server then sees any incoming connection routed on its local address so can handle them. The translation of messages, or better the routing, betwen the network and the machine is transparently handled by the router. When the client starts a connection it specify as the public address to which it want connect that of the remote router on the internet, then the router will dispatch messages to the local machine accordingly to its network translation table. These functions are normally available on any decent economic internet router.
This[^] white paper is a good font of info's.
 
Share this answer
 
v6
Comments
Michael Azzar 25-May-15 1:26am    
My vote is 5
Thank you very much Mr. Frankie-C for your clear answer.
I think that the solution included in it.
But unfortunately I can't try it now, because I am on a business trip for a week. But I'm going to try it and give you feedback
Frankie-C 25-May-15 3:25am    
You're welcome.
Thanks for the 5 and let me know if you solved the problem.
Michael Azzar 27-May-15 8:15am    
Hello Mr. Frankie
I hope you are fine
Wanted to tell you the outcome of the final test.
I've run my app on two PC connected to the internet.
But unfortunately l have encountered the same problem although I make an exception for the app in windows firewall
This is the. App code
You probably know the cause of the problem

Imports System.Net.Sockets
Imports System.Threading
Imports System.IO

Public Class Form1
Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = ""

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
GC.Collect()
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Timer1.Stop()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtmyIPAddress.Text = GetIPAddress()
Dim ListenerThread As New Thread(New ThreadStart(AddressOf Listening))
ListenerThread.Start()
Timer1.Start()
txtName.Focus()
End Sub
Private Sub Listening()
Listener.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()

Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
RichTextBox1.ForeColor = Color.Black
RichTextBox1.Text += Message + vbCrLf
'Here you can enter anything you would like
'to happen when a message is received,
'For instance; Play a sound, Show a message Box, A Balloon Tip etc.
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If txtName.Text = "" Or cmbAddress.Text = "" Then
'Check to make sure that the user has entered
'a display name, and a client IP Address
'If Not, Show a Message Box
MessageBox.Show("UserName and IPAddress must be Filled", _
"Error Sending Message", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Try
Client = New TcpClient(cmbAddress.Text, 65535)

'Declare the Client as an IP Address.
'Must be in the Correct form. eg. 000.0.0.0
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(txtName.Text & " Says: " & txtmessage.Text)
Writer.Flush()

'Write the Message in the stream
If Not txtmyIPAddress.Text.Trim = cmbAddress.Text.Trim Then
RichTextBox1.Text += (txtName.Text & " Says: " & txtmessage.Text) + vbCrLf
End If
txtmessage.Text = ""
Catch ex As Exception
Console.WriteLine(ex)
Dim Errorresult As String = ex.Message
MessageBox.Show(Errorresult & vbCrLf & vbCrLf & _
"Please Review Client Address", _
"Error Sending Message", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End If
End Sub
Private Function GetIPAddress() As String
Dim oAddr As System.Net.IPAddress
Dim sAddr As String
With System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
sAddr = oAddr.ToString
End With
GetIPAddress = sAddr
End Function
End
Frankie-C 27-May-15 9:10am    
Thanks, but I told you to make also settings on the router to move messages coming from public address to the local computer. Have you done it? This link will give you some hints http://www.wikihow.com/Set-Up-Port-Forwarding-on-a-Router
Remember that the address to use is the *public address* not the local address. Because it changes on each connection (unless your provider assigned you a static IP address) you can use one of the many dymanic IP DNS (see https://www.google.com/search?q=dynamic%20ip%20dns&rct=j).
Michael Azzar 27-May-15 9:55am    
Referring to router setting .I use cellular data( phone. ) to connect to the internet
Please see my comment to the question and my past answers:
Communication b/w two Windows applications on LAN.[^],
how i can send byte[] to other pc[^].

—SA
 
Share this answer
 
Comments
Michael Azzar 25-May-15 1:29am    
Thank you Mr.Sergey for your links
I think that it will help me more
Thank you 😊 very much...
Sergey Alexandrovich Kryukov 25-May-15 1:32am    
You are welcome. Will you accept the answer formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900