i create client server program using c#
client program is form application and server is console application.
in client i had 4 forms, when i send data from first form , server can receive it, but when i send from another form , server can not receive the data.
any one know why??
look : in client side i used windows application form,
first form it takes user name and password and send it to server.
then server check if this user is available and send availability to
client,
then client open another form and close the first form
and ask for some details like tel,mob and etc,(until here i don't have
any problem)
then it should send details to server,
but it seems the server dosen't accept the data from another form, it seems it connect only to first form.
the code for form1 is :
TcpClient c = new TcpClient("127.0.0.1", 1000);
NetworkStream ns = c.GetStream();
string st1 = textBox1.Text;
string st2 = textBox2.Text;
string name = string.Concat("F" + len + len2 + len3 + st1 + passhash + st3);
byte[] buf = System.Text.Encoding.ASCII.GetBytes(name);
ns.Write(buf, 0, name.Length);
this work for first form,but when i use this code for another form it doesn't work, i don't no why?
now i know the problem but i don't know how to solve this
it seems each form needs specific port number.
in this case i can not assign to each form a port, because it had many form, in other hand in server side i should listen to many port.
i tried to make child form, but it also doesn't work, and it's not inherit tcpclient.
so, what would the solution be ?
this is server code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWMOdel;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener l = new TcpListener(IPAddress.Any, 1000);
l.Start();
Console.WriteLine("Server has started.Waiting for clients...");
TcpClient c = l.AcceptTcpClient();
Console.WriteLine(" >> Accept Connection From Clinet ");
NetworkStream ns = c.GetStream();
while(true)
{
byte[] buf = new byte[200];
ns.Read(buf, 0, 200);
string st = System.Text.Encoding.ASCII.GetString(buf);
Console.WriteLine(st);
int le = st[1];
int le2 = st[2];
int le3 = st[3];
string flag = st.Substring(0, 1);
string name;
string pass;
string position;
name = st.Substring(4, le);
pass = st.Substring(4 + le, le2);
position = st.Substring(4 + le + le2, le3);
Console.WriteLine(name);
byte[] buf2;
if (string.Compare(name, "Administrator")==0)
{
string one = Convert.ToString(1);
buf2 = System.Text.Encoding.ASCII.GetBytes(Convert.ToString(one));
ns.Write(buf2, 0, one.Length);
}
NetworkStream ns2 = c.GetStream();
byte[] stin = new byte[200];
ns.Read(stin, 0, 100);
string stinfo = System.Text.Encoding.ASCII.GetString(stin);
Console.WriteLine(stinfo);
}
}
}
}
this is client code :
form 1 :
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.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
namespace newform1
{
public partial class Form1 : Form
{
TcpClient c = new TcpClient("127.0.0.1", 1000);
public Form1()
{
InitializeComponent();
textBox2.PasswordChar = '*';
}
private void Form1_Load(object sender, EventArgs e)
{
NetworkStream ns = c.GetStream();
}
public string CreateMD5Hash(string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream ns = c.GetStream();
string st1 = textBox1.Text;
string st2 = textBox2.Text;
string st3 = comboBox1.Text;
string passhash = CreateMD5Hash(st2);
int lenght1 = st1.Length;
int lenght2 = st2.Length;
int length3 = st3.Length;
char len;
char len2;
char len3;
len = Convert.ToChar(lenght1);
len2 = Convert.ToChar(lenght2);
len3 = Convert.ToChar(length3);
string name = string.Concat("F" + len + len2 + len3 + st1 + passhash + st3);
byte[] buf = System.Text.Encoding.ASCII.GetBytes(name);
ns.Write(buf, 0, name.Length);
byte[] buf2 = new byte[100];
ns.Read(buf2, 0, 100);
string add = System.Text.Encoding.ASCII.GetString(buf2);
if(string.Compare(add,"1")==1)
{
this.Hide();
studentform myform = new studentform();
myform.Show();
}
}
}
}
and this is code for second form :
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.Net;
using System.Net.Sockets;
namespace newform1
{
public partial class Form2 : Form
{
TcpClient c = new TcpClient("127.0.0.1", 1000);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream ns = c.GetStream();
string stin = "1";
byte[] buf = System.Text.Encoding.ASCII.GetBytes(stin);
ns.Write(buf, 0, stin.Length);
addremst myform = new addremst();
this.Hide();
myform.Show();
}
}
}
as i told before i gave one port to two form, it seems server doesn't accept connection from second form, i want them to use same port,
i have to many form, i don't want to give each one , port.