本文整理汇总了C#中System.Net.Sockets.Socket.BeginReceive方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceive方法的具体用法?C# Socket.BeginReceive怎么用?C# Socket.BeginReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.BeginReceive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FileReceive
public static FileReceiveStatus FileReceive(Socket socketFd)
{
//receive File size
var sizeBytes = new byte[sizeof(int)];
try
{
socketFd.Receive(sizeBytes, sizeBytes.Length, 0);
}
catch (Exception exc)
{
MessageBox.Show("Exception:\t\n" + exc.Message);
var window = (ProjectZip)Application.OpenForms[0];
window.SetControls(true);
}
var size = BitConverter.ToInt32(sizeBytes, 0);
//receive File
var fas = new FileAndSize
{
SizeRemaining = size,
SocketFd = socketFd
};
socketFd.BeginReceive(fas.Buffer, 0, FileAndSize.BUF_SIZE, 0, FileReceiveCallback, fas);
return FileReceiveStatus.Ok;
}
示例2: Connect
public void Connect(int aPort, IPAddress Ip, string userName, string password, string nick)
{
int attempts = 5;
_nick = nick;
// TODO: validar esse objeto, pois após a desconexão ocorre o dispose() dele
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
while (!_clientSocket.Connected && attempts > 0)
{
try
{
OnRaiseMessage(new MessageEventArgs(String.Format("Attempt {0} to connect at server {1}:{2}", attempts, Ip.MapToIPv4().Address, aPort)));
_clientSocket.Connect(Ip ,aPort);
OnRaiseMessage(new MessageEventArgs("Connected!"));
//solicita autenticação
Authenticate(userName, password);
//inicia recebimento assíncrono de mensagens
_clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), _clientSocket);
}
catch (Exception ex)
{
OnRaiseMessage(new MessageEventArgs("Connection failed."));
Thread.Sleep(1000);
attempts--;
}
}
}
示例3: Start
public bool Start()
{
if (_started) return true;
_socket = SocketUtils.OpenSocketConnection("api.triggrapp.com", 9090);
if (_socket == null)
{
_started = false;
return false;
}
_started = true;
StateObject state = new StateObject();
state.workSocket = _socket;
_socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
SendHandshake();
StartHeartbeat();
return true;
}
示例4: WebsocketClient
public WebsocketClient (Socket s)
{
this.bufferStrBuild = new StringBuilder();
clientSocket = s;
s.BeginReceive( this.buffer, 0, BUFFER_SIZE, 0,
new AsyncCallback(this.ReadCallback), this);
}
示例5: Client
public Client(Socket acceptedSck)
{
sck = acceptedSck;
ID = Guid.NewGuid().ToString();
sck.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
}
示例6: WaitForData
private void WaitForData(Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket(BufferLength);
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(
theSocPkt.dataBuffer,
0,
theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch (SocketException sex)
{
Debug.Fail(sex.ToString(), "WaitForData: Socket failed");
}
}
示例7: Start
public void Start(IPEndPoint dest)
{
this.destination = dest;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
socket.Connect(dest);
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveTcpData, null);
DataPacket p = DataPacket.Create(16);
p.Command = (ushort)CommandID.CA_PROTO_VERSION;
p.DataType = 1;
p.DataCount = (uint)EpicsConstants.CA_MINOR_PROTOCOL_REVISION;
p.Parameter1 = 0;
p.Parameter2 = 0;
Send(p);
p = DataPacket.Create(16 + this.Client.Configuration.Hostname.Length + TypeHandling.Padding(this.Client.Configuration.Hostname.Length));
p.Command = (ushort)CommandID.CA_PROTO_HOST_NAME;
p.DataCount = 0;
p.DataType = 0;
p.Parameter1 = 0;
p.Parameter2 = 0;
p.SetDataAsString(this.Client.Configuration.Hostname);
Send(p);
p = DataPacket.Create(16 + this.Client.Configuration.Username.Length + TypeHandling.Padding(this.Client.Configuration.Username.Length));
p.Command = (ushort)CommandID.CA_PROTO_CLIENT_NAME;
p.DataCount = 0;
p.DataType = 0;
p.Parameter1 = 0;
p.Parameter2 = 0;
p.SetDataAsString(this.Client.Configuration.Username);
Send(p);
}
示例8: XmppServerConnection
public XmppServerConnection(Form1 a, Socket sock)
: this()
{
m_Sock = sock;
frm = a;
m_Sock.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);
}
示例9: sendPackage
public void sendPackage(byte[] _data, Socket socket)
{
// DataPackage _data = new DataPackage("First package", 9999, true);
// DroidMessage _data = new DroidMessage("Hello socket server");
try
{
if (isOnline == true)
{
byte[] data = _data;
//_clientSocket.Send(buffer);
// socket.Send(_data);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
byte[] _buf = _buffersList.SingleOrDefault(k => k.Key == socket).Value;
// socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
socket.BeginReceive(_buf, 0, _buf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
}
catch (Exception ex)
{
if (OnServerError != null)
{
OnServerError(null, new NotyArgs(ex.Message));
}
}
}
示例10: JoinRoom
public void JoinRoom(string userName, string targetIP)
{
try
{
strName = userName;
ipInput = targetIP;
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(ipInput);
//Server is listening on port 1000
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);
//Connect to the server
clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
Debug.WriteLine("Username: " + strName);
byteData = new byte[1024];
//Start listening to the data asynchronously
clientSocket.BeginReceive(byteData,
0,
byteData.Length,
SocketFlags.None,
new AsyncCallback(OnReceive),
null);
}
catch (Exception exc) { Debug.WriteLine(exc.ToString()); }
}
示例11: ReadToStreamAsyn
/// <summary>
/// Receive data, remember to release the Stream
/// </summary>
/// <param name="socket">Socket</param>
/// <returns>Stream</returns>
public static MemoryStream ReadToStreamAsyn(Socket socket)
{
DataFlow flow = new DataFlow(socket);
IAsyncResult asyncResult = socket.BeginReceive(flow.Buf, 0, flow.Buf.Length, SocketFlags.None, new AsyncCallback(AsyncRead), flow);
flow.AutoEvent.WaitOne();
return flow.MStream;
}
示例12: Server
public Server(Socket _Sock)
{
Sock = _Sock;
Sock.BeginReceive(Buffer, 0, 8192, SocketFlags.None, new AsyncCallback(WaitForData), Sock);
ScSec = new PacketSecurity(true);
CsSec = new PacketSecurity(false);
}
示例13: Program
public Program(string outputfilename)
{
try
{
_output = File.Open(outputfilename, FileMode.Create);
System.Console.WriteLine("Capturing output to: " + _output.Name);
}
catch (IOException e)
{
Abort(e);
}
try
{
IPHostEntry e = Dns.GetHostEntry(HOST);
_incoming = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Console.WriteLine("Resolved {0} to {1}.", HOST, e.AddressList[0].ToString());
_incoming.Connect(e.AddressList, PORT);
_incoming.ReceiveTimeout = 10000; // 10 seconds because we may not always be receiving data
_incoming.BeginReceive(_blob, 0, BLOB_SIZE, SocketFlags.None, new AsyncCallback(OnReceiveData), null);
}
catch (SocketException e)
{
Abort(e);
}
}
示例14: checkBox1_CheckedChanged
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if ((sender as CheckBox).Checked)
{
(sender as CheckBox).Text = "&Stop me";
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
try
{
socket.Bind(new IPEndPoint(IPAddress.Parse(comboBox1.SelectedItem.ToString()), 0));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
byte[] byInc = new byte[] { 1, 0, 0, 0 };
byte[] byOut = new byte[4];
buffer = new byte[4096];
socket.IOControl(IOControlCode.ReceiveAll, byInc, byOut);
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnReceive, null);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
else
{
socket.Close();
(sender as CheckBox).Text = "&Start me";
}
}
示例15: Main
static void Main(string[] args)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("localhost", 4530);
//实现接受消息的方法
var buffer = new byte[1024];//设置一个缓冲区,用来保存数据
//socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
//{
// var length = socket.EndReceive(ar);
// var message = Encoding.Unicode.GetString(buffer, 0, length);
// //显示读出来的消息
// Console.WriteLine(message);
//}), null);
Console.WriteLine("Client connec to the server");
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
//接受用户输入,将消息发送给服务器端
while (true)
{
var message = "message from client: " + Console.ReadLine();
var outputBuffer = Encoding.Unicode.GetBytes(message);
socket.BeginSend(outputBuffer, 0, outputBuffer.Length, SocketFlags.None, null, null);
}
Console.Read();
}