Exforsys

Online Training

who to send and receive data?

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 ...


Go Back   Exforsys > Articles and Tutorials > Microsoft .NET Tutorials and Articles

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2007, 07:33 AM
Junior Member
 
Join Date: Feb 2007
Posts: 2
jason2 is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-28-2007, 08:20 PM
Junior Member
 
Join Date: Aug 2007
Posts: 1
jillujillu is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new questions
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

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 08: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 12: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 03:14 PM
FAQ: How to find people's E-mail addresses David Alex Lamb Tech FAQ 0 05-02-2004 05:30 AM
Apple II Csa2 FAQs: Telecom Hardware & Transfers, Part 20/25 rubywand@swbell.net Tech FAQ 0 04-04-2004 07:29 AM


All times are GMT -4. The time now is 10:38 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.