本文整理汇总了C#中System.Net.Sockets.Socket.BeginSend方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginSend方法的具体用法?C# Socket.BeginSend怎么用?C# Socket.BeginSend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.BeginSend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginSend
public void BeginSend(Socket objSocket, byte[] data)
{
if (objSocket != null && objSocket.Connected)
{
objSocket.BeginSend(data, 0, data.Length, SocketFlags.None, this.SendAsynCallBack, objSocket);
}
}
示例2: Echo
private static IEnumerator<IAsyncCall> Echo(Socket client)
{
byte[] buffer = new byte[1024];
AsyncCall<int> call = new AsyncCall<int>();
while (true)
{
yield return call
.WaitOn(cb => client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, cb, null))
& client.EndReceive;
int bytes = call.Result;
if (bytes > 0)
{
Console.WriteLine("read {0} bytes from {1}", bytes, client.RemoteEndPoint);
yield return call
.WaitOn(cb => client.BeginSend(buffer, 0, bytes, SocketFlags.None, cb, null))
& client.EndReceive;
Console.WriteLine("sent {0} bytes to {1}", bytes, client.RemoteEndPoint);
}
else
{
break;
}
}
Console.WriteLine("closing client socket {0}", client.RemoteEndPoint);
client.Close();
}
示例3: 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));
}
}
}
示例4: httpRequest
private async Task<String> httpRequest(Byte method, String headerAndBodyRaw, String link)
{
Socket youtubeSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Byte[] requbytes = new Byte[8192];
Int32 recvd = -1;
String request = "";
String headerAndBody = ((method == 1) ? "GET /" : "POST /") + link + headerAndBodyRaw.Substring(headerAndBodyRaw.IndexOf("HTTP/1.1") - 1);
try
{
await Task.Factory.FromAsync(youtubeSock.BeginConnect, youtubeSock.EndConnect, "www.youtube.com", 80, null);
if (youtubeSock.Connected)
{
var buffer = Encoding.ASCII.GetBytes(headerAndBodyRaw);
int bytessent = await Task.Factory.FromAsync<int>(youtubeSock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, youtubeSock), youtubeSock.EndSend);
if (await waitForDataToBecomeAvailable(2000, youtubeSock))
{
recvd = youtubeSock.Receive(requbytes);
request = Encoding.ASCII.GetString(requbytes).Substring(0, recvd);
}
youtubeSock.Dispose();
}
return request;
}
catch (Exception err)
{
LogError("\nhttpRequest() - Error: " + err.Message + "\n\n" + err.StackTrace + "\n" + headerAndBodyRaw);
}
return "";
}
示例5: SendMessage
public void SendMessage(Socket client, string message)
{
byte[] byteMessage = Encoding.ASCII.GetBytes(message);
client.BeginSend(byteMessage, 0, byteMessage.Length, SocketFlags.None, new AsyncCallback(SendCallBack), client);
sendDone.WaitOne();
sendDone.Reset();
}
示例6: 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();
}
示例7: OnReceiveData
private void OnReceiveData(Socket socket)
{
string strLogin = "succeed recived";
socket = this.socket;
byte[] data = Encoding.ASCII.GetBytes(strLogin);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, sendCallback, socket);
}
示例8: WriteTo
public void WriteTo(Socket socket)
{
var messageDataLengthBytes = BitConverter.GetBytes(messageData.Length);
var bytes = new byte[messageDataLengthBytes.Length + messageData.Length];
Buffer.BlockCopy(messageDataLengthBytes, 0, bytes, 0, messageDataLengthBytes.Length);
Buffer.BlockCopy(messageData, 0, bytes, messageDataLengthBytes.Length, messageData.Length);
socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, OnWrite, new State(socket));
}
示例9: BeginSend
private static void BeginSend(Socket socket, byte [] buffer, State state)
{
if (socket == null) throw new ArgumentNullException("socket");
if (buffer == null) throw new ArgumentNullException("buffer");
if (socket.Connected)
socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), state);
}
示例10: Send
public static void Send(Socket handler, String data, Boolean IsOver)
{
byte[] byteData = Encoding.UTF8.GetBytes(data);
StateObject state = new StateObject();
state.workSocket = handler;
state.IsOver = IsOver;
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), state);
}
示例11: SendMessage
public void SendMessage(Socket targetSocket, string messageContent, Action<string> messageSentCallBack)
{
byte[] message = BuildMessage(messageContent, Encoding.ASCII);
if(message.Length>0) {
var context = new SendContext() {
TargetSocket = targetSocket, MessageContent = messageContent, MessageSentCallback = messageSentCallBack
};
targetSocket.BeginSend(message, 0, message.Length, 0, new AsyncCallback(this.SendCallback), context);
}
}
示例12: SendReceive
private IEnumerator<IAsyncCall> SendReceive(string host, int port)
{
AsyncCall<IPHostEntry> call = new AsyncCall<IPHostEntry>();
yield return call
.WaitOn(cb => Dns.BeginGetHostEntry(host, cb, null)) & Dns.EndGetHostEntry;
if (!call.Succeeded)
{
Console.WriteLine(call.Exception.Message);
yield break;
}
IPAddress addr = call.Result.AddressList[0];
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
AsyncCall call2 = new AsyncCall();
yield return call2
.WaitOn(cb => socket.BeginConnect(new IPEndPoint(addr, port), cb, null)) & socket.EndConnect;
if (!call2.Succeeded)
{
Console.WriteLine(call2.Exception.Message);
yield break;
}
byte[] sendBuffer = System.Text.Encoding.ASCII.GetBytes("hello world");
byte[] receiveBuffer = new byte[1024];
AsyncCall<int> call3 = new AsyncCall<int>();
for (int i = 0; i < 128; ++i)
{
yield return call3
.WaitOn(cb => socket.BeginSend(
sendBuffer, 0, sendBuffer.Length, SocketFlags.None, cb, null)) & socket.EndSend;
int bytesSent = call3.Result;
Console.WriteLine("{0} sent {1} bytes", socket.LocalEndPoint, bytesSent);
if (bytesSent > 0)
{
yield return call3
.WaitOn(cb => socket.BeginReceive(
receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, cb, null)) & socket.EndReceive;
Console.WriteLine("{0} read {1} bytes", socket.LocalEndPoint, call3.Result);
}
}
Console.WriteLine("closing socket {0}", socket.LocalEndPoint);
socket.Close();
}
示例13: send_packet
public void send_packet(byte[] packet_data, int send_size,Socket socket)
{
try
{
socket.BeginSend(packet_data, 0, send_size, SocketFlags.None, new AsyncCallback(packet_send), socket_);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
示例14: SendMessage
public void SendMessage(Socket targetSocket, byte[] message, Action<SendResult> messageSentCallback)
{
var wrappedMessage = SocketUtils.BuildMessage(message);
if (wrappedMessage.Length > 0)
{
targetSocket.BeginSend(
wrappedMessage,
0,
wrappedMessage.Length,
SocketFlags.None,
new AsyncCallback(SendCallback),
new SendContext(targetSocket, wrappedMessage, messageSentCallback));
}
}
示例15: SendFile
public static void SendFile(string path, string parentDirectory, Socket socketFd, ManualResetEvent handle)
{
//send File name
var filePath = Path.Combine(parentDirectory, Path.GetFileName(path));
filePath = filePath.Replace('\\', '/');
var pathBytes = Encoding.ASCII.GetBytes(filePath);
var pathSizeBytes = BitConverter.GetBytes(pathBytes.Length);
try
{
socketFd.Send(pathSizeBytes, pathSizeBytes.Length, 0);
socketFd.Send(pathBytes, pathBytes.Length, 0);
}
catch (Exception exc)
{
MessageBox.Show("Exception:\t\n" + exc.Message);
var window = (ProjectZip)Application.OpenForms[0];
window.SetControls(true);
}
//send File size
var file = File.ReadAllBytes(path);
var fileSizeBytes = BitConverter.GetBytes(file.Length);
try
{
socketFd.Send(fileSizeBytes, fileSizeBytes.Length, 0);
}
catch (Exception exc)
{
MessageBox.Show("Exception:\t\n" + exc.Message);
var window = (ProjectZip)Application.OpenForms[0];
window.SetControls(true);
}
//send File
var fas = new FileAndSize
{
SocketFd = socketFd,
File = file,
FileSize = file.Length,
SizeRemaining = file.Length,
Handle = handle
};
socketFd.BeginSend(file, 0, (fas.SizeRemaining < FileAndSize.BUF_SIZE ? fas.SizeRemaining : FileAndSize.BUF_SIZE), 0, SendFileCallback, fas);
}