本地网络上的套接字通信 [英] Socket communication on local network

查看:72
本文介绍了本地网络上的套接字通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个家庭本地网络,有三台电脑A,B和C,还有一台路由器(一个ISP盒子)。计算机的特征如下:



-      
A:



  • Windows 10家庭版,连接Wi-Fi,IP地址:192.168.1.10


-      
B:



  • Windows 10家庭版,连接以太网,IP地址:192.168.1.14


-      
C:



  • Windows 10专业版,连接Wi-Fi,IP地址:192.168.1.15




所有计算机在文件资源管理器中相互可见(路由器,地址192.168.1.1,也可见)。我可以在远程桌面中从A或B连接到C.因此
本地网络组件之间的连接完全可以运行。





现在,我有两个应用程序Client.exe和Server.exe,它们通过套接字在同一台计算机上进行通信,具有以下实现特性:



Client.exe:



-      
C#浏览器外的Silverlight应用程序



-      
插座端口:4530



-      
服务器IP地址:"192.168.0.1"



-      
实现System.Net.Sockets.Socket



Server.exe:



-      
C#应用



-      
监听端口:4530



-      
服务器IP地址:"192.168.0.1"



-      
侦听IP地址:System.Net.IPAddress.Any



-      
implements:System.Net.Sockets.TcpListener



由于这在同一台计算机上运行良好的双向请求 - 响应,我想在上述家庭网络的两台计算机上运行它们。



因此,我在A上安装了客户端,在C上安装了服务器,在代码中相应地将服务器IP地址更改为C的(192.168.1.15)。我还在路由器上打开了端口
4530对于C计算机。



我在C上启动了Server.exe并从控制台执行'netstat -an':端口4530被标记为'正在'。但是当我从C启动Client.exe时它无法建立
连接。



要做什么,以便在两台本地联网的计算机上运行我的套接字客户端 - 服务器应用程序?



谢谢

解决方案

嗨Opariti,


根据根据你的描述,在我看来没有问题,我想也许客户端和服务器之间可能存在一些网络问题。我已经制作了一个简单的控制台应用程序,希望它对你有用。

服务器(10.157.13.69)。

 static void Main(string [] args)
{
int port = 80;
string host =" 10.157.13.69";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip ,port);
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(0);
Console.WriteLine("等待客户端");
套接字temp = s.Accept();
Console.WriteLine(" connected");
string recvStr ="" ;;
byte [] recvBytes = new byte [1024];
int bytes;
bytes = temp.Receive(recvBytes,recvBytes.Length,0);
recvStr + = Encoding.ASCII.GetString(recvBytes,0,bytes);

Console.WriteLine(" server get message:{0}",recvStr);
string sendStr =" ok!客户端发送消息成功!";
byte [] bs = Encoding.ASCII.GetBytes(sendStr);
temp.Send(bs,bs.Length,0);
temp.Close();
s.Close();
Console.ReadLine();
}

客户(10.157.13.70)

 static void Main(string [] args)
{
try
{
int port = 2000;
string host =" 10.157.13.69" ;;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip,port);
Socket c = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); Console.WriteLine(QUOT; Conneting ...");
c.Connect(ipe);
string sendStr =" hello!这是套接字测试";
byte [] bs = Encoding.ASCII.GetBytes(sendStr); Console.WriteLine("发送消息");
c.Send(bs,bs.Length,0);
string recvStr ="" ;;
byte [] recvBytes = new byte [1024];
int bytes;
bytes = c.Receive(recvBytes,recvBytes.Length,0); recvStr + = Encoding.ASCII.GetString(recvBytes,0,bytes);
Console.WriteLine(" client get message:{0}",recvStr); c.Close();
}
catch(ArgumentNullException e)
{
Console.WriteLine(" argumentNullException:{0}",e);
}
catch(SocketException e)
{
Console.WriteLine(" SocketException:{0}",e);
}
Console.WriteLine("按Enter键退出");
}

结果。



最好的问候

Abraham


            

Hi,

I have a home local network, with three computers A, B, and C, plus a router (an ISP box). The characteristics of the computers are as following:

-      A:

  • Windows 10 Family Edition, connection Wi-Fi, IP address: 192.168.1.10

-      B:

  • Windows 10 Family Edition, connection Ethernet, IP address: 192.168.1.14

-      C:

  • Windows 10 Professional Edition, connection Wi-Fi, , IP address: 192.168.1.15

All computers are visible each other in File Explorer (router, address 192.168.1.1, is also visible). I can connect in Remote Desktop from A or B into C. Therefore the connection between the local network components is fully operational.

Now, I have two applications, Client.exe and Server.exe, that communicate in the same computer via sockets, with the following implementation characteristics:

Client.exe:

-      C# out-of-browser Silverlight application

-      socket port: 4530

-      server IP address: "192.168.0.1"

-      implements System.Net.Sockets.Socket

Server.exe:

-      C# application

-      listener port: 4530

-      server IP address: "192.168.0.1"

-      listen on IP address: System.Net.IPAddress.Any

-      implements: System.Net.Sockets.TcpListener

As this works fine two-ways request-response in the same computer, I want to run them on two computers of the above home network.

Therefore, I installed Client on A and the Server on C, changing accordingly in the code the server IP address to the C’s one (192.168.1.15). I also opened the port 4530 on router for the C computer.

I started Server.exe on C and did ‘netstat –an’ from console: the port 4530 was marked as ‘listening’. But when I started Client.exe from C it couldn’t establish the connection.

What is to be done so that to make running my socket Client-Server application on the two locally networked computers?

Thanks

解决方案

Hi Opariti,

According to your description, there is no problem it seems to me, I think maybe some network problems between the client and the server. I have made a simple console application, wish it is useful to you.
Server(10.157.13.69).

static void Main(string[] args)
        {
            int port = 80;
            string host = "10.157.13.69";
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(0);
            Console.WriteLine("wait for the client");
            Socket temp = s.Accept();
            Console.WriteLine("connected");
            string recvStr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
            recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);

            Console.WriteLine("server get message:{0}", recvStr);
            string sendStr = "ok!Client send message successful!";
            byte[] bs = Encoding.ASCII.GetBytes(sendStr);
            temp.Send(bs, bs.Length, 0);
            temp.Close();
            s.Close();
            Console.ReadLine();
        }

Client(10.157.13.70)

       static void Main(string[] args)
        {
            try
            {
                int port = 2000;
                string host = "10.157.13.69";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("Conneting…");
                c.Connect(ipe);
                string sendStr = "hello!This is a socket test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr); Console.WriteLine("Send Message");
                c.Send(bs, bs.Length, 0);
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0); recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine("client get message:{0}", recvStr); c.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("argumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}", e);
            }
            Console.WriteLine("Press Enter to Exit");
        }

Result.

Best Regards
Abraham

            


这篇关于本地网络上的套接字通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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