joshmerd
September 26th, 2006, 11:01 PM
I hope you can help me with this. I am writing a TCP server which will listen for connections on a certain port then write a text tile to that client. When I run it, it displays my IP, but then immediately closes before going further. Why is this?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace TCPserver
{
class Program
{
public static ArrayList alSockets;
private static Object thisLock1 = new Object();
private static Object thisLock2 = new Object();
static void Main(string[] args)
{
string hostname = Dns.GetHostName();
IPHostEntry IPHost = Dns.GetHostEntry(hostname);
Console.WriteLine("My IP address is " + IPHost.AddressList[0].ToString());
ArrayList alSockets = new ArrayList();
}
public static void listenerThread()
{
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, 8080);
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
Console.WriteLine(handlerSocket.RemoteEndPoint.ToString() + " connected.");
lock (thisLock1)
{
alSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
public static void handlerThread()
{
Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (thisLock2)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite("hello.txt");
while (true)
{
thisRead = networkStream.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0) break;
}
fileStream.Close();
}
Console.WriteLine("File written.");
handlerSocket = null;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace TCPserver
{
class Program
{
public static ArrayList alSockets;
private static Object thisLock1 = new Object();
private static Object thisLock2 = new Object();
static void Main(string[] args)
{
string hostname = Dns.GetHostName();
IPHostEntry IPHost = Dns.GetHostEntry(hostname);
Console.WriteLine("My IP address is " + IPHost.AddressList[0].ToString());
ArrayList alSockets = new ArrayList();
}
public static void listenerThread()
{
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, 8080);
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
Console.WriteLine(handlerSocket.RemoteEndPoint.ToString() + " connected.");
lock (thisLock1)
{
alSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
public static void handlerThread()
{
Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (thisLock2)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite("hello.txt");
while (true)
{
thisRead = networkStream.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0) break;
}
fileStream.Close();
}
Console.WriteLine("File written.");
handlerSocket = null;
}
}
}