I got bored and felt like making something so I tried to make a basic chat server which worked out pretty well after fixing 2 or 3 problems 
You can basicly chat with everyone in the server, the server can also say stuff to everyone if you type "say [stuff]"
Just posting this incase anyone is interested in the source code
Server source:
Client chat form
Client login form
Full source code:
BasicServer.rar (Size: 84.75 KB / Downloads: 250)
Please give some advice in case you know a better/faster way of doing this
pic:
![[Image: mzembi45dN.png]](http://i.nowfollow.nl/mzembi45dN.png)
edit: yes I know the maximum amount of clients is bullshit and doesn't do anything but I got bored so I didn't finish that

You can basicly chat with everyone in the server, the server can also say stuff to everyone if you type "say [stuff]"
Just posting this incase anyone is interested in the source code
Server source:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace BasicServer
{
class Program
{
static TcpListener Listener;
static List<Client> Clients = new List<Client>();
static int Port = -1, MaxClients = -1, Connected = 0;
static void Main( string[] args )
{
Console.ForegroundColor = ConsoleColor.White;
for( int i = 0 ; i < args.Length ; i++ )
{
if( args[i].ToLower() == "+port" )
{
Port = Convert.ToInt32( args[i + 1] );
i++;
continue;
}
if( args[i].ToLower() == "+maxclients" )
{
MaxClients = Convert.ToInt32( args[i + 1] );
i++;
continue;
}
}
while( MaxClients < 1 )
{
Console.Write( "Enter the maximum amount of clients(1 or more): " );
MaxClients = Convert.ToInt32( Console.ReadLine() );
}
if( Port == -1 )
{
Console.Write( "Please enter the port that you want to use: " );
Port = Convert.ToInt32( Console.ReadLine() );
}
Console.Write( "Trying to start the server at port " + Port + ": " );
try
{
Listener = new TcpListener( System.Net.IPAddress.Any, Port );
Listener.Start();
new Thread( () =>
{
while( true )
{
TcpClient Client = Listener.AcceptTcpClient();
Thread ClientThread = new Thread( new ParameterizedThreadStart( ClientMain ) );
ClientThread.Start( Client );
Connected++;
}
} ).Start();
new Thread( new ThreadStart( ReadConsole ) ).Start();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine( "Success!" );
Console.ForegroundColor = ConsoleColor.White;
}
catch( Exception e )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( "Failed!" );
Console.WriteLine( "Error: " + e.Message );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "Press any key to exit..." );
Console.ReadKey();
Environment.Exit( 0 );
}
System.Diagnostics.Process.GetCurrentProcess().WaitForExit();
}
static void SendToClient( NetworkStream Client, string Text )
{
byte[] buffer = Encoding.ASCII.GetBytes( Text );
Client.Write( buffer, 0, buffer.Length );
Client.Flush();
}
static void ReadConsole()
{
string Text;
while( true )
{
Text = Console.ReadLine();
if( Text == "quit" )
{
foreach( Client c in Clients )
{
c.Stream.Dispose();
c.Stream.Close();
}
Environment.Exit( 0 );
}
if( Text.Split( ' ' )[0].ToLower() == "say" )
{
if( Connected <= 0 )
{
Console.WriteLine( "No clients connected ... " );
continue;
}
try
{
foreach( Client c in Clients )
SendToClient(
c.Stream,
"Server: " + Text.Substring( 4 )
);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write( "Send: \t\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine( Text.Substring( 4 ) );
}
catch( Exception e ) { Console.WriteLine( e.Message ); }
}
}
}
static void ClientMain( object obj )
{
TcpClient NewClient = ( TcpClient )obj;
NetworkStream ClientStream = NewClient.GetStream();
int ID = Connected;
byte[] bMessage = new byte[1024];
string Message;
int Recieved = ClientStream.Read( bMessage, 0, bMessage.Length );
Array.Resize( ref bMessage, Recieved );
if( !Encoding.ASCII.GetString( bMessage ).StartsWith( "#info" ) )
{
Console.WriteLine( "Someone tried to connect but it failed!" );
ClientStream.Dispose();
ClientStream.Close();
return;
}
string Name = Encoding.ASCII.GetString( bMessage );
Client Client = new Client( Name.Substring( 6 ), ID, ClientStream );
Clients.Add( Client );
Console.WriteLine( "New connection: " + Client.Name );
while( true )
{
Recieved = 0;
bMessage = new byte[1024];
try
{
Recieved = ClientStream.Read( bMessage, 0, bMessage.Length );
Array.Resize( ref bMessage, Recieved );
Message = Encoding.ASCII.GetString( bMessage );
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write( "Recieved: \t" );
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine( Message );
byte[] NewMes = Encoding.ASCII.GetBytes( Client.Name + ": " + Message );
foreach( Client c in Clients )
c.Stream.Write( NewMes, 0, NewMes.Length );
}
catch
{
Console.WriteLine( "Lost connection with " + Client.Name );
break;
}
}
ClientStream.Dispose();
ClientStream.Close();
Clients.Remove( Client );
Connected--;
}
public class Client
{
public NetworkStream Stream;
public int ID;
public string Name;
public Client( string name, int id, NetworkStream stream )
{
Name = name;
ID = id;
Stream = stream;
}
}
}
}Client chat form
Code:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace Client
{
public partial class ChatForm : Form
{
NetworkStream Stream;
public ChatForm( NetworkStream Stream )
{
InitializeComponent();
this.Stream = Stream;
new System.Threading.Thread( () =>
{
byte[] buffer;
int Recieved;
while( true )
{
try
{
buffer = new byte[1024];
Recieved = this.Stream.Read( buffer, 0, buffer.Length );
Array.Resize( ref buffer, Recieved );
Invoke( ( MethodInvoker ) delegate
{
MessageBox.Text += Encoding.ASCII.GetString( buffer ) + "\r\n";
} );
}
catch( Exception e )
{
System.Windows.Forms.MessageBox.Show( "Disconnected! \r\n" + e.Message );
Environment.Exit( 0 );
}
}
} ).Start();
}
private void button1_Click( object sender, EventArgs e )
{
byte[] buffer = Encoding.ASCII.GetBytes( TextBox.Text );
this.Stream.Write( buffer, 0, buffer.Length );
this.Stream.Flush();
TextBox.Text = "";
}
private void TextBox_KeyPress( object sender, KeyPressEventArgs e )
{
if( e.KeyChar == ( char )Keys.Enter )
button1_Click( new object(), new EventArgs() );
}
}
}Client login form
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Client
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e )
{
try
{
TcpClient Client = new TcpClient();
IPEndPoint IP = new IPEndPoint(
IPAddress.Parse( IPBox.Text ),
Convert.ToInt32( PortBox.Text )
);
if( !Login( NameBox.Text, PwBox.Text ) )
{
MessageBox.Show( "Invalid password or username!\r\n" +
"Are you sure this account exists?" );
return;
}
Client.Connect( IP );
NetworkStream stream = Client.GetStream();
byte[] buffer = Encoding.ASCII.GetBytes( "#info " + GetRealName( NameBox.Text ) );
stream.Write( buffer, 0, buffer.Length );
stream.Flush();
ChatForm Chat = new ChatForm( stream );
Chat.Show();
this.Hide();
}
catch( Exception ex )
{
MessageBox.Show( ex.ToString() );
}
}
string GetRealName( string name )
{
WebClient wb = new WebClient();
string source = wb.DownloadString( "http://www.itsmods.com/forum/User-" + name + ".html" );
int title = source.IndexOf( "<title>" );
wb.Dispose();
return source.Substring( title + 30, name.Length ).Trim();
}
private bool Login( string User, string Password )
{
System.Collections.Specialized.NameValueCollection Data
= new System.Collections.Specialized.NameValueCollection();
Data.Add( "username", User );
Data.Add( "password", Password );
Data.Add( "submit", "Login" );
Data.Add( "action", "do_login" );
WebClient wb = new WebClient();
byte[] bytes = wb.UploadValues( "http://itsmods.com/forum/member.php", "POST", Data );
if( System.Text.Encoding.ASCII.GetString( bytes ).Contains( "successfully" ) )
return true;
return false;
}
}
}Full source code:
BasicServer.rar (Size: 84.75 KB / Downloads: 250)
Please give some advice in case you know a better/faster way of doing this

pic:
![[Image: mzembi45dN.png]](http://i.nowfollow.nl/mzembi45dN.png)
edit: yes I know the maximum amount of clients is bullshit and doesn't do anything but I got bored so I didn't finish that
(08-10-2011, 12:58)Pozzuh Wrote:Se7en Wrote:Stealed, from cod4 mod ...look who's talking
[Release] Old School Mod v2.2
[Release] Scroll menu


![[Image: lQDUjba.jpg]](http://i.imgur.com/lQDUjba.jpg)

![[Image: i9rWVTsHYh.png]](http://i.nowfollow.nl/i9rWVTsHYh.png)
![[Image: r212360a129ce9b84444093b6cd2699013a1fbn155.png]](http://roflsaurus.com/users/public/r212360a129ce9b84444093b6cd2699013a1fbn155.png)
