Reputation: 960
I have a socket listener application implemented in Java, and works fine
C# Code:
IPAddress serverAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(serverAddress, 1234);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] data = new byte[client.ReceiveBufferSize];
int bytesRead = stream.Read(data, 0, Convert.ToInt32(client.ReceiveBufferSize));
string request = Encoding.ASCII.GetString(data, 0, bytesRead);
Console.WriteLine(request);
}
White it works fine in Java:
ServerSocket server = new ServerSocket(1234);
Socket socket = server.accept();
In java a new client connection started, and I managed to read data. While in C# it didn't create any TcpClient at all.
Please Help!
Upvotes: 1
Views: 1302
Reputation: 855
Try replacing your TcpLister initialization with this:
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
When you use "127.0.0.1" you are binding the server socket to the loopback address. Only clients on the same machine are able to communicate with servers that use the loopback IP.
Upvotes: 1