本文整理汇总了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();
}
示例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);
}
示例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);
}
}
示例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());
}
}
示例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.");
}
示例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);
}
}
示例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
}
}
示例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);
}
}
示例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;
}
}
示例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());
}
}
示例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!");
}
}
示例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;
}
示例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());
}
}
示例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;
}
示例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();
}
}