iT邦幫忙

0

C# 做Server 和 Clinet 的問題

  • 分享至 

  • xImage

上網自學了一點C# 但不是很理解
我想用C#做一個Server 和 Clinet
用別人的範例可以做到同網段下通信, 但要是網段不同就連不上了, 網路上搜尋到使用UPnp來解決
參考網站
https://blog.csdn.net/yangyifan0/article/details/52295253
不曉得哪裡寫錯了, 想請問要怎麼修改能解決不同網段可以通信的問題呢

Server端程式碼

IPAddress ipv4;
String eip;
int iport = 8733;
int eport = 8733;
static Socket socketListener;

public Form1()
{
    InitializeComponent();

    String name = Dns.GetHostName();
    ipv4 = Dns.GetHostEntry(name).AddressList.Where(i => i.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();

    WebClient webClient = new WebClient();
    HttpClient client = new HttpClient();
    String rawRes = webClient.DownloadString("http://checkip.dyndns.org/");
    String regex = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
    eip = Regex.Match(rawRes, regex).Value;
    
    OpenUPnp();
    ServerEnd(eport, 10);
    Thread th = new Thread(ServerCommunity);
    th.Start(socketListener);
}

 private Boolean OpenUPnp()
 {
     UPnPNAT uPnPNAT = new UPnPNAT();

     IStaticPortMappingCollection mapping = uPnPNAT.StaticPortMappingCollection;
     if (mapping == null)
     {
         return false;
     }
     mapping.Add(eport, "TCP", iport, ipv4.ToString(), true, "測試");
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket.Bind(new IPEndPoint(ipv4, iport));
     IList<ServicePoint> servicePoints = new List<ServicePoint>();
     return true;
 }
 
 private static void ServerCommunity(object obListener)
{
    Socket temp = (Socket)obListener;
    while (true)
    {
        Socket socketSender = temp.Accept();
        logd("ServerCommunity()", ("Client IP = " + socketSender.RemoteEndPoint.ToString()) + " Connect Succese!");
        Thread ReceiveMsg = new Thread(ReceiveClient);
        ReceiveMsg.IsBackground = true;
        Thread SendToClient = new Thread(SendMsgToClient);
        SendToClient.Start(socketSender);
        ReceiveMsg.Start(socketSender);
    }
}

Client端程式碼

IPAddress ipv4;
String eip;
int iport = 8733;
int eport = 8733;
static Socket socketListener;

public Form1()
{
    InitializeComponent();

    String name = Dns.GetHostName();
    ipv4 = Dns.GetHostEntry(name).AddressList.Where(i => i.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();

    WebClient webClient = new WebClient();
    HttpClient client = new HttpClient();
    String rawRes = webClient.DownloadString("http://checkip.dyndns.org/");
    String regex = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
    eip = Regex.Match(rawRes, regex).Value;
    
    ClientSocket(ipv4.ToString(), iport);
}

private static void ClientSocket(string ip, int port)
{
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress myIp = IPAddress.Parse(ip);
    IPEndPoint point = new IPEndPoint(myIp, port);
    socket.Connect(point);

    logd("ClientSocket(" + ip + ", " + port+")", "Connect Succese! " + socket.RemoteEndPoint.ToString());

    Thread sendMsg = new Thread(SendMsgToServer);
    Thread ReceiveMsg = new Thread(ReceiveMsgFromServer);
    ReceiveMsg.IsBackground = true;
    ReceiveMsg.Start();
    sendMsg.Start();
}

感激不盡

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2024-03-03 16:57:49 檢舉
1. UPnP要路由器有開才行。
2. 這也不是兩個網段互通,例如兩個內網網段 192.168.0.0/24 和 192.168.1.0/24 沒在路由器設定路由也不會通。
3. UPnP 就是自動幫你作路由器的 Port Forwarding 而已
我這邊使用台哥大凱擘網路,接asus路由器,兩個都有去檢查有開UPnP
我想請問有沒有怎麼樣的寫法,可以達到兩台電腦在不同位置(不同網段)可通信的方法呢?
感謝
GJ iT邦好手 1 級 ‧ 2024-03-05 16:32:03 檢舉
在tracert 指令下路由有通嗎?
我對網路概念不是很熟悉, 我這邊測試環境是一台電腦接手機基地台, ip 172.20.10.15, 另一台電腦下指令tracert 172.20.10.15
結果如下
在上限 30 個躍點上追蹤 172.20.10.15 的路由
1 <1 ms <1 ms <1 ms router.asus.com [192.168.50.1]
2 11 ms 10 ms 8 ms 10.47.0.1
3 10 ms 9 ms 8 ms 10.4.26.81
4 10 ms 11 ms 9 ms 10.4.26.41
5 9 ms 9 ms 10 ms 10.4.90.131
6 10 ms 9 ms 8 ms 10.4.90.137
7 20 ms 54 ms 26 ms 61-31-51-193.static.tfn.net.tw [61.31.51.193]
8 9 ms 11 ms 11 ms 60-199-47-105.static.tfn.net.tw [60.199.47.105]
9 9 ms 9 ms 13 ms 60-199-17-42.static.tfn.net.tw [60.199.17.42]
10 * * * 要求等候逾時。
v60i iT邦新手 3 級 ‧ 2024-03-09 20:52:29 檢舉
你的Windows 防火牆有開8733 允入允出嗎?這是特殊port 要手動開啟
有開的, 透過這個方式開啟
https://wiki.mcneel.com/zh-tw/zoo/window7firewall
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答