+ Reply to Thread
Results 1 to 2 of 2

Thread: LAN Chatting Program using Sockets Tutorial

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10

    LAN Chatting Program using Sockets Tutorial

    I would like to create a simple LAN Chatting application using network socket programming.

    Can you show me how can that be done ?
    Last edited by sarah; 11-09-2012 at 12:20 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Our application will consist of two main functions :
    - Client
    - Server

    Well , actually for PCs to talk to each other then the these two functions must be present , one to send and one to hear.

    So , how will it work ?
    1) Application will run on both modes : client and server
    2) Server function will be working on the background to be able to listen all messages coming to it on a specific port
    3) When it receives a message it will display it to the user
    4) The user will reply to this message using the client function which will send a message to another PC on the same port but with different IP Address.

    Before beginning , you have to add these libraries :

    Code:
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    Okay i`ll begin with the Server Function :

    Code:
            static void BeingServer()
            {
                // configure the socket properties
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                // Accept Any IP Address and Define the port you want your server to listen on
                socket.Bind(new IPEndPoint(IPAddress.Any, 11511));
                socket.Listen(100);
    
                while (true)
                {
                    Socket AcceptedConnection = socket.Accept();
                    // The Default buffer size is 8192 bytes meant to be recieved at one time
                    Buffer = new byte[AcceptedConnection.SendBufferSize];
    
                    // Get the number of bytes recieved or read
                    int bytesread = AcceptedConnection.Receive(Buffer);
    
                    byte[] Formatted = new byte[bytesread];
    
                    // we will copy the buffer date to Variable Formatted to flush it later otherwise the Buffer will explode :D
                    for (int i = 0; i < bytesread; i++)
                    {
                        Formatted[i] = Buffer[i];
                    }
    
                    // convert these bytes to a readable text
                    string RecivedData = Encoding.ASCII.GetString(Formatted);
    
                    // split that message into two parts , Part 1 for IP and Part 2 for Message
                    string[] SplitRecivedData = RecivedData.Split('|');
    
                    int counter = 0;
    
                    foreach (string get_info in SplitRecivedData)
                    {
                        counter++;
                        // that will extract the ip from message
                        if (counter == 1)
                        {
                            Console.WriteLine("Recieved Data from ip : " + get_info);
                        }
                        // that will display the message
                        if (counter == 2)
                        {
                            Console.WriteLine("Data are >>>" + get_info);
                        }
                    }
                    counter = 0;
                    Buffer = null;
    
    
                    //Console.WriteLine(RecivedData);
                    //Console.Read();
                    //socket.Close();
                    //AcceptedConnection.Close();
                }
            }
    The Client Function :

    Code:
            static void Being_Client(string IP_ADDRESS ,string Data)
            {
                // configure the socket properties
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                try
                {
                    // try to establish a connection to that IP Address with that Port
                    socket.Connect(IP_ADDRESS, 11511);
                    Console.WriteLine("Connection Established with " + IP_ADDRESS);
                }
                catch(Exception) { Console.WriteLine("Failed To Connect to the Other Host"); }
    
                // convert the message into bytes
                byte[] send_data = Encoding.ASCII.GetBytes(Data);
    
                // And then send the message
                socket.Send(send_data);
    
                // Finally close the Connection
                socket.Close();
            }
    Now Inside Main :

    Code:
            static void Main(string[] args)
            {
                Thread t = new Thread(new ThreadStart(BeingServer));
                t.Start();
    
                Console.WriteLine("|---Write The IP Address you want to communicate with---|");
                Console.WriteLine("---------------------------------------------------------");
                string ip_address = Console.ReadLine();
    
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine("Now Enter the data :");
                    Console.WriteLine("--------------------");
                    string datatobesent = Console.ReadLine();
    
                    // Get the Local IP Address of that machine
                    string myHost = Dns.GetHostName();
                    // Send data along with the source IP Address , so the server knows who it is talking to
                    Being_Client(ip_address, Dns.GetHostByName(myHost).AddressList[0].ToString() + "|" + datatobesent);
                }
            }
    I guess Comments clarify everything .
    If you do not have two PCs on your home or office , just send the message to IP : 127.0.0.1 << that is a loopback IP Address , means your Computer Internal IP Address , actually anything goes to 127.0.0.1 is for your pc

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts