Ident

  • Thread starter Thread starter Roberto Oakenfold
  • Start date Start date
R

Roberto Oakenfold

I'm trying to to write an ident server, which yesterday worked fine, but
today its not. When I run it and connect to port 113 and try and send data
as soon as I press a button it dumps the connection, and wont let me send
the whole request, ie (1 , 2). Sometimes it'll send back
System.Byte[] : USERID : UNIX : NICKNAME
No matter what I do cant cant solve it.

[csharp]
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace IRC.Identd
{
public class Identd
{
private static string username;
private const int IdentdPort = 113;

public static void Start(string UserName)
{
username = UserName;
Thread SocketThread = new Thread(new ThreadStart(Identd.Run));
SocketThread.Start();
}

private static void Run()
{
try
{
string userid = " : USER : UNIX : " + username;

IPEndPoint listenEndPoint = new IPEndPoint(IPAddress.Any,
IdentdPort);
Socket tcpServer = new Socket(listenEndPoint.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

tcpServer.Bind(listenEndPoint);
tcpServer.Listen(int.MaxValue);

// Enter the listening loop.
while (true)
{
// Perform a blocking call to accept requests.
Socket tcpClient = tcpServer.Accept();

while (true)
{
byte[] buffer = new byte[256];
tcpClient.Receive(buffer, 0, tcpClient.Available,
SocketFlags.None);

byte[] SendBack = Encoding.ASCII.GetBytes(buffer +
userid);
tcpClient.Send(SendBack);

tcpClient.Close();
}
}
}
catch { }
}
}
}
[/csharp]
 
Roberto Oakenfold said:
I'm trying to to write an ident server, which yesterday worked fine, but
today its not. When I run it and connect to port 113 and try and send data
as soon as I press a button it dumps the connection, and wont let me send
the whole request, ie (1 , 2). Sometimes it'll send back
System.Byte[] : USERID : UNIX : NICKNAME
No matter what I do cant cant solve it.

Well, it would help if you didn't just ignore any exceptions which are
thrown - when you catch an exception, log it. That's likely to help to
solve the connection drops.

Now, the data it returns is broken because you're trying to add a
string to a byte array. You should decode the data you've read into a
string, then add the two strings together before re-encoding them. (In
fact at the moment you could just send the received buffer back and
then the rest, but I'm assuming that's not what you want eventually.)

Note that you should be looking at the return value of Receive *and*
you should be waiting until you've seen a complete request (i.e. parsed
to the end of it) before returning the response - you're probably
reading just the first part of the request because that much was sent
by the client program, and then sending the response and closing the
socket.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top