广州高深商电子技术有限公司 用户登录
                                                      用户注册 ......
                                                      首页 公告 收款机 抽奖机 来电管理 数据采集 台球灯控 软件开发 综合 会员下载 发货查询
                                                      公司首页
                                                      当前位置:首页>> 来电管理>> 二次开发 >>正文
                                                      使用C#的Socket开发网络版来电管理软件
                                                      chen在2009/9/7发表,被浏览4372
                                                      适合对象:记得来电显示管理器的网络版
                                                      开发环境:Visual C#
                                                      下载范例:6.zip ( 46.13K )  [ 2009-09-07 ]

                                                      使用环境:需要来电监听服务程序TeleListen.exe(点击下载)
                                                      相关连接:来电管理器服务程序




                                                      using System;
                                                      using System.Collections.Generic;
                                                      using System.ComponentModel;
                                                      using System.Data;
                                                      using System.Drawing;
                                                      using System.Text;
                                                      using System.Windows.Forms;
                                                      using System.Net;
                                                      using System.Net.Sockets;
                                                      using System.Threading;

                                                      namespace _
                                                      {
                                                          public partial class Form1 : Form
                                                          {
                                                              static string strReceived="";
                                                              static string server;
                                                              static int port;
                                                              static Socket socketTele;
                                                              static Thread thrListen;

                                                              public Form1()
                                                              {
                                                                  InitializeComponent();
                                                              }
                                                              
                                                              private static Socket ConnectSocket()
                                                              {
                                                                  Socket socket = null;                    //实例化Socket对象,并初始化为空
                                                                  IPHostEntry iphostentry = null;          //实例化IPHostEntry对象,并初始化为空
                                                                  iphostentry = Dns.GetHostEntry(server);  //获得主机信息
                                                                  //循环遍历得到的IP地址列表
                                                                  foreach (IPAddress address in iphostentry.AddressList)
                                                                  {
                                                                      //使用指定的IP地址和端口号实例化IPEndPoint对象
                                                                      IPEndPoint IPEPoint = new IPEndPoint(address, port);
                                                                      //使用Socket的构造函数实例化一个Socket对象,以便用来连接远程主机
                                                                      Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                                                                      newSocket.Connect(IPEPoint);          //调用Connect方法连接远程主机
                                                                      if (newSocket.Connected)              //判断远程连接是否连接
                                                                      {
                                                                          socket = newSocket;
                                                                          break;
                                                                      }
                                                                      else
                                                                      {
                                                                          continue;
                                                                      }
                                                                  }
                                                                  return socket;
                                                              }

                                                              //接收Socket服务发送的内容
                                                              private static string SocketReceive()
                                                              {
                                                                  Byte[] btReceived = new Byte[256];
                                                                  int intContent = 0;
                                                                  if (socketTele == null) return "Not connected";
                                                                  do
                                                                  {
                                                                      Application.DoEvents();
                                                                      //从绑定的Socket接收数据
                                                                      intContent = socketTele.Receive(btReceived, btReceived.Length, 0);
                                                                      //将接收到的数据转换为字符串类型
                                                                      strReceived += Encoding.ASCII.GetString(btReceived, 0, intContent) + "\n";
                                                                  }
                                                                  while (intContent > 0);
                                                                  return strReceived;
                                                              }

                                                              //使用线程监听Socket
                                                              public static void ListenThread()
                                                              {
                                                                  //调用自定义方法SocketSendReceive获取指定主机的主页面内容
                                                                  string strContent = SocketReceive();
                                                                  MessageBox.Show(strContent);
                                                              }

                                                              //启动线程
                                                              private void button1_Click(object sender, EventArgs e)
                                                              {
                                                                  server = textBox1.Text;               //指定主机名
                                                                  port = Convert.ToInt32(textBox2.Text);   //指定端口号
                                                                  socketTele = ConnectSocket();
                                                                  if (socketTele == null)
                                                                  {
                                                                      strReceived = "连接失败!\n";
                                                                      return;
                                                                  }
                                                                  thrListen = new Thread(new ThreadStart(ListenThread));
                                                                  thrListen.Start();
                                                              }

                                                              //关闭Socket连接
                                                              private void button2_Click(object sender, EventArgs e)
                                                              {
                                                                  if (socketTele != null)
                                                                  {
                                                                      socketTele.Close();
                                                                      thrListen.Abort();
                                                                  }
                                                              }

                                                              //显示接收的内容
                                                              private void timer1_Tick(object sender, EventArgs e)
                                                              {
                                                                  int i;
                                                                  if (strReceived.Length > 0)
                                                                  {
                                                                      i = strReceived.IndexOf("\n");
                                                                      while (i > 0)
                                                                      {
                                                                          listBox1.Items.Add(strReceived.Substring(0, i+1));
                                                                          strReceived=strReceived.Remove(0, i+1);
                                                                          i = strReceived.IndexOf("\n");
                                                                      }
                                                                  }
                                                              }
                                                          }
                                                      }
                                                      【相关文章】
                                                      使用C#的Socket开发…
                                                      【同类文章】
                                                      高深商GSM无线固话盒OC…
                                                      WIFI接口来电显示管理器…
                                                      新一代USB转COM口来电…
                                                      来电管理器故障排除的方法
                                                      在WIN7或Win8中注册…
                                                      计算机收发短信的OCX控件…
                                                      使用Java开发来电显示管…
                                                      开发来电显示及录音程序的C…
                                                      C#来电显示管理器开发示例…
                                                      一个用JavaScript…

                                                      广州高深商电子技术有限公司

                                                      地址:天河北路908号、高科大厦B座2704
                                                      电话:020-38259081、38258857
                                                      电子邮箱:gaoykosen@foxmail.com

                                                      彩神Ⅴll