用你所有知识采取Asp.net网页方式通过Socket的TCP连接到服务器192.168.5.3,端口88,然后发送“你好”到服务器,根据返回内容继续操作
测试要求:
从发送“你好”开始,总共需要发送三条信息,必须只能通过一次Socket连接完成。页面必须有可以任意输入内容的编辑框和发送按钮,三条返回的信息必须同时在同一个页面同一时间显示出来.(求帮我补全)using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Sockets;
public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //发送数据
        SocketSend();
    }
Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private void SocketSend()
    {
        //创建发送数据的Socket
        
        //设置发送数据的地址
       IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.5.2"), 88);
        string sendStr = "你好";
        byte[] sendBytes = System.Text.Encoding.Default.GetBytes(sendStr);
        //链接目的地
        //sendsocket.Connect(endPoint);
        //发送数据
        //sendsocket.Send(sendBytes);
        //关闭发送数据的Socket
        
        //sendsocket.Shutdown(SocketShutdown.Send);
        //sendsocket.Close();
//创建socket链接
        Response.Write("连接");
        sendsocket.BeginConnect(endPoint, new AsyncCallback(ConnectCallback), sendsocket);
    }    private void ConnectCallback(IAsyncResult ar)
{
        
try
        {
            
            // Retrieve the socket from the state object.     
            //Socket client = (Socket)ar.AsyncState;
            // Complete the connection.     
            //client.EndConnect(ar);
            //Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
            // Signal that the connection has been made.                 Response.Write("连接成功");
            string sendStr = "你好";
            byte[] sendBytes = System.Text.Encoding.Default.GetBytes(sendStr);
            
            //绑定发送消息
            sendsocket.BeginSend(sendBytes, 0, sendBytes.Length, 0, new AsyncCallback(SendCallback), sendsocket);            byte[] buffer = new byte[256];//创建字节 创建接收回调
            sendsocket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
        }
        catch (Exception e)
        {
           // Console.WriteLine(e.ToString());
            
        }
} private void SendCallback(IAsyncResult ar)
    {
       try
       {
            Socket handler = (Socket)ar.AsyncState;
           // handler.EndSend(ar);
            
       }
       catch (SocketException ex)
       {    }
    } private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                int REnd = sendsocket.EndReceive(ar);
                if (REnd > 0)
                {
                    byte[] data = new byte[REnd];
                    //在此次可以对data进行按需处理                    Response.Write("收到服务器信息");
                    Response.Write(data);
//string str = System.Text.Encoding.Default.GetString ( data );
//Response.write(str);

sendsocket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveCallback), sendsocket);
                }
                else
                {
                    dispose();
                }
            }
            catch (SocketException ex)
            { }
        }        private void dispose()
        {
            try
            {
               sendsocket.Shutdown(SocketShutdown.Both);
               sendsocket.Close();
            }
            catch (Exception ex)
            { }
        }
        public byte[] buffer { get; set; }
}

解决方案 »

  1.   

    如果这题目不是为了考察你对keep-alive或者WebSocket的认知,那么我觉得就有点扯了,为了要实现这功能你得在不同的http请求间维持住同一个socket才能实现所谓的“必须只能通过一次Socket连接”
      

  2.   

    因为我是新手,所以他说这个作为测试,叫我去看.net socket.
    求大神帮我
      

  3.   

    首先流程不对,
    Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    要写在 Gloax文件里面,否则每次页面加载都会连接一次
    而且要是静态的,页面用ajax调用
      

  4.   

    感觉你写的有问题啊,
    没有判断有没有连接就直接BeginReceive了
    //设置监听
    TcpListener listener;
     IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.5.2"), 88);
    listener = new TcpListener(endPoint);
    listener.Start(100);
    //接受连接请求的异步调用 
    //即有用户连接进来了就回调AccepetCallBack
     AsyncCallback callback = new AsyncCallback(AccepetCallBack);
     listener.BeginAcceptSocket(callback,listener);然后在AccepetCallBack里面
     AsyncCallback callback = new AsyncCallback(ReceiveCallback);
    再去   BeginReceive
      

  5.   

    你知道http是短链接 tcp/ip是长连接吗?你知道 page页面的生命周期吗?你知道 什么是websocket吗??好像都不知道...