Click here to Skip to main content
15,922,533 members
Home / Discussions / C#
   

C#

 
AnswerRe: Passing an array from C# into a C function (Interop) Pin
Luc Pattyn5-Oct-10 5:15
sitebuilderLuc Pattyn5-Oct-10 5:15 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
harold aptroot5-Oct-10 5:29
harold aptroot5-Oct-10 5:29 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
harold aptroot5-Oct-10 7:02
harold aptroot5-Oct-10 7:02 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
Luc Pattyn5-Oct-10 7:15
sitebuilderLuc Pattyn5-Oct-10 7:15 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
harold aptroot5-Oct-10 7:23
harold aptroot5-Oct-10 7:23 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
Henry Minute5-Oct-10 7:49
Henry Minute5-Oct-10 7:49 
GeneralRe: Passing an array from C# into a C function (Interop) Pin
harold aptroot5-Oct-10 7:56
harold aptroot5-Oct-10 7:56 
QuestionThreaded server client two way communication problems and best practises [modified] Pin
teknolog1235-Oct-10 2:36
teknolog1235-Oct-10 2:36 
Hi Everyone, Sorry for long code below. I thought it was the best way to express my problem. I would be glad if you could forward me to an article or could just answer the below questions. (in spite of reading many articles.I'm confused about the below issues)

- On click of a button, server/client will want server/client to do something(in addition to what's below) but I couldn't implement it.Errors and Errors Frown | :(
- Is it good practice to use CanRead and DataAvailable properties of NetworkStream not to face with exceptions?
- When server or client want each other to do something(two way job request) Is this implementation capable of handling it?
- On each loop at client side, I open and close connection. Is it good practice?
- Can I determine the size of the byte buffer according to the incoming stream? I currently put a random size

Thanks in advance for your patience and help.
Regards

 //Server
private void AnaForm_Load(object sender, EventArgs e)
        {
            thdListener = new Thread(new ThreadStart(listenerThread));
            thdListener.Start();
          }

 public void listenerThread()
 {
     try
     {
         string ipAdress = GetIP();
         IPAddress IPadres = IPAddress.Parse(ipAdress);
         IPEndPoint ipEndPoint = new IPEndPoint(IPadres, 8000);
         TcpListener tcpListener = new TcpListener(ipEndPoint);
         tcpListener.Start();
         while (true)
         {
             listenerSocket = tcpListener.AcceptSocket();
             ntrStream = new NetworkStream(listenerSocket);

            if (listenerSocket.Connected)
            {
                if (ntrStream.CanRead)
                {
                    byte[] byt = new byte[500];
                    ntrStream.Read(byt, 0, byt.Length);
                    MemoryStream ms = new MemoryStream(byt);
                    BinaryFormatter bf = new BinaryFormatter();
                    hashTableInfo.Clear();
                    hashTableInfo = (Hashtable)bf.Deserialize(ms); 
                }
                if (msc.Name.Remove(0, 4) == hashTableInfo["MasaNo"].ToString())
                {
                    thdHandler = new Thread(new ThreadStart(handlerThread));
                    thdHandler.Start();
                }
            }
		}
	}
    catch (Exception)
    {
    }
}
public void handlerThread()
{
    ntrStream = new NetworkStream(listenerSocket);
    try
    {
         //Send Time info
         if (ntrStream.CanWrite)
         {
             MemoryStream msTime = new MemoryStream();
             msTime = new MemoryStream();
             BinaryFormatter bfTime = new BinaryFormatter();
             bfTime.Serialize(msTime, hashTimes);
             byte[] bellekSure = new byte[msTime.Length];
             bellekSure = msTime.ToArray();
             ntrStream.Write(bellekSure, 0, bellekSure.Length); 
         }

          //Send Table of Drinks
        if (ntrStream.CanWrite)
        {
            MemoryStream msDrinks = new MemoryStream();
            BinaryFormatter bfDrinks = new BinaryFormatter();
            bfDrinks.Serialize(msDrinks, hashDrinks);
            byte[] bellekKafe = new byte[msDrinks.Length];
            bellekKafe = msDrinks.ToArray();
            ntrStream.Write(bellekKafe, 0, bellekKafe.Length);
            urunlerGuncellendi = false;  
        }
    }
    catch (Exception)
    {
        
    }
}


//Client
 private void Client_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
		
 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) //servera bağlan ve istemciyi güncelle
 {
    
while(true)
{
try
    {
        tcpClient = new TcpClient("serverName", 8000);
        if (tcpClient.Connected)
        {
            ntrStream = tcpClient.GetStream();

            // Send Table info to server
            MemoryStream msSip = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(msSip, htTableInfo);
            byte[] byt = new byte[msSip.Length];
            byt = msSip.ToArray();
            if (ntrStream.CanWrite) ntrStream.Write(byt, 0, byt.Length);

            //Get Time info of the table from server
            if (ntrStream.CanRead)
            {
                byte[] bytTimes = new byte[500]; ///How can I determin the size according to incoming stream?
                ntrStream.Read(bytTimes, 0, bytTimes.Length);
                MemoryStream msTimes = new MemoryStream(bytTimes);
                BinaryFormatter bfSure = new BinaryFormatter();
                hashTimes.Clear(); 
                hashTimes = (Hashtable)bfSure.Deserialize(msTimes);
                if (InvokeRequired)  this.Invoke(new dlgApplyTimes(applyTimes));
                else applyTimes();
            }

            //Get table of drinks from server
            if (ntrStream.DataAvailable)
            {
                byte[] bellekKafe = new byte[500];
                ntrStream.Read(bellekKafe, 0, bellekKafe.Length);
                MemoryStream msKafe = new MemoryStream(bellekKafe);
                BinaryFormatter bfKafe = new BinaryFormatter();
                hashDrinks.Clear(); 
                hashDrinks = (Hashtable)bfKafe.Deserialize(msKafe);
                if (InvokeRequired) this.Invoke(new dlgApplyDrinks(applyDrinks));
                else appyDrinks();
            }
            ntrStream.Close();
            tcpClient.Close();
            tcpClient = null;
        }
    }
    catch (Exception) 
    {
    }
    Thread.Sleep(3000); 
} 
 }      


modified on Tuesday, October 5, 2010 8:42 AM

AnswerMessage Closed Pin
5-Oct-10 3:00
stancrm5-Oct-10 3:00 
GeneralRe: Threaded server client two way communication problems and best practises Pin
teknolog1235-Oct-10 3:30
teknolog1235-Oct-10 3:30 
AnswerRe: Threaded server client two way communication problems and best practises Pin
PIEBALDconsult5-Oct-10 3:04
mvePIEBALDconsult5-Oct-10 3:04 
GeneralRe: Threaded server client two way communication problems and best practises Pin
teknolog1235-Oct-10 3:36
teknolog1235-Oct-10 3:36 
GeneralRe: Threaded server client two way communication problems and best practises Pin
PIEBALDconsult5-Oct-10 15:08
mvePIEBALDconsult5-Oct-10 15:08 
QuestionXML File problem Pin
InderK5-Oct-10 1:22
InderK5-Oct-10 1:22 
QuestionHow to identify is a certificate is in a smart card? Pin
Sunil P V4-Oct-10 22:38
Sunil P V4-Oct-10 22:38 
AnswerRe: How to identify is a certificate is in a smart card? Pin
Rajesh Anuhya4-Oct-10 23:28
professionalRajesh Anuhya4-Oct-10 23:28 
AnswerRe: How to identify is a certificate is in a smart card? Pin
Goutam Patra5-Oct-10 2:15
professionalGoutam Patra5-Oct-10 2:15 
QuestionB.K.M for dynamically resolve assembly location - a question [modified] Pin
Shultz 24-Oct-10 20:57
Shultz 24-Oct-10 20:57 
AnswerRe: B.K.M for dynamically resolve assembly location - a question Pin
Mycroft Holmes4-Oct-10 22:00
professionalMycroft Holmes4-Oct-10 22:00 
GeneralRe: B.K.M for dynamically resolve assembly location - a question Pin
Shultz 24-Oct-10 23:18
Shultz 24-Oct-10 23:18 
AnswerRe: B.K.M for dynamically resolve assembly location - a question Pin
Bernhard Hiller4-Oct-10 22:25
Bernhard Hiller4-Oct-10 22:25 
GeneralRe: B.K.M for dynamically resolve assembly location - a question Pin
Shultz 24-Oct-10 23:12
Shultz 24-Oct-10 23:12 
AnswerRe: B.K.M for dynamically resolve assembly location - a question Pin
PIEBALDconsult5-Oct-10 3:07
mvePIEBALDconsult5-Oct-10 3:07 
GeneralRe: B.K.M for dynamically resolve assembly location - a question Pin
Shultz 25-Oct-10 4:59
Shultz 25-Oct-10 4:59 
GeneralRe: B.K.M for dynamically resolve assembly location - a question Pin
PIEBALDconsult5-Oct-10 15:08
mvePIEBALDconsult5-Oct-10 15:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.