C#套接字:客户端未连接到分区计算机中的服务器

时间:2016-07-20 17:03:55

标签: c# .net sockets

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

服务器代码:

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();
    }

    }
}
}

我希望问题很明确,你会回答的, 伊泰

1 个答案:

答案 0 :(得分:0)

嗯,我发现解决方案非常快。 我只是将端口im端口转发到服务器ip。 例如,如果服务器IP为10.0.0.1且端口为2222 我只需要使用“lan users”10.0.0.1转发端口2222。 无论如何,谢谢你的帮助。