Question:
My C# server only accepts connections from localhost or another computer on the network?
vthokie4ever
2009-09-11 22:50:37 UTC
How do I make it accept connections from the open internet? Thanks.

Here's my code:

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

namespace SimpleConsoleServer
{
class Program
{
// Global methods
private static TcpListener tcpListener;
private static Thread listenThread;
private string endl = "\r\n";

///
/// Main server method
///

static void Main(string[] args)
{
tcpListener = new TcpListener(IPAddress.Any, 80);
listenThread = new Thread(new ThreadStart(ListenForClients));
listenThread.Start();
}

///
/// Listens for client connections
///

private static void ListenForClients()
{
tcpListener.Start();

while (true)
{
try
{
// Blocks until a client has connected to the server
TcpClient client = tcpListener.AcceptTcpClient();

// Create a thread to handle communication
// with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
catch (Exception)
{

}
}
}

///
/// Handles client connections
///

///
private static void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();

byte[] message = new byte[4096];
int bytesRead;

do
{
bytesRead = 0;

try
{
// Blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception)
{
// A socket error has occured
break;
}

if (bytesRead == 0)
{
// The client has disconnected from the server
break;
}

// Message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();

// Output message
Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
Console.WriteLine(encoder.GetString(message, 0, bytesRead));

} while (clientStream.DataAvailable);

// Release connections
clientStream.Close();
tcpClient.Close();
}
}
}
Three answers:
Ratchetr
2009-09-11 23:17:22 UTC
It's almost certainly not your code, this is usually a firewall issue.



But you are listening on 80 (not 4096 ;), which isn't usually blocked, so that's odd.



If you really can connect from localhost, and other machines on the same subnet, then that should rule out your local machine. But I would still disable your local firewall AND antivirus *TEMPORARILY* just to verify that.



If it's not your local machine, then what is the next firewall between your machine and the other end? Is it configured to forward HTTP to your machine? Are you trying to connect through the internet? Does your ISP block HTTP? Some do.



What are you using as a client to connect? Could the problem be with the client? You can always try connecting with the universal client: telnet yo.ur.ip 80. And of course there is always the ping test: can you ping your machine from the client that won't connect?
bonyai
2016-11-01 15:01:05 UTC
instead of web hosting your web page on localhost host it on your community handle. in all possibility some thing like 192.168.a million.33 to examine it open up the command instant (initiate>Run - "cmd" in XP or initiate>seek "cmd" enter in vista) and ask your self ipconfig
Nick
2009-09-11 22:56:07 UTC
byte[] message = new byte[4096];



4096 is the port



you need to portforward the connection. portforward.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...