当前位置: 首页>>代码示例>>C#>>正文


C# Socket.BeginReceive方法代码示例

本文整理汇总了C#中Socket.BeginReceive方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceive方法的具体用法?C# Socket.BeginReceive怎么用?C# Socket.BeginReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Socket的用法示例。


在下文中一共展示了Socket.BeginReceive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Client

    public Client(Socket accepted)
    {
        sck = accepted;
        ID = Guid.NewGuid().ToString();

        EndPoint = (IPEndPoint)sck.RemoteEndPoint;
        sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);

        confirmConnect();
    }
开发者ID:ferdbold,项目名称:les-jardins-de-la-tendresse-server,代码行数:10,代码来源:Client.cs

示例2: prepareForReceive

 void prepareForReceive(Socket socket, Action<string> onReceive)
 {
     var buffer = new byte[socket.ReceiveBufferSize];
     socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
         ar => {
             var client = (Socket)ar.AsyncState;
             var bytesReceived = client.EndReceive(ar);
             var trimmedBuffer = new byte[bytesReceived];
             Array.Copy(buffer, trimmedBuffer, bytesReceived);
             onReceive(Encoding.UTF8.GetString(trimmedBuffer));
         }, socket);
 }
开发者ID:sschmid,项目名称:NLog,代码行数:12,代码来源:describe_TcpServerSocket.cs

示例3: Receive

 private void Receive(Socket client)
 {
     try
     {
         StateObject state = new StateObject();
         state.workSocket = client;
         client.BeginReceive(state.buffer, 0, 1024, 0, new AsyncCallback(ReceiveCallback), state);
     }
     catch (Exception e)
     {
         Debug.LogException(e);
     }
 }
开发者ID:lwtbn1,项目名称:WWL,代码行数:13,代码来源:NetWorkManager.cs

示例4: Receive

    private static void Receive(Socket client)
    {
        try {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }
开发者ID:ambasta,项目名称:doe,代码行数:14,代码来源:Program.cs

示例5: AcceptCallback

    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        connectSocket = (Socket) ar.AsyncState;
        connectSocket = connectSocket.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = connectSocket;
        connectSocket.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
        Send(connectSocket, "Hello!");
        Debug.Log("[ROTATION_SERVER] Sent INIT sequence to client.");
    }
开发者ID:stefanseibert,项目名称:padawan101,代码行数:16,代码来源:RotationServer.cs

示例6: NetIO

 private NetIO()
 {
     try
     {
         //创建客户端连接
         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         //连接到服务器
         socket.Connect(ip, port);
         //开启异步接收,消息到达会直接写入缓冲区
         socket.BeginReceive(readBuff, 0, 1024, SocketFlags.None, ReceiveCallBack, readBuff);
     }
     catch (Exception e)
     {
         Debug.Log(e.Message);
     }
 }
开发者ID:dafei2015,项目名称:LOLClient,代码行数:16,代码来源:NetIO.cs

示例7: Connected

    private void Connected(IAsyncResult iar)
    {
        client = (Socket)iar.AsyncState;
        try
        {
            client.EndConnect(iar);

            sendAuthLogonChallenge();

            client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
        }
        catch (SocketException)
        {
            Console.WriteLine("Connection failed");
            // Error connecting
        }
    }
开发者ID:vipuldivyanshu92,项目名称:Projects,代码行数:17,代码来源:RealmClient.cs

示例8: Receive

    private static void Receive(Socket client)
    {
        try
        {
            StateObject state = new StateObject();
            state.workSocket = client;

            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception ex)
        {
            if (ex.Message == "No connection could be made because the target machine actively refused it")
                Console.WriteLine("File Sending fail. Because server not running?");
            else
                Console.WriteLine("File Sending fail. " + ex.Message);
        }
    }
开发者ID:sevenlabs,项目名称:Labs_AsyncSend,代码行数:18,代码来源:Program.cs

示例9: connectServer

	public void connectServer(){
		try{
			/*
			GameWorld gw= GameWorld.getInstance();
			string serverid ="server1";
			LitJson.JsonData server_info =gw.getServerInfo(serverid);
            Debug.Log(server_info["host"]+"================================"+server_info["port"]);
            int port = int.Parse(server_info["port"].ToString());
			string host=(server_info["host"]).ToString();
			*/
			string host = "127.0.0.1";
			int port = 9301;
			IPAddress ipad=IPAddress.Parse(host);
			IPEndPoint ipend=new IPEndPoint(ipad,port);

			//create connection
			sc= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			Debug.Log("begin connect server "+host+":"+port);

			sc.Connect(ipend);

			//send msg to server
			string sendmsg="hello conenct";
			byte[] bs=Encoding.UTF8.GetBytes(sendmsg);

			sc.Send(bs,bs.Length,0);

			Debug.Log("------------CONNECT over-------------------");
            isConnected = true;
            if (sc.Connected) {
                Debug.Log("begin recive msg from server=================================");
                sc.BeginReceive(receiveBuff,0,receiveBuff.Length,0,new AsyncCallback(reciveMessageCallBack),null);
            }
            
		}
		catch(ArgumentNullException e){
			//Debug.Log ("ArgumentNullException : "+e.ToString());
            throw e;
		}
		catch(SocketException e){
			//Debug.Log ("SocketException : "+e.ToString());
            throw e;
		}
	}
开发者ID:ZhHong,项目名称:u3d_client,代码行数:44,代码来源:LFL_NetWork.cs

示例10: Read

	private void Read(Socket client, bool beginNewPhase)
	{
		try {
			// Begin receiving the data from the remote device.
			if (beginNewPhase)
			{
				mState.bytesRead = 0;
				client.BeginReceive( mState.readBuffer, 0, JStateObject.BufferSize,
				                    0,
				                    new AsyncCallback(ReadCallback), mState);
			}
			else
			{
				client.BeginReceive( mState.readBuffer, 0, JStateObject.BufferSize - mState.bytesRead,
				                    0,
				                    new AsyncCallback(ReadCallback), mState);
			}
		} catch (Exception e) {
			//Console.WriteLine(e.ToString());
			Debug.Log("C: Receive " + e.ToString());
		}
	}
开发者ID:serenitii,项目名称:jeisimple,代码行数:22,代码来源:AsyncClient.cs

示例11: JoinServer

 public void JoinServer()
 {
     YourSocket = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream,
                             ProtocolType.IP);
     try {
         YourSocket.Connect(IpText.text,
                            NetworkConnector.Port);
         this.LocalPlayer[0] = false;
         this.LocalPlayer[1] = true;
         this.IsConnected = true;
         YourSocket.BeginReceive(this.SocketBuffer,
                                 0,
                                 this.SocketBuffer.Length,
                                 SocketFlags.None,
                                 new System.AsyncCallback(this.OnSocketRead),
                                 null);
     }
     catch{
         Debug.Log("Join Failed!");
     }
 }
开发者ID:lasagnaphil,项目名称:Beat-It,代码行数:22,代码来源:NetworkConnector.cs

示例12: Connect

	public void Connect(string name) {
		queue = new Queue<string>();
		
		IPAddress address = Dns.Resolve(host).AddressList[0];
		IPEndPoint remote = new IPEndPoint(address, port);
		
		pull = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		pull.NoDelay = true;
		pull.Connect(remote);
		
		String data = "GET /pull?name=" + name + " HTTP/1.1\r\n"
			+ "Host: " + host + "\r\n"
				+ "Head: less\r\n\r\n"; // enables TCP no delay
		
		pull.Send(Encoding.ASCII.GetBytes(data));
		
		State state = new State();
		state.socket = pull;
		
		pull.BeginReceive(state.data, 0, State.size, 0, new AsyncCallback(Callback), state);
		
		connected = true;
	}
开发者ID:siloenaps,项目名称:Comet-w.-Rupy-and-Unity,代码行数:23,代码来源:Stream.cs

示例13: Receive

 //Receives the message and uses ReceiveCallback
 private void Receive(Socket client)
 {
     try
     {
         StateObject state = new StateObject(); // Create the state object.
         state.workSocket = client;
         client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
         log.Debug(ex.ToString());
     }
 }
开发者ID:samardzicnenad,项目名称:CooperUnion,代码行数:15,代码来源:AsynchronousClient.cs

示例14: OnSocketConnect

 private void OnSocketConnect(System.IAsyncResult ar)
 {
     Debug.Log("Wow! Such Connection!");
     YourSocket = MySocket.EndAccept(ar);
     YourSocket.BeginReceive(this.SocketBuffer,
                             0,
                             this.SocketBuffer.Length,
                             SocketFlags.None,
                             this.OnSocketRead,
                             null);
     this.LocalPlayer[0] = true;
     this.LocalPlayer[1] = false;
     this.IsConnected = true;
 }
开发者ID:lasagnaphil,项目名称:Beat-It,代码行数:14,代码来源:NetworkConnector.cs

示例15: Receive

    private static void Receive(Socket client)
    {
        try
        {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception e)
        {
            distributor.closeConnection();
        }
    }
开发者ID:stefanseibert,项目名称:padawan101,代码行数:16,代码来源:RotationDistributor.cs


注:本文中的Socket.BeginReceive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。