S
saqsaqPK
Guest
I am new in C# Programming Kindly Advise me.
create simple TCPIP Listener and TCP IP Clint (Consol application ) c#
My Problems.
1).when i use Client Application to Send Data to Server . Clint Application Send Data Successfully one time when i tray to send second data client application Close.
2).Restart the Client Application and Try to send Data to server . this time data not transfer until Server Application Restart.
3.) how to Received continuous data from Clint .?
4.) how to send ACK , ENQ between Server and Clint
Kindly Advise us .
Server Code :-
namespace ServerOK
{
class Program
{
static void Main(string[] args)
{
try
{
string HostName = Dns.GetHostName();
String MyIP = Dns.GetHostByName(HostName).AddressList[0].ToString();
IPAddress IPAd = IPAddress.Parse(MyIP);
IPAddress ipAd = IPAddress.Parse(MyIP); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(""));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
}
Clint Code :-
public class clnt
{
public static void Main()
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("192.168.43.9", 8001); // use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb));
tcpclnt.Close();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
Continue reading...
create simple TCPIP Listener and TCP IP Clint (Consol application ) c#
My Problems.
1).when i use Client Application to Send Data to Server . Clint Application Send Data Successfully one time when i tray to send second data client application Close.
2).Restart the Client Application and Try to send Data to server . this time data not transfer until Server Application Restart.
3.) how to Received continuous data from Clint .?
4.) how to send ACK , ENQ between Server and Clint
Kindly Advise us .
Server Code :-
namespace ServerOK
{
class Program
{
static void Main(string[] args)
{
try
{
string HostName = Dns.GetHostName();
String MyIP = Dns.GetHostByName(HostName).AddressList[0].ToString();
IPAddress IPAd = IPAddress.Parse(MyIP);
IPAddress ipAd = IPAddress.Parse(MyIP); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(""));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
}
Clint Code :-
public class clnt
{
public static void Main()
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("192.168.43.9", 8001); // use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb));
tcpclnt.Close();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
Continue reading...