private void Test1()
{
try
{
this.m_strNetAddress = "127.0.0.1";
m_nPort = 8081;
SocketInformation sInfo = new SocketInformation();
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(m_strNetAddress), m_nPort);
AsyncCallback onconnect = new AsyncCallback(OnConnect);
s.BeginConnect(epServer, onconnect, s);
(为什么总是连接不上?这几句有什么错误呢?)
int nOut = 0;
for(int i=0; i<this.nBufSize; i++)
buffer[i] = this.fileBuffer[this.iCurPosition+i];
nOut = s.Send(this.buffer );
this.iCurPosition += this.nBufSize;
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
public void OnConnect(IAsyncResult ar)
{
// Socket was the passed in object
Socket sock = (Socket)ar.AsyncState;
try
{
//sock.EndConnect( ar );
if (sock.Connected)
{
/*
byte[] abc = new byte[256];
for(int i=0; i<256; i++)
abc[i] = (byte)i;
///int error = sock.Send(abc, 0, 256, 0);
SetupRecieveCallback( sock );
*/
}
else
MessageBox.Show("Unable to connect to remote machine", "Connect Failed!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unusual error during Connect!");
}
}