以下是完整代码:
首先是程序入口类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using HurysCommon;
using System.Xml;
using System.Configuration;
using HurysModel;
namespace HurysDataConventTools
{
class Program
{
static void Main(string[] args)
{
try
{
//1.建立Socket实例,连接四通诱导平台
IPAddress ip = IPAddress.Parse(SocketHelper._host);
IPEndPoint ipe = new IPEndPoint(ip, SocketHelper._port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(ip + " Connecting...................");
socket.Connect(ipe);
if (socket.Connected)
{
Console.WriteLine(ip + " Connected...................");
}
while (true)
{
//2.发送登录请求
string sendStr = SocketHelper._login;
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("Send Login Requst...................");
socket.Send(bs, bs.Length, 0);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("Client Get Message:{0}", recvStr);//显示服务器返回信息
-------这个是sockethelper类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net.Sockets;
using System.Collections;
using System.Configuration;
using System.Net;
namespace HurysCommon
{
public class SocketHelper
{
public static string _host = ConfigurationManager.AppSettings["host"].ToString();
public static int _port = CommonFun.StrToInt(ConfigurationManager.AppSettings["port"].ToString());
public static string _login = GetRequestXml("Login.xml");
public static string _error = GetRequestXml("Error.xml");
public static string _vms = GetRequestXml("Vms.xml");
public static string _page = GetRequestXml("Page.xml");