Release [C#] Chat server
#1
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 Smile

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:

.rar   BasicServer.rar (Size: 84.75 KB / Downloads: 250)


Please give some advice in case you know a better/faster way of doing this Smile

pic:
[Image: 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 Big Grin
(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

Reply

#2
looks very cool
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply

#3
Good job @ieagle
[Image: lQDUjba.jpg]
Reply

#4
Nice one.

[Image: r212360a129ce9b84444093b6cd2699013a1fbn155.png]
Reply

#5
(02-08-2012, 16:34)G-Man Wrote: Nice one.


This is not for actual use (unless you feel like using this instead of skype/msn/irc etc.). I just wanted to learn how to use simple network code.
(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

Reply

#6
(02-08-2012, 16:57)iAegle Wrote:
(02-08-2012, 16:34)G-Man Wrote: Nice one.


This is not for actual use (unless you feel like using this instead of skype/msn/irc etc.). I just wanted to learn how to use simple network code.

I said that "nice one" as i know that your work is much harder and idk if i can do same)) the thing you made is also a chat but it has another working system (much better than just web-chat) me just Dumb Bitch
[Image: r212360a129ce9b84444093b6cd2699013a1fbn155.png]
Reply

#7
thanks for your work
Reply

#8
I set to multiple start up projects give error 404
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
Rainbow How can i add my private server in unranked? Geded12 6 10,482 12-06-2025, 08:25
Last Post: jackey78
  [Release] Windows 8.1 Fix for MW3 Server Addon master131 16 30,727 09-29-2014, 23:08
Last Post: SuperNovaAO
Brick [Release] MW3 Server Administration Addon iRoNinja 5 10,846 11-10-2013, 15:46
Last Post: Casper
Exclamation Help cmdlist, dvarlist server crash Nerus 17 16,413 11-09-2013, 23:54
Last Post: Nerus
  Our Level Fastfile is Different from the Server. CheeseToast 6 15,495 11-03-2013, 17:52
Last Post: CheeseToast
  Dedicated Server External (public) IP Nerus 3 9,542 11-02-2013, 14:16
Last Post: Casper
  MW3 Server Version superg1973 7 17,462 10-28-2013, 01:15
Last Post: kotyra972
  Help how to turn off map in dedicated server pero123 8 9,547 10-15-2013, 19:00
Last Post: Nekochan
  Issue with server addon and NAT dimitrifrom31 3 8,142 10-08-2013, 18:11
Last Post: iRoNinja
  GETTING SERVER ONLINE raym 6 9,137 09-28-2013, 22:42
Last Post: Nekochan

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.