Exforsys
+ Reply to Thread
Results 1 to 2 of 2

who to send and receive data?

This is a discussion on who to send and receive data? within the Microsoft .NET forums, part of the Programming Talk category; hi, i new in compactframework and i need to build a aplication that can send and receive information between a ...

  1. #1
    jason2 is offline Junior Member Array
    Join Date
    Feb 2007
    Answers
    1

    who to send and receive data?

    hi,
    i new in compactframework and i need to build a aplication that can send and receive information between a mobile phone using compactframework and a pc using a winsock control.

    i can i receive and send information in the mobile? i have tryed this:

    Dim server As TcpListener

    server = Nothing

    Try

    ' Set the TcpListener on port 13000.

    Dim port As Int32 = 3071

    Dim localAddr As IPAddress = IPAddress.Parse("192.168.2.129")

    server = New TcpListener(localAddr, port)

    server.Server.Listen(1)

    ' Start listening for client requests.

    server.Start()

    with this code the program stucks and i canīt work with the program

    who can can i receive and send information in the mobile to pc like i using winsock control?

    can anyone help ?

    thanks a lot


  2. #2
    jillujillu is offline Junior Member Array
    Join Date
    Aug 2007
    Answers
    1

    Thumbs up

    Hi,
    Here the answer goes..

    The algorithm is like this.

    1) Get the data
    2) Open the socket
    3) Wait for the recieve data
    4) Send the data (3 and 4 can be replacable)
    5) Receive the data
    6) process and use the data
    7) go and wait for the recieve data


    public class Communicator
    {
    Socket ServerSocket;
    Queue MessageQeue;
    AutoResetEvent QeueSetEvent;
    Thread CommThread;

    IAsyncResult result;
    public AsyncCallback pfnCallBack;
    public StringBuilder rcvXMLRPCMsg = new StringBuilder();

    public void InitializeComm()
    {
    this.MessageQeue = new Queue();
    this.QeueSetEvent = new AutoResetEvent(false);

    this.CommThread = new Thread(new ThreadStart(StartMaint));
    CommThread.Start();
    }
    public void SendMaintenanceData(object objMessage)
    {
    this.MessageQeue.Enqueue(objMessage);
    this.QeueSetEvent.Set();

    }
    private Socket ConnectWithServer()
    {
    SetServerSocket(null);
    bool bCOnnectionMade = false;
    IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
    IPEndPoint remoteEp = new IPEndPoint(ipAddress, 5000);
    Socket tempSocket = new Socket(remoteEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

    while(!bCOnnectionMade)
    {
    tempSocket.Connect(remoteEp);
    if(tempSocket.Connected)
    {
    bCOnnectionMade = true;
    SetServerSocket(tempSocket);
    WaitForData();
    }
    else
    {
    SetServerSocket(null);
    Thread.Sleep(1000);
    continue;
    }
    }
    return tempSocket;
    }

    private void SendDataToServer(string data)
    {
    Byte[] bytesSent = Encoding.ASCII.GetBytes(data);
    Socket ServerSocket = GetServerSocket();
    if (ServerSocket == null)
    {
    ServerSocket = ConnectWithServer();
    SendDataToServer(data);
    }
    else
    {
    int nErrorCode = ServerSocket.Send(bytesSent, bytesSent.Length, 0);
    }
    }
    private void StartMaint()
    {
    while(true)
    {
    this.QeueSetEvent.WaitOne();

    while(MessageQeue.Count > 0)
    {
    data = (string) MessageQeue.Dequeue();
    SendDataToServer(data);
    }
    }
    }
    public void OnDataReceived(IAsyncResult asyn)
    {
    SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
    int iRx = theSockId.thisSocket.EndReceive (asyn);
    char[] chars = new char[iRx + 1];
    if(iRx > 0)
    {
    System.Text.Decoder d = System.Text.Encoding.ASCII.GetDecoder();
    int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
    System.String szData = new System.String(chars);
    rcvXMLRPCMsg.Append(szData);
    }
    WaitForData();
    }
    private void WaitForData()
    {
    if ( pfnCallBack == null )
    {
    pfnCallBack = new AsyncCallback (OnDataReceived);
    }
    SocketPacket theSocPkt = new SocketPacket ();
    theSocPkt.thisSocket = GetServerSocket();
    // Start listening to the data asynchronously
    this.result = GetServerSocket().BeginReceive (theSocPkt.dataBuffer,
    0, theSocPkt.dataBuffer.Length,
    SocketFlags.None,
    pfnCallBack,
    theSocPkt);
    }
    public class SocketPacket
    {
    public System.Net.Sockets.Socket thisSocket;
    public byte[] dataBuffer = new byte[2048];
    }

    private Socket GetServerSocket()
    {
    return this.ServerSocket;
    }
    private void SetServerSocket(Socket Sock)
    {
    this.ServerSocket = Sock;
    }
    private void ReinitializeSocket()
    {
    ServerSocket.Close();
    SetServerSocket(null);
    }

    for more information visit ekutta . com
    Thanks
    Satheesh


Latest Article

Network Security Risk Assessment and Measurement

Read More...