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]

Dani AI

Generated

— good job getting a LAN version working. For the internet the usual reality is that NATs/routers and firewalls block arbitrary direct client-to-client sockets, so most real-world chat systems use a publicly reachable server (or else a NAT‑traversal stack like ICE/STUN/TURN when true P2P is required). This is essentially what pointed out. For a standards-based route, IMPP describes the model for presence/IM and XMPP is the widely used, open protocol that implements IM + presence. (developer.mozilla.org)

Practical choices for a .NET implementation:

  • Quickest path: a simple central server (hosted VPS or cloud) with persistent connections from clients; implement authentication, presence state, and message routing. Using WebSockets (or Microsoft SignalR) removes a lot of plumbing for browser or .NET clients and handles reconnection/scaling concerns.
  • Standards path: run or connect to an XMPP server if federation/features (rosters, presence, MUC) are wanted.
  • P2P path: only recommended when willing to implement ICE/STUN/TURN or use WebRTC data channels. (learn.microsoft.com)

Implementation and safety checklist (practical, immediate fixes and tests):

  • Host the server where it has a public IP (or use dynamic DNS + port‑forwarding for home hosting).
  • Secure raw sockets with TLS (SslStream) and authenticate users. (learn.microsoft.com)
  • Avoid blocking the UI thread: use async APIs (AcceptTcpClientAsync / async/await) and proper async server loops instead of polling or tight DoEvents loops. Marshal UI updates to the UI thread with Invoke/InvokeAsync rather than updating controls directly from worker threads. (learn.microsoft.com)

A pragmatic next plan: prototype a minimal public server (echo + login), test clients over the internet, add secure auth & presence, then add offline storage, heartbeats and exponential backoff reconnect. The sample posted by is a useful learning reference but should be modernized (remove Application.DoEvents, use async I/O, marshal UI calls, call Start once and dispose streams properly). For someone new to .NET sockets this staged approach keeps each problem isolated and easier to debug. (blog.codinghorror.com)

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(); 
   } 
}

thank you ...

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.