问题描述
客户端应用程序代码
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Threading
Public Class Form1
Private m_sock As Socket
Private m_byBuff As Byte() = New Byte(36000) {}
Dim byteDateLine As Byte()
Dim data As IDataObject
Private Sub frmWebCam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
If m_sock IsNot Nothing AndAlso m_sock.Connected Then
m_sock.Shutdown(SocketShutdown.Both)
System.Threading.Thread.Sleep(10)
m_sock.Close()
End If
m_sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim epServer As New IPEndPoint(IPAddress.Parse(txip.Text), 5502)
m_sock.Blocking = False
Dim onconnect__1 As New AsyncCallback(AddressOf OnConnect)
m_sock.BeginConnect(epServer, onconnect__1, m_sock)
Catch ex As Exception
End Try
End Sub
Public Sub OnConnect(ByVal ar As IAsyncResult)
Dim sock As Socket = DirectCast(ar.AsyncState, Socket)
Try
If sock.Connected Then
SetupRecieveCallback(sock)
Else
MsgBox("Unable to connect to remote machine Connect Failed!")
End If
Catch ex As Exception
MsgBox(ex.Message & "Unusual error during Connect!")
End Try
End Sub
Public Sub OnRecievedData(ByVal ar As IAsyncResult)
Dim sock As Socket = DirectCast(ar.AsyncState, Socket)
Try
Dim nBytesRec As Integer = sock.EndReceive(ar)
If nBytesRec < 0 Then
sock.Shutdown(SocketShutdown.Both)
sock.Close()
ElseIf nBytesRec > 0 Then
Dim sRecieved As String = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec)
MsgBox(sRecieved)
End If
Catch ex As Exception
Finally
SetupRecieveCallback(sock)
End Try
End Sub
Public Sub SetupRecieveCallback(ByVal sock As Socket)
Try
Dim recieveData As New AsyncCallback(AddressOf OnRecievedData)
sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, sock)
Catch ex As Exception
' MessageBox.Show(Me, ex.Message, "Setup Recieve Callback failed!")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DateLine As [Byte]() = Encoding.ASCII.GetBytes(Environment.MachineName & " :- " & txport.Text.ToCharArray())
m_sock.Send(DateLine, DateLine.Length, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If m_sock IsNot Nothing AndAlso m_sock.Connected Then
m_sock.Shutdown(SocketShutdown.Both)
System.Threading.Thread.Sleep(10)
m_sock.Close()
End If
m_sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim epServer As New IPEndPoint(IPAddress.Parse(txip.Text), 5502)
m_sock.Blocking = False
Dim onconnect__1 As New AsyncCallback(AddressOf OnConnect)
m_sock.BeginConnect(epServer, onconnect__1, m_sock)
Catch ex As Exception
End Try
End Sub
End Class
服务器端服务代码(静态IP)
server side service code( At static IP)
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class Service1
Dim WithEvents atimer As New System.Timers.Timer(760)
Public Shared m_aryClients As New ArrayList()
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Protected Overrides Sub OnStart(ByVal args() As String)
End Sub
Private Sub TIMTICK(ByVal o As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles atimer.Elapsed
End Sub
Public Sub New()
InitializeComponent()
listener.Bind(New IPEndPoint(Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0), 5502))
listener.Listen(10)
listener.BeginAccept(New AsyncCallback(AddressOf OnConnectRequest), listener)
atimer.Start()
atimer.Enabled = True
End Sub
Protected Overrides Sub OnStop()
listener.Close()
End Sub
Public Sub OnConnectRequest(ByVal ar As IAsyncResult)
Try
Dim listener As Socket = DirectCast(ar.AsyncState, Socket)
NewConnection(listener.EndAccept(ar))
listener.BeginAccept(New AsyncCallback(AddressOf OnConnectRequest), listener)
Catch ex As Exception
End Try
End Sub
Public Sub NewConnection(ByVal sockClient As Socket)
Try
Dim client As New SocketChatClient(sockClient)
m_aryClients.Add(client)
client.SetupRecieveCallback(Me)
Catch ex As Exception
End Try
End Sub
Public Sub OnRecievedData(ByVal ar As IAsyncResult)
Dim client As SocketChatClient = ar.AsyncState
Dim aryRet = client.GetRecievedData(ar)
Try
If aryRet.Length < 1 Then
client.Sock.Close()
m_aryClients.Remove(client)
Return
Else
For Each clientSend As SocketChatClient In m_aryClients
Try
clientSend.Sock.Send(aryRet, aryRet.Length, 0)
Catch
clientSend.Sock.Close()
m_aryClients.Remove(clientSend)
End Try
Next
End If
Catch ex As Exception
Finally
client.SetupRecieveCallback(Me)
End Try
End Sub
Friend Class SocketChatClient
Private m_sock As Socket
Private m_byBuff As Byte() = New Byte(36000) {}
Public Sub New(ByVal sock As Socket)
m_sock = sock
End Sub
Public ReadOnly Property Sock() As Socket
Get
Return m_sock
End Get
End Property
Public Sub SetupRecieveCallback(ByVal app As Service1)
Try
Dim recieveData As New AsyncCallback(AddressOf app.OnRecievedData)
m_sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, Me)
Catch ex As Exception
Console.WriteLine("Recieve callback setup failed! {0}", ex.Message)
End Try
End Sub
Public Function GetRecievedData(ByVal ar As IAsyncResult) As Byte()
Dim nBytesRec As Integer = 0
Try
nBytesRec = m_sock.EndReceive(ar)
Catch
End Try
Dim byReturn As Byte() = New Byte(nBytesRec - 1) {}
Array.Copy(m_byBuff, byReturn, nBytesRec)
Return byReturn
End Function
End Class
End Class
但是在服务器端,我有两个LAN卡,一个用于LAN,另一个用于WAN(静态IP)或Internet,因此我如何通过静态ip直接在WAN卡上进行通信.
在服务器上有两个局域网卡
一个IP地址为192.168.0.2
和
第二个IP地址为292.268.154.54(Airtel提供静态IP)
现在我想通过具有客户端代码exe和不同网络连接的两台不同的PC进行通信,因此在黑白连接上存在问题
请帮助我
but on server side i have two LAN Card one for LAN and other For WAN(Static IP) or internet so how can i communicate directly on WAN Card by static ip.
ON Server there two lan card
one has IP Address is 192.168.0.2
and
second has ip address is 292.268.154.54(Airtel Provide Static IP)
now i want to communicate by two different pc which has client code exe and different Network connection , so there is problem on connection b/w them
Please Help me
推荐答案
Private Function GetLocalIpAddresses() As List(Of System.Net.IPAddress)
Dim strHostName As String
Dim addresses() As System.Net.IPAddress
Dim retAddresses As New List(Of System.Net.IPAddress)
strHostName = System.Net.Dns.GetHostName()
addresses = System.Net.Dns.GetHostAddresses(strHostName)
' Find an IpV4 address
For Each address As System.Net.IPAddress In addresses
If address.ToString.Contains(".") then
retAddresses.Add(address)
End If
Next
' No IpV4 address? Return the loopback address.
If retAddresses.Count = 0 Then retAddresses.Add(System.Net.IPAddress.Loopback)
Return retAddresses
End Function
获得列表后,您只需确定要使用的列表即可.
您将替换
After you have the list, you just decide which one you want to use.
You would replace
listener.Bind(New IPEndPoint(Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0), 5502))
像这样:
with something like:
Dim addresses As List(Of System.Net.IPAddress)
addresses = GetLocalIpAddresses()
' Lets say you know you want to use the first address...
listener.Bind(addresses.Item(0), 5502))
另外,您可以只使用我的客户端和服务器类通过tcp/ip进行通信. 此处..
希望这会有所帮助...如果这是您的答案,请单击绿色按钮.
-皮特
Also, you can just use my client and server classes to do communication over tcp/ip. Have a look at the project here.
Hope this helps... and if this is your answer, please hit the green button.
- Pete
这篇关于静态IP上的聊天服务器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!