i am interested in Client & Server application. By the way, i am working on real time project. I have come accross a series problem for me and i cannot figure out. Would you help me about this sowftware. Let me explain my project below;
my project consists of two parts. One of them is Client other, part is Server. There is device on router. I am trying to connect this device via ethernet. I can connect to the device over its Ipnumber and port number which are 172.16.204.100, 50007 and i can send my data ( command ).
CLIENT PART
namespace client
{
class Program
{
static Socket sck;
//static byte[] Buffer { get; set; }
static void Main(string[] args)
{
Console.Title = "rrrrrr";
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localendpoint = new IPEndPoint(IPAddress.Parse("172.16.204.100"), 50007); //50007 bidirectional interface The event
//channel is unidirectional and connects across port 50008.The event channel reports asynchronous events such as errors, tag arrivals, and digital output triggers.
try
{
sck.Connect(localendpoint);
if (sck.Poll(-1, SelectMode.SelectWrite))
{
Console.WriteLine("this socket is writable");
}
else if (sck.Poll(-1, SelectMode.SelectRead))
{
Console.WriteLine("this socket is readable");
}
else if (sck.Poll(-1, SelectMode.SelectError))
{
Console.WriteLine("this socket has an error");
}
}
catch
{
Console.Write(" unable ");
Main(args);
}
Console.Write(" \r\n ");
// Using the RemoteEndPoint property.
Console.WriteLine("I am connected to " + IPAddress.Parse(((IPEndPoint)sck.RemoteEndPoint).Address.ToString()) + " on port number " + ((IPEndPoint)sck.RemoteEndPoint).Port.ToString());
Console.Write(" \r\n ");
// Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)sck.LocalEndPoint).Address.ToString()) + " I am connected on port number " + ((IPEndPoint)sck.LocalEndPoint).Port.ToString());
Console.Write(" \r\n ");
Console.Write("enter text: ");
string text = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(text);
sck.Send(data);
Console.Write(" data sent! \r\n ");
//Console.Write(" press ant key to continue ");
Console.Read();
sck.Close();
}
}
}
then, I wrote my computer's Ipnumber for setting up connection between Device and my computer in Bind method. i am start to wait getting response. At this point, I have never got an answer. I think that My port number should be wrong and this can cause a problem. I don't know what is the problem actually. I just know that I cannot get any responce.
By the way, I have just been studying this platform for almost 15 days. I am not professional. I am really in trouble about this work.
Would you help me? If someone can help me, i will be really pleasure to him.
SERVER PART
namespace server
{
class Program
{
static byte[] Buffer { get; set; }
static Socket sck;
static void Main(string[] args)
{
Console.Title = " Server ";
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.Bind(new IPEndPoint(IPAddress.Parse("172.16.204.104"), 4561));
//sck.Bind(new IPEndPoint(IPAddress.Parse("172.16.204.104"), 0));
while (true)
{
sck.Listen(100);
Socket accepted = sck.Accept();
Buffer = new byte[accepted.SendBufferSize];
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
string strData = Encoding.ASCII.GetString(formatted);
Console.Write(strData + "\r\n");
Console.Read();
sck.Close();
accepted.Close();
}
}
}
}