开发者

.Net Socket not streaming from correct port?

C# .Net application which connects to five different data streams. The data streams are all in the same plaintext format, but have information from different areas. The application creates an array of custom objects, which contains sockets. Each socket connects to its IP/port (same IP, different ports) with no problems. However, when I read data off each socket, I'm getting the same data stream, from the first port number (in range, 10085-10089). For instance, I read data off the socket that's connected to 10088, but I get data from 10085.

The IP/port is grabbed from a database, so I deleted all but the record for port 10088, so the array created has only one object; there is only one socket connection, to port 10088. But I'm still getting only data from 10085.

I've viewed the data from each port through putty; the data is definitely different. Any idea why I'm getting the same data no matter what port a socket's connected to?

This is some simpler code that replicates the problem:

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

namespace MultipleFeedConnectionsTest
{
class Program
{
    static void Main(string[] args)
    {
        Socket[] sockets;
        IPAddress ipAddress;
        IPAddress currentIP;
        int portNumber = 10085;

        ipAddress = IPAddress.Parse("xxx.xxx.xxx.xxx"); // changed the IP
        currentIP = IPAddress.Parse("192.168.5.122");

        sockets = new Socket[5];

        for (int counter = 0; counter < 5; counter++)
        {
            if (counter != 1) 
            {
                sockets[counter] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sockets[counter].Bind(new IPEndPoint(currentIP, portNumber + counter));
                sockets[counter].Connect(new 开发者_如何学CIPEndPoint(ipAddress, portNumber + counter));
            }
        }

        while (true) 
        {
            for (int counter = 0; counter < 5; counter++)
            {
                if (counter != 1) 
                {
                    byte[] receivedData = new byte[255];
                    NetworkStream stream = new NetworkStream(sockets[counter]);
                    stream.Read(receivedData, 0, 255);

                    Console.WriteLine("FEED " + (portNumber + counter));
                    Console.WriteLine(Encoding.ASCII.GetString(receivedData));
                }
            }
        }
    }
}
    }

The above code outputs the same data from each socket. Again, when I access these through putty, they are different.


Tried your code and it works just fine.

You should however get rid of the call to sockets[counter].Bind(........) because it has nothing to do with your code - at all!!
Binding is usually done on the listening socket side, not the connecting one.

Anyway, besides that everything looks normal - I've also made sure I'm sending a unique message to each socket..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜