Search Here : TutorialsDefcon ProgrammingBugmenotMilliworm Proxy DiscussHere SourceCodeWinProgDownloadBlackBookHackingDrugsMovies....
Welcome to Ozone...Enjoy the Ozone feel Here Get Updated Daily for self improvement...
Google Yahoo Sify RediffIRCTCGmail Orkut TeluguFM TamilFM Msn Hotmail Cricket Youtube English Astro Raaga Music TechInterviews Ebooks Naukri Monster TimesJob TimePassMails
Hindustan Times Indian Express Times of India Financial Express Dhina Malar Deccan Chronicle The Hindu The Pioneer catalog Freshers World WikiPedia Kamesh Chalasani Blog

Monday, July 6, 2009

Server - Code for Messenger

using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Data;
using System.Collections;
using System.Windows;
using System.Runtime;
using System.ComponentModel;
using System.Globalization;
using System.Configuration;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.InteropServices;


namespace Server
{
public partial class Form1 : Form
{
static N.Sockets.TcpClient TC;
[DllImport("kernel32.dll")]
private static extern void ExitProcess(int a);
public Form1()
{
InitializeComponent();
}
System.Net.Sockets.TcpListener server;
public static Hashtable handles;
public static Hashtable handleByConnect;
IPAddress ip;
Boolean asss;
public void TalkServ()
{
asss = true;
ip = IPAddress.Parse("192.168.3.200");
handles = new Hashtable(100);
handleByConnect = new Hashtable(100);
server = new TcpListener(ip,13000);
while(true)
{
server.Start();
if(server.Pending())
{
N.Sockets.TcpClient connection = server.AcceptTcpClient();
MessageBox.Show("Connection Made");
BackForth BF = new BackForth(connection);
}
server.Stop();
}
}
public static void SendToAll(string name, string msg)
{

StreamWriter SW;
ArrayList ToRemove = new ArrayList(0);
N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[handles.Count];
handles.Values.CopyTo(tc, 0);
for (int i = 0; i < tc.Length; i++)
{
try
{
if (msg.Trim() == "" || tc[i] == null)
continue;
SW = new StreamWriter(tc[i].GetStream());
SW.WriteLine(name + ": " + msg);
SW.Flush();
SW = null;
}
catch (Exception)
{
//e44 = e44;
string g = (string)handleByConnect[tc[i]];
SendSysMsg("** " + g + " ** HAS LEFT US.");
handles.Remove(g);
handleByConnect.Remove(tc[i]);
}
}
}
public static void SendSysMsg(string msg)
{
StreamWriter SW;
ArrayList ToRemove = new ArrayList(0);
N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[handles.Count];
handles.Values.CopyTo(tc, 0);
for (int i = 0; i < tc.Length; i++)
{
try
{
if (msg.Trim() == "" || tc[i] == null)
continue;
SW = new StreamWriter(tc[i].GetStream());
SW.WriteLine(msg);
SW.Flush();
SW = null;
}
catch (Exception)
{
handles.Remove(handleByConnect[tc[i]]);
handleByConnect.Remove(tc[i]);
}
}
}

private static void talk_Closing(object s, CancelEventArgs e)
{
e.Cancel = false;
Application.Exit();
ExitProcess(0);
}
private static void key_up(object s, KeyEventArgs e)
{
TextBox TB = (TextBox)s;
if (TB.Lines.Length > 1)
{
StreamWriter SW = new StreamWriter(TC.GetStream());
SW.WriteLine(TB.Text);
SW.Flush();
TB.Text = " ";
TB.Lines = null;
}
}

private void Form1_Load_1(object sender, EventArgs e)
{
TalkServ();
}
}

class BackForth
{
N.Sockets.TcpClient client;
System.IO.StreamReader SR;
System.IO.StreamWriter SW;
public string handle;

public BackForth(System.Net.Sockets.TcpClient c)
{
client = c;
Thread t = new Thread(new ThreadStart(init));
t.Start();
}

private string GetHandle()
{
SW.WriteLine("What is your Name? ");
SW.Flush();
return SR.ReadLine();
}

public void run()
{
try
{
string l = " ";
while (true)
{
l = SR.ReadLine();
Form1.SendToAll(handle, l);
}
}
catch (Exception e44)
{
MessageBox.Show(e44.ToString());
}
}

public void init()
{
SR = new System.IO.StreamReader(client.GetStream());
SW = new System.IO.StreamWriter(client.GetStream());
SW.WriteLine("WELCOME TO ChatOffice! Be Nice!");
SW.WriteLine("What is your Name? ");
SW.Flush();
handle = GetHandle();
while (Form1.handles.Contains(handle))
{
SW.WriteLine("ERR - Name already exists!");
handle = GetHandle();
}
Form1.handles.Add(handle, client);
Form1.handleByConnect.Add(client, handle);
Form1.SendSysMsg("** " + handle + " ** HAS JOINED US.");
SW.WriteLine("Now Talking.....\r\n-------------------------------");
SW.Flush();
Thread t = new Thread(new ThreadStart(run));
t.Start();
}
}
}

0 comments:

Post a Comment

 
Get subscribers