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: 213)
Please give some advice in case you know a better/faster way of doing this
pic:
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:
CSHARP 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 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.Start();
-
- {
- while( true )
- {
- TcpClient Client = Listener.AcceptTcpClient();
- ClientThread.Start( Client );
- Connected++;
- }
- } ).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;
- 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 );
- Clients.Add( Client );
- Console.WriteLine( "New connection: " + Client.Name );
-
- while( true )
- {
- Recieved = 0;
- 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
CSHARP 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;
-
- {
- byte[] buffer;
- int Recieved;
- while( true )
- {
- try
- {
- 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 )
- }
- }
- }
Client login form
CSHARP 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
- {
- 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();
- Chat.Show();
- this.Hide();
- }
- catch( Exception ex )
- {
- MessageBox.Show( ex.ToString() );
- }
- }
-
- string GetRealName( string name )
- {
- 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
- Data.Add( "username", User );
- Data.Add( "password", Password );
- Data.Add( "submit", "Login" );
- Data.Add( "action", "do_login" );
-
- 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: 213)
Please give some advice in case you know a better/faster way of doing this
pic:
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