Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys...

I want you to help me to do real network between two devices, actually I tried to do chat
and it was Excellent, but I couldn't do it in real IPAddress or in different IPAddress in the same class only you can use it with one IPAddress I tried to use application with VMware but No response.

the Question: how can I use it in different IPAddress (same class)?

Sorry for English

this is the code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
 
namespace ChatApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private static Socket client;
        private static byte[] data = new byte[1024];
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //Thread fh = new Thread(new ThreadStart(findHosts));
            //fh.IsBackground = true;
            //fh.Start();
        }
 
        private void btnListen_Click(object sender, EventArgs e)
        {
 

            listBox1.Items.Add("Listening for a client...");
            Socket newsock = new Socket
             (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8080);
            newsock.Bind(iep);
            newsock.Listen(5);
            newsock.BeginAccept(new AsyncCallback(AcceptConn), newsock);
            Thread advertise = new Thread(new ThreadStart(srvrAdvertise));
            advertise.IsBackground = true;
            advertise.Start();
        }
 
        private void btnConnect_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("Connecting...");
            client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //string selectedhost = (string)listBox2.SelectedItem;
            //string[] hostarray = selectedhost.Split(':');
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
            client.BeginConnect(iep, new AsyncCallback(Connected), client);
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] message = Encoding.ASCII.GetBytes(txtSend.Text);
            txtSend.Clear();
            client.BeginSend(message, 0, message.Length, 0,
                     new AsyncCallback(SendData), client);
        }
 
        void AcceptConn(IAsyncResult iar)
        {
            Socket oldserver = (Socket)iar.AsyncState;
            client = oldserver.EndAccept(iar);
            listBox1.Items.Add("Connection from: " + client.RemoteEndPoint.ToString());
            Thread receiver = new Thread(new ThreadStart(ReceiveData));
            receiver.IsBackground = true;
            receiver.Start();
        }
        void Connected(IAsyncResult iar)
        {
            try
            {
                client.EndConnect(iar);
                listBox1.Items.Add("Connected to: " + client.RemoteEndPoint.ToString());
                Thread receiver = new Thread(new ThreadStart(ReceiveData));
                receiver.IsBackground = true;
                receiver.Start();
            }
            catch (SocketException)
            {
                listBox1.Items.Add("Error connecting");
            }
        }
        void SendData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int sent = remote.EndSend(iar);
        }
        void ReceiveData()
        {
            int recv;
            string stringData;
            while (true)
            {
                recv = client.Receive(data);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                if (stringData == "bye")
                    break;
                listBox1.Items.Add(stringData);
            }
            stringData = "bye";
            byte[] message = Encoding.ASCII.GetBytes(stringData);
            client.Send(message);
            client.Close();
            listBox1.Items.Add("Connection stopped");
            return;
        }
        void srvrAdvertise()
        {
            Socket server = new Socket(AddressFamily.InterNetwork,
                          SocketType.Dgram, ProtocolType.Udp);
            server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 8080);
            byte[] hostname = Encoding.ASCII.GetBytes(Dns.GetHostName());
            while (true)
            {
                server.SendTo(hostname, iep);
                Thread.Sleep(60000);
            }
        }
 
        void findHosts()
        {
            //while (true)
            //{
            //    Socket remoteHosts = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //    IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            //    EndPoint ep = (EndPoint)iep;
            //    remoteHosts.Bind(iep);
            //    byte[] data = new byte[1024];
            //    int recv = remoteHosts.ReceiveFrom(data, ref ep);
            //    string stringData = Encoding.ASCII.GetString(data, 0, recv);
            //    string entry = stringData + ":" + ep.ToString();
            //    if (!listBox2.Items.Contains(entry))
            //        listBox2.Items.Add("" + entry);
            //    break;
            //}
        }
    }
}
Posted
Comments
Richard MacCutchan 19-Feb-14 10:07am    
You need two computers, the server and the client. Start the server system, then call it from the client. All the client needs to know is the IP address of the server, and the port number it is listening on.
Ammar Al-hamdabni 21-Feb-14 5:23am    
Thank you Richard MacCutchan.

But how to make client know the IP address of server. Can you edit the code?

and Can I do it with VMware Workstation?please help me
Richard MacCutchan 21-Feb-14 5:31am    
You need to tell the client the IP address of the server, or use DNS if it is a publicly known DNS name. Either hard code the value or enter it at run time.

I have no experience of VMWare.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900