i am trying to create a server - client chat application. but it dosen't recive the messages i send.
can anyone help me??
sorce:
public class Server { public List mailing; public TcpClient client; public TcpListener listener; public int Get_port; public int Send_port; public bool contiuneing; public List ToSend;
public Server(int get_port, int send_port) { Get_port = get_port; Send_port = send_port;
client = new TcpClient(); listener = new TcpListener(Get_port);
contiuneing = true; ToSend = new List(); mailing = new List(); }
public void start() { listener = new TcpListener(new IPEndPoint(IPAddress.Any, Get_port)); listener.Start();
contiuneing = true;
Thread recivethread = new Thread(getdata); recivethread.IsBackground = true; recivethread.Start(); }
public void getdata() { while (contiuneing) { try { using (TcpClient client = listener.AcceptTcpClient()) { NetworkStream stream = client.GetStream(); using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { string read = r.ReadString(); ToSend.Add(read); Console.WriteLine(read); } } } sendata(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { listener.Stop(); } } } public void sendata() { if (ToSend.Count != 0) { foreach (IPAddress i in mailing) { try { client.Connect(i, Send_port);
NetworkStream stream = client.GetStream();
using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { foreach (string s in ToSend) { w.Write(s); } } } } catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.ToString()); } finally { client.Close(); } } } } }
class Program { static bool iserver; static Server server; static IPAddress serverIP; static IPAddress yourIP; static void Main(string[] args) { Console.WriteLine("are you the server?[y/n]");
if (Console.ReadLine().ToLower() == "y") { server = new Server(8010, 8015); iserver = true; server.contiuneing = true;
server.start(); Console.WriteLine("you are the server"); } else { Console.WriteLine("IP of server:?"); serverIP = IPAddress.Parse(Console.ReadLine()); Console.WriteLine("your IP:?"); yourIP = IPAddress.Parse(Console.ReadLine()); NetworkManager.SendData(serverIP, 8015, "I" + yourIP.ToString()); } Thread getthread = new Thread(getdata); getthread.IsBackground = true; getthread.Start(); Console.WriteLine("hum...click...hum...click...click"); Console.ReadLine(); if (!iserver) { TcpClient client = new TcpClient(); client.Connect(serverIP, 8010);
NetworkStream stream = client.GetStream();
using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { while (true) { string read = Console.ReadLine(); if (read != "QUIT") { w.Write(read); } } } } }
} private static void getdata() { while (true) { TcpListener listener; if (!iserver) { listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8015)); } else { listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8005)); } try { using (TcpClient client = listener.AcceptTcpClient()) { NetworkStream stream = client.GetStream(); using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { string read = r.ReadString(); if (!iserver) { Console.WriteLine(read); } else { Console.WriteLine(read); if (read[0] == "I".ToCharArray()[0]) { read.Remove(0, 1); server.mailing.Add( IPAddress.Parse(read)); } } } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { listener.Stop(); } } } } public class NetworkManager { public static void SendData(IPAddress address, int port, string message) { TcpClient client = new TcpClient();
try { client.Connect(address, port);
NetworkStream stream = client.GetStream();
using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { w.Write(message); } } } catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.ToString()); } finally { client.Close(); } }
public static string GetData(int port) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any,port)); listener.Start();
try { using (TcpClient client = listener.AcceptTcpClient()) { NetworkStream stream = client.GetStream(); using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { return r.ReadString(); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); return null; } finally { listener.Stop(); } }
/// /// Waits Untill data is found /// /// /// /// public static string GetData(int port,IPAddress address) { TcpListener listener = new TcpListener(new IPEndPoint(address, port)); listener.Start();
try { using (TcpClient client = listener.AcceptTcpClient()) { NetworkStream stream = client.GetStream(); using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { return r.ReadString(); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); return null; } finally { listener.Stop(); } } }
|
thanks, for reading i am only a student and have not leart much so please exuse my nieve question.
John WiesePosted Jun 3, 2010, 9:30 AM
I don't have time right now to test your app, but a quick glance makes it appear that you have a simple port issue with this line:
NetworkManager.SendData(serverIP, 8015, "I" + yourIP.ToString());
You are sending data to the wrong port. When you create your server it sets up to Listen on port #8010 and Send on port #8015
Change this line to use port 8010 and see if that fixes it.