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