PDA

View Full Version : آموزش: ارسال ديتا با استفاده از GPRS در بستر TCP/IP



hunter_ara
سه شنبه 12 مهر 1390, 11:37 صبح
سلام دوستان تصميم گرفتم اين اموزش رو بزارم
چون اين پروژه حدود 1 ماه درگيرم كرده بود و هرچي تو اين سايت يا سايت هاي مشابه Search كردم مطلبي كه يه كمك درست و حسابي بهم بكنه پيدا نكردم.

هركي استفاده كرد منم دعا كنه.

كدي كه ميزارم شامل Server و Client ميشه. براي من جواب داده ولي نميتونم تضمين كنم كه همه جا درست كار كنه!!!

Base code رو ميزارم كه هر كسي بسته به نيازش تغييرش بده.
در حال حاضر Client به Server كانكت ميشه و سرور به ريپلاي Successful Connection به كلاينت ميده.

كد سرور يا به اصطلاح Listener :

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SimpleServer
{
class Program
{
public static void Main()
{
// Data is send in bytes -- so we need to convert a C#‎ string to a Byte[]
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] message = encoding.GetBytes("Congratulation. \n You had a successful connection");
try
{
// Setup a listener at the local IP adres, port 2200
IPAddress localAddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localAddress, 2200);
// Start listening, only allow 1 connection to queue at the same time
listener.Start(1);
// Start listening for connections.
while (true)
{
Console.WriteLine("Server is waiting on socket {0}", listener.LocalEndpoint);
// The program is suspended while waiting for an incoming connection.
// This is a synchronous TCP application
TcpClient client = listener.AcceptTcpClient();
// Obtain a stream object for reading and writing
NetworkStream io = client.GetStream();
// An incoming connection needs to be processed.
Console.WriteLine("Received Connection from {0}", client.Client.RemoteEndPoint);
Console.WriteLine("Sending message..");
io.Write(message, 0, message.Length);
// End of the incomming connection
Console.WriteLine("Ending the connection");
client.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception: {0}", e.ToString());
}
}
}
}


كد Client :


public void DoTcpConnection()
{
string url = "127.0.0.1";
bool res = GPRSConnection.Setup("http://" + url + "/");
try
{
if (res)
{
TcpClient tc = new TcpClient(url, 2200);
NetworkStream ns = tc.GetStream();
byte[] buf = new byte[100];
ns.Write(buf, 0, 100);
tc.Client.Shutdown(SocketShutdown.Both);
ns.Close();
tc.Close();
MessageBox.Show("Text Send sucssesfully!");
}
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
TxtSend.Text = string.Empty;
}
//---------------------------------------------------------------------------------------------------
public class GPRSConnection
{
const int S_OK = 0;
const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;
const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
const uint INFINITE = 0xffffffff;
const uint CONNMGR_STATUS_CONNECTED = 0x10;
static Hashtable ht = new Hashtable();
static GPRSConnection()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.Handle = ConnMgrApiReadyEvent();
mre.WaitOne();
CloseHandle(mre.Handle);
}
~GPRSConnection()
{
ReleaseAll();
}
public static bool Setup(Uri url)
{
return Setup(url.ToString());
}
public static bool Setup(string urlStr)
{
ConnectionInfo ci = new ConnectionInfo();
IntPtr phConnection = IntPtr.Zero;
uint status = 0;
if (ht[urlStr] != null)
return true;
if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK)
return false;
ci.cbSize = (uint)Marshal.SizeOf(ci);
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ci.bExclusive = 0;
ci.bDisabled = 0;
ci.hWnd = IntPtr.Zero;
ci.uMsg = 0;
ci.lParam = 0;
if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status)
!= S_OK && status != CONNMGR_STATUS_CONNECTED)
return false;
ht[urlStr] = phConnection;
return true;
}
public static bool Release(Uri url)
{
return Release(url.ToString());
}
public static bool Release(string urlStr)
{
return Release(urlStr, true);
}
private static bool Release(string urlStr, bool removeNode)
{
bool res = true;
IntPtr ph = IntPtr.Zero;
if (ht[urlStr] == null)
return true;
ph = (IntPtr)ht[urlStr];
if (ConnMgrReleaseConnection(ph, 1) != S_OK)
res = false;
CloseHandle(ph);
if (removeNode)
ht.Remove(urlStr);
return res;
}
public static void ReleaseAll()
{
foreach (DictionaryEntry de in ht)
{
Release((string)de.Key, false);
}
ht.Clear();
}
[StructLayout(LayoutKind.Sequential)]
public struct ConnectionInfo
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public int bExclusive;
public int bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[DllImport("cellcore.dll")]
private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex);
[DllImport("cellcore.dll")]
private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
[DllImport("cellcore.dll")]
private static extern IntPtr ConnMgrApiReadyEvent();
[DllImport("cellcore.dll")]
private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
[DllImport("coredll.dll")]
private static extern int CloseHandle(IntPtr hObject);
}
}
}
تو Client هر جا خواستيد كانكت بشيد فقط كافيه تابع DoTcpConnection() رو Call كنيد.

يه DLL هم هست كه براي ديدن Exeption هاي درست بايد به Refrence اضافه كنيد كه واقعا اجدادم از جلوي چشمم رد شدن تا رديفش كنم.

76212

سرور و كلاينت هر دو تو اين كد Local هستن كه بايد IP هر دو رو به IP سرور تغيير بدين و پورت هم بايد روي سرور براي TCP باز باشه

zelatan
چهارشنبه 16 آذر 1390, 15:40 عصر
با سلام میخاستم ببینم چه جوری باید فایلexe برنامه کلاینت را روی موبایل ریخت؟

zelatan
پنج شنبه 17 آذر 1390, 11:34 صبح
هیچکس نمیدونه چه جوری باید فایل exe کلاینت را روی موبایل ریخت ؟

mahvar
سه شنبه 27 آبان 1393, 10:05 صبح
سلام .
من اگه بخوام از مودم gprs برای ارسال یک متن به سرور استفاده کنم باید چی کار کنم.