我该如何更改此代码以符合我的目的? [英] How should I change this code to suit my purpose?

查看:45
本文介绍了我该如何更改此代码以符合我的目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨人们再次



我有2段代码,一段用于发送消息:



Hi peoples once again

I have 2 sections of code, one for sending messages:

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Public Class Form1
       Private Const port As Integer = 54545
    Private Const broadcastAddress As String = "255.255.255.255"
    Private sendingClient As UdpClient
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeSender()
    End Sub
    Private Sub InitializeSender()
        sendingClient = New UdpClient(broadcastAddress, port)
        sendingClient.EnableBroadcast = True
    End Sub
   public toSend As String
    Dim data() As Byte
    Public Shared Sub go()
        Form1.data = Encoding.ASCII.GetBytes(Form1.toSend)
        Form1.sendingClient.Send(Form1.data, Form1.data.Length)
    End Sub
end class





和一个用于接收消息:





and one for receiving messages:

   Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
public class form1
 Delegate Sub AddMessage(ByRef message As String)
       Private Const port As Integer = 54545
    Private Const broadcastAddress As String = "255.255.255.255"
 Private receivingClient As UdpClient
    Private receivingThread As Thread
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeReceiver()
    End Sub
Private Sub InitializeReceiver()
        receivingClient = New UdpClient(port)
        Dim start As ThreadStart = New ThreadStart(AddressOf Receiver)
        receivingThread = New Thread(start)
        receivingThread.IsBackground = True
        receivingThread.Start()
    End Sub
 Private Sub Receiver()
        Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port)
        Dim messageDelegate As AddMessage = AddressOf MessageReceived
        While (True)
            Dim data() As Byte
            data = receivingClient.Receive(endPoint)
            Dim message As String = Encoding.ASCII.GetString(data)
            Invoke(messageDelegate, message)
        End While
    End Sub
    Private Sub MessageReceived(ByRef message As String)
        (show image in picturebox)
    End Sub
end class





问题在于:它向本地网络上的每台PC发送消息。如何更改此设置以便用户可以输入世界上任何人的IP地址并仅将消息发送给他们? (我认为端口必须是80)

我正在尝试制作视频聊天程序,在那里输入IP并将网络摄像头图像与代码一起发送到该IP地址



谢谢!



The problem is this: it sends messages to every PC on the local network. How can I change this so that the user can enter an IP Address of anyone in the world and send the message to them only? (I think that the port has to be 80)
I am trying to make a video chat program, where you enter the IP and the webcam image is sent with the code to that ip address

Thanks!

推荐答案

首先,再看看你的代码。您正在向广播地址发送数据,当然,这正是您所描述的内容。将广播地址替换为目标IP地址并且它将工作,前提是您和接收器之间没有阻止流量的防火墙。



第二个部分问题取决于接收机器的网络/路由器配置。您的客户端代码必须能够收听发件人,这意味着在可能的情况下打开防火墙中的端口,以便它可以听到客户端使用的端口上的内容。



这意味着,您必须依靠网络管理员设置firewal配置,或者您的代码必须能够使用UPnP在较小的网络上打开端口。
First, look at your code again. You''re sending data to the broadcast address, which, of course, does exactly what you''re describing. Replace the broadcast address with the target IP address and it''ll work, provided there isn''t a firewall between you and the receiver blocking the traffic.

The second part of your question depends on the network/router configuration of the receiving machine. Your client code has to be able to listen to the sender, so that means opening ports in firewalls, if possible, so it can hear something on the ports your client is using.

That means, either you have to rely on a network admin setting up firewal configurations or your code has to be able to open the ports itself on smaller networks using UPnP.


这篇关于我该如何更改此代码以符合我的目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆