using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpServer
{
public static void Main()
{try
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
EndPoint tmpRemote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
data = new byte[1024];
string ss = "Welcome to the Server";
data = Encoding.ASCII.GetBytes(ss);
newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
Console.WriteLine("\nSent Acknowledgement");
// newsock.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
// Console.ReadLine();
}
}
//This is the client code which im calling inside a button click event
private void Send_Click(object sender, EventArgs e)
{
Callback();}
private void Callback()
{try
{
byte[] data = new byte[1024];
listBox3.Items.Add("Connecting....");
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
listBox3.Items.Add("Connected");
data = Encoding.ASCII.GetBytes("hello");
listBox3.Items.Add("Transmitting...");
server.SendTo(data, data.Length, SocketFlags.None, ipep);//sent hello
listBox3.Items.Add("Sent...");
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
EndPoint tmpRemote = (EndPoint)sender;
listBox3.Items.Add("Message received from {0}:");
listBox3.Items.Add(tmpRemote.ToString());
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref tmpRemote);
listBox3.Items.Add(Encoding.ASCII.GetString(data,0,recv));
//listBox3.Items.Add("Stopping client");
//server.Close();}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
Console.ReadLine();}
When i run this code:
My form design is as follows:
With a button click i list the drives in the system,then with another button click i list the folders in each drive,and then listing the exe files in the folder..I have set a default path say C:\projects\ports.The ports folders in turn contains many folders like 10000,10001 etc..I have copied the exe file obtained when compiling the Server Application to 10000...So with a button click event i will get the exe of the server application which should act as server and i have defined the client in the Form1.cs..Now when i have another button click the message(which i have defined inside the code)should be sent to the exe(i.e.server) opened...Please check my code and tell me whether i should make any changes.
Right now when i run this application the exe(server)window closes when i click "Send" button to send the message to the exe...Please help me with this

New Topic/Question
Reply




MultiQuote






|