I need to create client server application in which server listens on one port and sends response on another port for same client. I have created something like this which is not working and i guess i am not even near to send and receive on different port. Though i have achieved this for same port. Help highly Appreciated. Thanks
Server:-
public void createListener()
{
TcpListener tcpListener = null;
IPAddress ipAddress = IPAddress.Any;
try
{
tcpListener = new TcpListener(ipAddress, 1818);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
SocketHelper helper = new SocketHelper();
helper.processMsg(tcpClient, stream, bytes);
}
}
class SocketHelper
{
TcpClient mscClient;
string mstrMessage;
string mstrResponse;
byte[] bytesSent;
public void processMsg(TcpClient client, NetworkStream stream, byte[] bytesReceived)
{
mstrMessage = Encoding.ASCII.GetString(bytesReceived, 0, bytesReceived.Length);
mscClient = client;
if (mstrMessage.Length>0)
{
mstrResponse = mstrMessage;
}
else
{
mstrResponse = "You have sent blank message";
}
bytesSent = Encoding.ASCII.GetBytes(mstrResponse);
stream.Write(bytesSent, 0, bytesSent.Length);
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Now Listening";
this.createListener();
}
Client:--
static string output = "";
public Client()
{
InitializeComponent();
}
public void createListener()
{
TcpListener tcpListener = null;
IPAddress ipAddress = IPAddress.Any;
try
{
tcpListener = new TcpListener(ipAddress, 1819);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
Thread.Sleep(10);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
string output = "";
}
}
static string Connect(string serverIP, string message)
{
string output = "";
try
{
Int32 port = 1818;
TcpClient client = new TcpClient(serverIP, port);
Byte[] data = new Byte[256];
data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
output = "Sent: " + message;
MessageBox.Show(output);
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
output = "Received: " + responseData;
MessageBox.Show(output);
stream.Close();
client.Close();
return responseData;
}
catch (ArgumentNullException e)
{
output = "ArgumentNullException: " + e;
MessageBox.Show(output);
return e.Message;
}
catch (SocketException e)
{
output = "SocketException: " + e.ToString();
MessageBox.Show(output);
return e.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
string serverIP = "192.168.0.205";
string message = textBox1.Text;
textBox2.Text=Connect(serverIP, message);
}