本文整理汇总了C#中System.Net.Sockets.StateObject类的典型用法代码示例。如果您正苦于以下问题:C# StateObject类的具体用法?C# StateObject怎么用?C# StateObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StateObject类属于System.Net.Sockets命名空间,在下文中一共展示了StateObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
public static void Start()
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
listener.Bind(new IPEndPoint(IPAddress.Any, PORT));
Console.WriteLine("Waiting for a connection..." + GetLocalIpAddress());
while (true) {
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
StateObject state = new StateObject();
state.WorkSocket = listener;
listener.ReceiveFrom(state.Buffer, ref remoteEndPoint);
var rawMessage = Encoding.UTF8.GetString(state.Buffer);
var messages = rawMessage.Split(';');
if (messages.Length > 1) {
var command = messages[0];
var deviceName = messages[1];
Console.WriteLine("Command is received from Device Name +"+deviceName+"+");
string[] portno = remoteEndPoint.ToString().Split(':');
Send(portno[1],remoteEndPoint);
}
}
}
示例2: 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;
}
示例3: SyncSocClient
public SyncSocClient(string _ipAddress, int port, int timeout)
{
try
{
IPAddress ipAddress = System.Net.IPAddress.Parse(_ipAddress);
mPort = port;
remoteEP = new IPEndPoint(ipAddress, mPort);
mSender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
if (timeout > 0)
{
mSender.ReceiveTimeout = timeout;
mSender.SendTimeout = timeout;
}
//mSender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeout);
// The socket will linger for 10 seconds after Socket.Close is called.
LingerOption lingerOption = new LingerOption(true, 10);
mSender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
stateObj = new StateObject(mSender);
}
catch (Exception e)
{
SetErrorMessage(e, string.Format("소켓생성Error ip[{0}]/port[{1}]/timeout[{2}]]",_ipAddress,port,timeout));
Logger.error(e.ToString());
}
}
示例4: Connect
private bool Connect(StateObject stateObject) {
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(hostIp), hostPort);
stateObject.workSocket = client;
int retry = retryTime;
while (true) {
try {
// Connect to the remote endpoint.
client.Connect(ipEndPoint);
break;
} catch (Exception be) {
retry--;
Logging.LogError(be.Message);
Logging.LogError("Retry time: " + retry);
}
if (retry <= 0) {
Logging.LogError("Connecntion Error.");
return false;
}
}
client.SendTimeout = timeout;
client.ReceiveTimeout = timeout;
return true;
}
示例5: start
public Object start(TransferObject to) {
Object resultObject = null;
String calleeClass = to.getCalleeClass();
String calleeMethod = to.getCalleeMethod();
StateObject stateObject = new StateObject();
stateObject.transferObject = to;
try {
if (Connect(stateObject)) {
Send(stateObject);
resultObject = Receive(stateObject);
}
} catch (Exception e) {
Logging.LogError("Callee Class and Method: [" + calleeClass + "." + calleeMethod + "]");
Logging.LogError(e.ToString());
} finally {
Close(stateObject);
}
return resultObject;
}
示例6: Connection
public Connection(Server server, Socket socket)
{
m_Server = server;
m_Socket = socket;
var state = new StateObject { Socket = m_Socket };
m_Socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, SocketFlags.None, ReceiveCallback, state);
}
示例7: ReadMessage
public string ReadMessage(Socket client)
{
StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
readDone.WaitOne();
readDone.Reset();
return state.sb.ToString();
}
示例8: AcceptCallback
public void AcceptCallback(IAsyncResult ar)
{
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
AskForReceive(handler, state);
}
示例9: BeginConnect
IAsyncResult BeginConnect(AsyncCallback requestCallback, object userToken)
{
var stateObject = new StateObject()
{
AsyncState = userToken,
Callback = requestCallback,
IsCompleted = false
};
this.ConnectAsync(stateObject);
return stateObject;
}
示例10: StartReceiving
public void StartReceiving()
{
if (OnNotifyMulticastSocketListener == null)
throw new ApplicationException("No socket listener has been specified at OnNotifyMulticastSocketListener.");
// Create the state object.
StateObject state = new StateObject();
state.WorkSocket = udpSocket;
//get in waiting mode for data - always (this doesn't halt code execution)
Recieve(state);
}
示例11: DoWork
// Method passed to ThreadStart.
//
// This is the thread's starting point. It starts the process on the server machine, creates three ``state objects'', which will
// help us handle the redirections asynchronously. We associate the BeginRead methods of the standard output and error streams
// with our call-back (LocalReadCallback), to start processing the output of the command. We also associate the BeginRead() method
// the socket to our socket read call-back (ReadCallback), so we can process the data received through the socket.
public void DoWork()
{
Process process = Process.Start(_processStartInfo);
LocalStateObject outputLocalStateObject = new LocalStateObject(_socket, process, process.StandardOutput.BaseStream);
process.StandardOutput.BaseStream.BeginRead(outputLocalStateObject.Buffer, 0, LocalStateObject.BUFFER_SIZE, new AsyncCallback(LocalReadCallback), outputLocalStateObject);
LocalStateObject errorLocalStateObject = new LocalStateObject(_socket, process, process.StandardError.BaseStream);
process.StandardError.BaseStream.BeginRead(errorLocalStateObject.Buffer, 0, LocalStateObject.BUFFER_SIZE, new AsyncCallback(LocalReadCallback), errorLocalStateObject);
StateObject stateObject = new StateObject(_socket, process);
_socket.BeginReceive(stateObject.Buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), stateObject);
process.WaitForExit();
_socket.Close();
}
示例12: AcceptCallback
public static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
示例13: ConnectAsync
void ConnectAsync(StateObject stateObject)
{
var e = new SocketAsyncEventArgs();
e.UserToken = stateObject;
e.RemoteEndPoint = _endpoint;
e.Completed += OnConnectedAsync;
try
{
this.Client.ConnectAsync(e);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " >" + ex.StackTrace);
}
}
示例14: AcceptCallback
public void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, ReadCallbackFileRecive, state);
flag = 0;
}
示例15: AcceptCallback
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}