c#异步.客户端和服务器端都做完了.但发现客户机连不上服务器端.
客户端在:
//在此句停止
Socket client=(Socket)ar.AsyncState;
client.EndConnect(ar);而服务器端在:
private void target()
{
while(true)
{
MyReSet.Reset();
mySocket.BeginAccept(new AsyncCallback(AcceptCallBack),mySocket);
MyReSet.WaitOne();//执行完就停止了.一直没发现客户端连接上
}
}我测试的时候服务器端写了127.0.0.1和端口1234调试
或本机本地局域网地址:192.168.1.5和端口1234调试
但发觉客户端还是连不上.源代码在下面地址:ftp://a1:[email protected]/%BB%D8%B5%F7%CA%BE%C0%FD/谢谢.

解决方案 »

  1.   

    客户端:using System;
    using System.Drawing;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;
    class AsyncTcpClient:Form
    {
      private TextBox newText;
      private TextBox conStatus;
      private ListBox results;
      private Socket client;
      private byte[] data = new byte[1024];
      private int size = 1024;
      public AsyncTcpClient()
      {
       Text = "Asynchronous TCP Client";
       Size = new Size(400, 380);
       
       Label label1 = new Label();
       label1.Parent = this;
       label1.Text = "Enter text string:";
       label1.AutoSize = true;
       label1.Location = new Point(10, 30);
       newText = new TextBox();
       newText.Parent = this;
       newText.Size = new Size(200, 2 * Font.Height);
       newText.Location = new Point(10, 55);
       results = new ListBox();
       results.Parent = this;
       results.Location = new Point(10, 85);
       results.Size = new Size(360, 18 * Font.Height);
       Label label2 = new Label();
       label2.Parent = this;
       label2.Text = "Connection Status:";
       label2.AutoSize = true;
       label2.Location = new Point(10, 330);
       conStatus = new TextBox();
       conStatus.Parent = this;
       conStatus.Text = "Disconnected";
       conStatus.Size = new Size(200, 2 * Font.Height);
       conStatus.Location = new Point(110, 325);
       Button sendit = new Button();
       sendit.Parent = this;
       sendit.Text = "Send";
       sendit.Location = new Point(220,52);
       sendit.Size = new Size(5 * Font.Height, 2 * Font.Height);
       sendit.Click += new EventHandler(ButtonSendOnClick);
       Button connect = new Button();
       connect.Parent = this;
       connect.Text = "Connect";
       connect.Location = new Point(295, 20);
       connect.Size = new Size(6 * Font.Height, 2 * Font.Height);
       connect.Click += new EventHandler(ButtonConnectOnClick);
       Button discon = new Button();
       discon.Parent = this;
       discon.Text = "Disconnect";
       discon.Location = new Point(295,52);
       discon.Size = new Size(6 * Font.Height, 2 * Font.Height);
       discon.Click += new EventHandler(ButtonDisconOnClick);
      }
      void ButtonConnectOnClick(object obj, EventArgs ea)
      {
       conStatus.Text = "Connecting...";
       Socket newsock = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);
       IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.2.218"), 9050);
       newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
      }
      void ButtonSendOnClick(object obj, EventArgs ea)
      {
       byte[] message = Encoding.ASCII.GetBytes(newText.Text);
       newText.Clear();
       client.BeginSend(message, 0, message.Length, SocketFlags.None,
              new AsyncCallback(SendData), client);
      }
      void ButtonDisconOnClick(object obj, EventArgs ea)
      {
       client.Close();
       conStatus.Text = "Disconnected";
      }
      void Connected(IAsyncResult iar)
      {
       client = (Socket)iar.AsyncState;
       try
       {
         client.EndConnect(iar);
         conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
         client.BeginReceive(data, 0, size, SocketFlags.None,
                new AsyncCallback(ReceiveData), client);
       } catch (SocketException)
       {
         conStatus.Text = "Error connecting";
       }
      }
      void ReceiveData(IAsyncResult iar)
      {
       Socket remote = (Socket)iar.AsyncState;
       int recv = remote.EndReceive(iar);
       string stringData = Encoding.ASCII.GetString(data, 0, recv);
       results.Items.Add(stringData);
      }
      void SendData(IAsyncResult iar)
      {
       Socket remote = (Socket)iar.AsyncState;
       int sent = remote.EndSend(iar);
       remote.BeginReceive(data, 0, size, SocketFlags.None,
              new AsyncCallback(ReceiveData), remote);
      }
      public static void Main()
      {
       Application.Run(new AsyncTcpClient());
      }
    }
    服务器:
    using System;
    using System.Drawing;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;
    class AsyncTcpSrvr:Form
    {
      private TextBox conStatus;
      private ListBox results;
      private byte[] data = new byte[1024];
      private int size = 1024;
      private Socket server;
      public AsyncTcpSrvr()
      {
       Text = "Asynchronous TCP Server";
       Size = new Size(400, 380);
       results = new ListBox();
       results.Parent = this;
       results.Location = new Point(10, 65);
       results.Size = new Size(350, 20 * Font.Height);
       Label label1 = new Label();
       label1.Parent = this;
       label1.Text = "Text received from client:";
       label1.AutoSize = true;
       label1.Location = new Point(10, 45);
       Label label2 = new Label();
       label2.Parent = this;
       label2.Text = "Connection Status:";
       label2.AutoSize = true;
       label2.Location = new Point(10, 330);
       conStatus = new TextBox();
       conStatus.Parent = this;
       conStatus.Text = "Waiting for client...";
       conStatus.Size = new Size(200, 2 * Font.Height);
       conStatus.Location = new Point(110, 325);
       Button stopServer = new Button();
       stopServer.Parent = this;
       stopServer.Text = "Stop Server";
       stopServer.Location = new Point(260,32);
       stopServer.Size = new Size(7 * Font.Height, 2 * Font.Height);
       stopServer.Click += new EventHandler(ButtonStopOnClick);
       server = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream, ProtocolType.Tcp);
       IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
       server.Bind(iep);
       server.Listen(5);
       server.BeginAccept(new AsyncCallback(AcceptConn), server);
      }
      void ButtonStopOnClick(object obj, EventArgs ea)
      {
       Close();
      }
      void AcceptConn(IAsyncResult iar)
      {
       Socket oldserver = (Socket)iar.AsyncState;
       Socket client = oldserver.EndAccept(iar);
       conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
       string stringData = "Welcome to my server";
       byte[] message1 = Encoding.ASCII.GetBytes(stringData);
       client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
             new AsyncCallback(SendData), client);
      }
      void SendData(IAsyncResult iar)
      {
       Socket client = (Socket)iar.AsyncState;
       int sent = client.EndSend(iar);
       client.BeginReceive(data, 0, size, SocketFlags.None,
             new AsyncCallback(ReceiveData), client);
      }
      void ReceiveData(IAsyncResult iar)
      {
       Socket client = (Socket)iar.AsyncState;
       int recv = client.EndReceive(iar);
       if (recv == 0)
       {
         client.Close();
         conStatus.Text = "Waiting for client...";
         server.BeginAccept(new AsyncCallback(AcceptConn), server);
         return;
       }
       string receivedData = Encoding.ASCII.GetString(data, 0, recv);
       results.Items.Add(receivedData);
       byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
       client.BeginSend(message2, 0, message2.Length, SocketFlags.None,
              new AsyncCallback(SendData), client);
      }
      public static void Main()
      {
       Application.Run(new AsyncTcpSrvr());
      }
    }
      

  2.   

    可以试一试用localhost 连接
      

  3.   

    不是好长.那二楼的代码.我的代码放于ftp中了.能有空下载运行看.看看实际的问题目是什么嘛?
      

  4.   

    楼上.问题我己经说了啊.还有.完整的工程在ftp上.你下载下来运行下就知道了.工程里有完整的代码.
      

  5.   

    楼上.问题我己经说了啊.还有.完整的工程在ftp上.你下载下来运行下就知道了.工程里有完整的代码.
      

  6.   

    http://www.junanwww.com/news/list.asp?articleid=778