Click to See Complete Forum and Search --> : TCP/IP Server


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;
}


}
}

MadHatter
September 27th, 2006, 12:27 AM
uhm... you have to do something in order to get something.

your code does... nothing. you have 2 methods that do something, but you never invoke them. you never start the server. you never spawn any threads. what exactly are you expecting?

try something like this:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TestBed {
class Program {
static void Main(string[] args) {
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint listenPoint = new IPEndPoint(IPAddress.Any, 8080);
server.Bind(listenPoint);
server.Listen(int.MaxValue);
while(true) {
Socket client = server.Accept();
ThreadPool.QueueUserWorkItem(ProcessClient, client);
}
}
static void ProcessClient(object state) {
using(Socket client = state as Socket) {
if(client == null) return;
using(Stream file = File.Create("hello.txt")) {
byte[] buffer = new byte[8192];
int count = 0;
do {
count = client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
file.Write(buffer, 0, count);
} while(count == buffer.Length);
}
}
}
}
}

joshmerd
September 27th, 2006, 02:41 PM
Would it work if I add something like:


ThreadStart listenHandler = new ThreadStart(listenerThread);
Thread liHandler = new Thread(listenHandler);
liHandler.Start();


to main?