Friend i m new in .net socket programing ............
i have made many management application in C#.net .........

i have tried to make chat application over local network and i have made it.

but now i want to make a chat application which work over internet .............

can anyone help me ?

plz email me at [email removed]

Recommended Answers

All 4 Replies

Its exactly the same. The only problem is that people are behind routers, firewalls, and other systems that make a direct connection more difficult.

So what you end up having to do is create a server somewhere that is directly connected to the internet, that way all the clients can connect to it without having to forward ports and all that jazz.

It's really not worth the effort unless you have the resources to keep up a dedicated server, and really want to use such a chat application.

Thank you very much for your reply.....

i have heard that through IMPP (Instant Messaging and Presence Protocol) we can make chat application............

can u help me for IMPP? i just want idea about it.....

Try using Server - Client appication with tcpListener and tcpClient. This is an example code:
Source

//The client:

using System.IO; 
using System.Net; 
using System; 
using System.Threading; 
using N = System.Net; 
using System.Collections; 
using System.Windows.Forms; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 

class TalkUser { 
    
   static Form talk; 
   static N.Sockets.TcpClient TC; 

   [DllImport("kernel32.dll")] 
   private static extern void ExitProcess(int a); 
    
   public static void Main() { 
       talk = new Form(); 
       talk.Text = "TalkUser - The OFFICIAL TalkServ Client"; 
       talk.Closing += new CancelEventHandler(talk_Closing); 
       talk.Controls.Add(new TextBox()); 
       talk.Controls[0].Dock = DockStyle.Fill; 
       talk.Controls.Add(new TextBox()); 
       talk.Controls[1].Dock = DockStyle.Bottom; 
       ((TextBox)talk.Controls[0]).Multiline = true; 
       ((TextBox)talk.Controls[1]).Multiline = true; 
       talk.WindowState = FormWindowState.Maximized; 
       talk.Show(); 
       ((TextBox)talk.Controls[1]).KeyUp += new KeyEventHandler(key_up); 
       TC = new N.Sockets.TcpClient(); 
       TC.Connect("IP OF A SERVER HERE",4296); 
       Thread t = new Thread(new ThreadStart(run)); 
       t.Start(); 
       while(true) { 
           Application.DoEvents(); 
       } 
   } 
    
   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 static void run() { 
       StreamReader SR = new StreamReader(TC.GetStream()); 
       while(true) { 
           Application.DoEvents(); 
           TextBox TB = (TextBox)talk.Controls[0]; 
           TB.AppendText(SR.ReadLine()+"\r\n"); 
           TB.SelectionStart = TB.Text.Length; 
       } 
   } 
}

//And the server:

using System.IO; 
using System.Net; 
using System; 
using System.Threading; 
using N = System.Net; 
using System.Collections; 

class TalkServ { 
    
   System.Net.Sockets.TcpListener server; 
   public static Hashtable handles; 
   public static Hashtable handleByConnect; 
    
   public static void Main() { 
       TalkServ TS = new TalkServ(); 
   } 

   public TalkServ() { 
       handles = new Hashtable(100); 
       handleByConnect = new Hashtable(100); 
       server = new System.Net.Sockets.TcpListener(4296); 
       while(true) { 
           server.Start(); 
           if(server.Pending()) { 
               N.Sockets.TcpClient connection = server.AcceptTcpClient(); 
               Console.WriteLine("Connection made"); 
               BackForth BF = new BackForth(connection);             
           } 
       } 
   } 
    
   public static void SendToAll(string name,string msg) { 
       StreamWriter SW; 
       ArrayList ToRemove = new ArrayList(0); 
       N.Sockets.TcpClient[] tc = new N.Sockets.TcpClient[TalkServ.handles.Count]; 
       TalkServ.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 = e44; 
               string g = (string) TalkServ.handleByConnect[tc[i]]; 
               TalkServ.SendSysMsg("** " + g + " ** HAS LEFT US."); 
               TalkServ.handles.Remove(g); 
               TalkServ.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[TalkServ.handles.Count]; 
       TalkServ.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 e44) { e44 = e44; 
               TalkServ.handles.Remove(TalkServ.handleByConnect[tc[i]]); 
               TalkServ.handleByConnect.Remove(tc[i]); 
           } 
       } 
   } 
}//end of class TalkServ 

class BackForth { 
   N.Sockets.TcpClient client; 
   System.IO.StreamReader SR; 
   System.IO.StreamWriter SW; 
   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 handle? "); 
       SW.Flush(); 
       return SR.ReadLine(); 
   } 
    
   private void run() { 
       try { 
       string l = ""; 
       while(true) { 
           l = SR.ReadLine(); 
           TalkServ.SendToAll(handle,l); 
       } 
       } catch(Exception e44) { Console.WriteLine(e44); } 
   } 
    
   private void init() { 
       SR = new System.IO.StreamReader(client.GetStream()); 
       SW = new System.IO.StreamWriter(client.GetStream()); 
       SW.WriteLine("WELCOME TO TalkServ! Be Nice!"); 
       //SW.WriteLine("What is your handle? "); 
       //SW.Flush(); 
       handle = GetHandle(); 
       while(TalkServ.handles.Contains(handle)) { 
           SW.WriteLine("ERR - Handle already exists!"); 
           handle = GetHandle(); 
       } 
       TalkServ.handles.Add(handle,client); 
       TalkServ.handleByConnect.Add(client,handle); 
       TalkServ.SendSysMsg("** " + handle + " ** HAS JOINED US."); 
       SW.WriteLine("Now Talking.....\r\n-------------------------------"); 
       SW.Flush(); 
       Thread t = new Thread(new ThreadStart(run)); 
       t.Start(); 
   } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.