This is a discussion on who to send and receive data? within the Microsoft .NET Tutorials and Articles forums, part of the Articles and Tutorials category; hi, i new in compactframework and i need to build a aplication that can send and receive information between a ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| FAQ: How to find people's E-mail addresses | David Alex Lamb | Tech FAQ | 1 | 06-24-2007 09:57 AM |
| Artificial Intelligence FAQ:5/6 AI Web Directories & Online Papers [Monthly posting] | Ric Crabbe and Amit Dubey | Tech FAQ | 0 | 06-02-2004 01:12 AM |
| Artificial Intelligence FAQ:5/6 AI Web Directories & Online Papers [Monthly posting] | Ric Crabbe and Amit Dubey | Tech FAQ | 0 | 05-05-2004 04:14 PM |
| FAQ: How to find people's E-mail addresses | David Alex Lamb | Tech FAQ | 0 | 05-02-2004 06:30 AM |
| Apple II Csa2 FAQs: Telecom Hardware & Transfers, Part 20/25 | rubywand@swbell.net | Tech FAQ | 0 | 04-04-2004 08:29 AM |