【问题标题】:C# Sockets: client is not connecting to server in seprate computersC# 套接字:客户端未连接到单独计算机中的服务器
【发布时间】:2016-07-21 01:16:40
【问题描述】:

我是 C# 套接字编程的新手,我正在处理一个为某些客户端发送字符串的服务器的小项目。我通过修改MSDN's Synchronous Serverclient Socket 示例来实现。 当我在同一台计算机上运行服务器和客户端时,它们工作正常,但是当我在一台计算机上运行服务器并在另一台计算机上运行客户端时,客户端上显示套接字异常(两台计算机在同一个网络中)。 现在我不确定该怎么做:端口转发?改代码?

服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace textweb
{
    class Program
    {
    static int counter = 0;
    static Socket[] _socket = new Socket[2];
    static void Main(string[] args)
    {
        byte[] bytes = new Byte[1024];
        //running the server on the local host
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
        Console.WriteLine(ipHostInfo.HostName);
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        Console.WriteLine(ipAddress);
        TcpListener listener = new TcpListener(ipAddress, /*MyPort*/);

        try
        {

            listener.Start(10);
            Console.WriteLine("Waiting for a connection...");

            while (counter < 2)
            {
                while (!listener.Pending()) { }
                while (listener.Pending())
                {
                    _socket[counter] = listener.AcceptSocket();
                    counter++;
                }
            }
            bool _continue = true;
            while (_continue)
            {
                string m = Console.ReadLine();
                byte[] msg = Encoding.ASCII.GetBytes(m);

                foreach (Socket soc in _socket)
                {
                    soc.Send(msg);
                    if (m == "exit")
                    {
                        soc.Shutdown(SocketShutdown.Both);
                        soc.Close();
                        _continue = false;
                    }
                }
            }




        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress any key to continue...");
        Console.ReadKey();
    }
}
}

客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace textclient
{
  class Program
   {
    static void Main(string[] args)
    {
         byte[] bytes = new byte[1024];


    try {

        IPHostEntry ipHostInfo = Dns.GetHostByName(/*server's ip*/); 
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint remoteEP = new IPEndPoint(ipAddress,/*MyPort*/);
        Socket sender = new Socket(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp );

        try {

            sender.Connect(remoteEP);
            Console.WriteLine("Socket connected to {0}",sender.RemoteEndPoint.ToString());
            bool _continue = true;
            while (_continue)
            {
                if (sender.Available>0)
                {
                    int bytesRec = sender.Receive(bytes);
                    string a = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    Console.WriteLine(a);
                    if (a == "exit")
                    {
                        sender.Shutdown(SocketShutdown.Both);
                        sender.Close();
                        _continue = false;
                    }
                }
            }

            Console.ReadKey();


        } catch (ArgumentNullException ane) {
            Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); 
            Console.ReadKey();
        } catch (SocketException se) {
            Console.WriteLine("SocketException : {0}", se.ToString()); 
            Console.ReadKey();
        } catch (Exception e) {
            Console.WriteLine("Unexpected exception : {0}", e.ToString());
            Console.ReadKey();
        }

    } catch (Exception e) {
        Console.WriteLine( e.ToString());
        Console.ReadKey();
    }

    }
}
}

我希望问题很清楚,你会回答它, 伊泰

【问题讨论】:

    标签: c# .net sockets


    【解决方案1】:

    嗯,我很快就找到了解决方案。 我只是将我使用的端口转发到服务器 ip。 例如,如果服务器 ip 是 10.0.0.1 并且端口是 2222 我只需要将端口 2222 与“局域网用户”10.0.0.1 一起转发。 无论如何,谢谢你的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多