原因:
IE浏览器设代理了,把代理去掉就能调起来了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace SocketConnTimeOut


{
class TimeOutSocket

{
private static bool IsConnectionSuccessful = false;
private static Exception socketexception;
private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);

public static TcpClient Connect(IPEndPoint remoteEndPoint, int timeoutMSec)

{
TimeoutObject.Reset();
socketexception = null;

string serverip = Convert.ToString(remoteEndPoint.Address);
int serverport = remoteEndPoint.Port;
TcpClient tcpclient = new TcpClient();

tcpclient.BeginConnect(serverip, serverport, new AsyncCallback(CallBackMethod), tcpclient);

if (TimeoutObject.WaitOne(timeoutMSec, false))

{
if (IsConnectionSuccessful)

{

return tcpclient;
}
else

{
throw socketexception;
}
}
else

{
tcpclient.Close();
throw new TimeoutException("TimeOut Exception");
}
}
private static void CallBackMethod(IAsyncResult asyncresult)

{
try

{
IsConnectionSuccessful = false;
TcpClient tcpclient = asyncresult.AsyncState as TcpClient;

if (tcpclient.Client != null)

{
tcpclient.EndConnect(asyncresult);
IsConnectionSuccessful = true;
}
}
catch (Exception ex)

{
IsConnectionSuccessful = false;
socketexception = ex;
}
finally

{
TimeoutObject.Set();
}
}

}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Windows.Forms;
namespace SocketConnTimeOut
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
string strIP = "";
int strPort = 8000;
int timeout = 1000;
private void btnConn_Click(object sender, EventArgs e)
{
try
{
strIP = txtIP.Text;
strPort = Convert.ToInt32(txtPort.Text);
IPAddress localAddr = IPAddress.Parse(strIP);
IPEndPoint remoteEndPoint = new IPEndPoint(localAddr, strPort);
TcpClient NetworkClient = TimeOutSocket.Connect(remoteEndPoint, timeout);
}
catch (Exception ex)
{
MessageBox.Show("连接失败");
}
}
}
}
posted @
2010-02-25 14:57 天书 阅读(25) |
评论 (0) |
编辑 收藏
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.NetworkInformation;
namespace PingIpAddress
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private Ping pingSender = new Ping();
private string strIP = "";
private void button1_Click(object sender, EventArgs e)
{
strIP = txtIP.Text;
PingOptions pingOption = new PingOptions();
pingOption.DontFragment = true;
string data = "sendData:goodgoodgoodgoodgoodgood";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(strIP, timeout, buffer);
if (reply.Status == IPStatus.Success)
{
MessageBox.Show("能ping通 ");
}
else
{
MessageBox.Show("ping不通");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using OSSDOM.CMT.ConfigData;
using System.Net;
using System.Net.NetworkInformation;

namespace OSSDOM.CMT.CommonUse


{
public class PingServer

{
private IPEndPoint EPServer = null;
public PingServer()

{
try

{
EPServer = new IPEndPoint(IPAddress.Parse(MakeOptionDatas.MakeOptionDataInstance.proxy.strProxyIP), int.Parse(MakeOptionDatas.MakeOptionDataInstance.proxy.strProxyPort));
}
catch (Exception ex)

{
MessageBox.Show(ex.Message + " " + ex.StackTrace);
}
}
//连接
private Ping pingSender = new Ping();
public bool ServerConnected()

{
try

{
PingOptions pingOption = new PingOptions();
pingOption.DontFragment = true;

string data = "sendData";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(EPServer.Address, timeout, buffer);
if (reply.Status == IPStatus.Success)

{
//MessageBox.Show("能ping通 ");
return true;
}
else

{
//MessageBox.Show("ping不通");
return false;
}
}
catch

{
return false;
}
}
//断开
public void Disconnect()

{
}
}
}

posted @
2010-02-25 14:47 天书 阅读(21) |
评论 (0) |
编辑 收藏
首先:服务端要发四个字节过来,代表接下来他发了多少数据过来,然后每次客户端就读这个长度的数据即可。这样收发就同步了哈哈!
然后:客户端就按照这个长度来读服务端一次发过来的信息。
还要解决一个问题就是 socket接收信息由字节转换成整型:
Byte[] RecNum = new byte[4];
int iRevNum = netstream.Read(RecNum, 0, RecNum.Length);//读取客户发送来的字节信息。
int i = BitConverter.ToInt32(RecNum, 0);
i = System.Net.IPAddress.NetworkToHostOrder(i);
接下来就按i这么大来读信息就能同步了。具体代码见下:(网元监控中使用)
private void Read()
{
try
{
Byte[] Recp = new byte[1024];
if (ClientSocket == null)
{
return;
}
NetworkStream netstream = new NetworkStream(ClientSocket);
int iRevp = netstream.Read(Recp, 0, Recp.Length);//读取客户发送来的信息。
string strRevp = System.Text.Encoding.GetEncoding("gb2312").GetString(Recp, 0, iRevp);
if (strRevp.IndexOf("请输入指令") != -1)
{
string sendMsg = "";
if (curPort.Equals("ALL"))
{
sendMsg = "track " + curNet.NeENName + "\r\n";
}
else
{
sendMsg = "track " + curNet.NeENName + "#" + curPort + "\r\n";
}
DispatchMessage(sendMsg);
}
while (true)
{
try
{
Byte[] RecNum = new byte[4];
int iRevNum = netstream.Read(RecNum, 0, RecNum.Length);//读取客户发送来的字节信息。
int i = BitConverter.ToInt32(RecNum, 0);
i = System.Net.IPAddress.NetworkToHostOrder(i);
Byte[] Rec = new byte[i];
int iRev = netstream.Read(Rec, 0, Rec.Length);//读取客户发送来的信息。
string strRev = System.Text.Encoding.GetEncoding("gb2312").GetString(Rec, 0, iRev);
strRev = strRev.Replace("\0", "");
ProcessReceiveData(strRev);
}
catch (Exception ex)
{
;
}
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
;
}
}
posted @
2010-01-07 11:34 天书 阅读(35) |
评论 (0) |
编辑 收藏
跨线程时 一定要用System.Timer,而不要用System.windows.Forms.Timer.
Timer 控件到点时,一定要先stop然后做其他工作,最后再messageBox.show() ;否则messageBox.show() 将线程阻塞在那块等待用户操作了,要是用户不在没有操作,则不能stop()。
posted @
2010-01-07 10:04 天书 阅读(24) |
评论 (0) |
编辑 收藏
1:网线或者网络的事,比如IP被占用,网线不好用。
查找方法: ping一下网关。
2:接收数据中有"\0",把后面数据截断了。
解决办法: string.replace("\0","")。
posted @
2010-01-07 10:03 天书 阅读(29) |
评论 (0) |
编辑 收藏
colorFont.myFont.fontStyle :int 类型(存在数据库中读出来的)
FontStyle fs = (FontStyle)colorFont.myFont.fontStyle;
Font ft = new Font(colorFont.myFont.fontName, colorFont.myFont.fontSize,fs);
int ifontStyle = (int)ft.fontStyle;
posted @
2009-11-09 20:09 天书 阅读(19) |
评论 (0) |
编辑 收藏