Client- Sever,

makiavelli

Grünschnabel
Client- Sever Application, brauche dringend Hilfe!

Hallo, ich muss eine Client-Server Application herstellen unzwar einen virtueller Shop. Ich kriege es aber nicht hin die Information von Server zu Client zu ubertragen. Ich mochte das der Client die Informationen uber die Produkte sehen kann( die Funktion anzeigen aus der server Class) und dass der Client die Operation fur Kaufen( Funktion operation_durchfueren aus server class machen soll)

Hier ist sind die Quell-Datein

Code:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Collections;



public class clnt {
	
	public static void Main() {
		
		try {
			TcpClient tcpclnt = new TcpClient();
			StreamReader ttt= new StreamReader ( new FileStream ( "c:\\password.txt", System.IO.FileMode.Open ) );
			string line= ttt.ReadToEnd();
			Console.WriteLine(line);Console.WriteLine("Connecting.....");
			Console.WriteLine(line.Length);
			Console.WriteLine("enter password");
			string parola= Console.In.ReadLine ();
			int i =line.IndexOf(parola, 0, line.Length);
			
			if (i >0)
			
			{
				//bool zzz = true;
				tcpclnt.Connect("192.168.0.7",8001); // use the ipaddress as in the server program
				Console.WriteLine("Connected");
				Stream stm = tcpclnt.GetStream();
				byte[] bb=new byte[100];
				int k=stm.Read(bb,0,100);
			
				for (int j=0;j<k;j++)
					Console.Write(Convert.ToChar(bb[i]));
				tcpclnt.Close();}
			else
				Console.WriteLine("Parola gresita");
				tcpclnt.Close();
				//break; 
			
		}
		
		catch (Exception e) {
			Console.WriteLine("Error..... " + e.StackTrace);
		}
	}

}

Der Server

Code:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.IO;
using System.Threading;

public class serv {
	private int temp;
	public ArrayList mc; //Produse collection
	private void lesen ()
	{
		
		mc= new ArrayList ( 100 );
		StreamReader sr= new StreamReader ( new FileStream ( "c:\\date.in", System.IO.FileMode.Open ) );
		string line= sr.ReadLine ();
		int n= System.Int32.Parse ( line );
		int i;
		for ( i=0; i<n; i++ )
		{
				
			String l1= sr.ReadLine ();
			String l2= sr.ReadLine ();
			String l3= sr.ReadLine ();
			Produse m= new Produse ( l1, l2, l3 );
			mc.Add ( m );
		}
		sr.Close ();
	}
	private void speichern ()
	{
		
		StreamWriter sw= new StreamWriter ( new FileStream ( "c:\\date.in", System.IO.FileMode.OpenOrCreate ) );
		sw.WriteLine ( mc.Count );
		
		foreach ( Produse m in mc )
		{
			sw.WriteLine ( m.nm );
			sw.WriteLine ( m.v1 );
			sw.WriteLine ( m.v2 );
		}
			
		sw.Close ();
	}
		
	private  void anzeigen ()
	{
		citire_date();
		Console.Out.WriteLine ( "date server:" );
		int contor= 0;
		foreach ( Produse m in mc )
		{
			Console.Out.WriteLine ( contor +" "+ m.toString () );
			contor ++;
		}
		
	}
		
	private void operation_durchfueren ()
	{
		Console.Out.WriteLine ( "durchfueren:" );
		string name= Console.In.ReadLine ();
		Produse x= null;
		foreach ( Produse m in mc )
		{
			if ( m.nm == name )
			{
				x= m;
				break;
			}
		}
			
		if ( x == null ) 
		{	
			Console.Out.WriteLine ( "nume inexistent." );				
			return;
		}
			
		Console.Out.WriteLine ( "v1,v2:" );
		string v1= Console.In.ReadLine ();
		string v2= Console.In.ReadLine ();
				
		int ind= mc.IndexOf ( x );
		mc.Remove ( x );
		mc.Insert ( ind, new Produse ( name, v1 , v2 ) );
	}
		
	public static void Main() 
	{
	try {
		IPAddress ipAd = IPAddress.Parse("192.168.0.7"); 
		TcpListener myList=new TcpListener(ipAd,8001);
		myList.Start();
		bool zzz= true;
		while (zzz )
		{
			Console.WriteLine("The server is running at port 8001...");	
			Console.WriteLine("The local End point is  :" + myList.LocalEndpoint );
			serv bla= new serv();
			bla.lesen();
			Console.WriteLine("Waiting for a connection.....");
			Socket s=myList.AcceptSocket();
			Console.WriteLine("Connection accepted from "+s.RemoteEndPoint);
			
			ArrayList mc;	
			mc= new ArrayList ( 100 );
			foreach ( Produse m in mc )	
			{
				string str = m.toString ();
				Console.WriteLine(str);	
				byte[] ba=asen.GetBytes(str); 
				s.SendTo(ba,  s.RemoteEndPoint);

			}
			Console.WriteLine("\nSent Acknowledgement");
					}	
	}
	catch (Exception e) {
		Console.WriteLine("Error..... " + e.StackTrace);
	}	
	}
	
}
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück