本文整理汇总了C#中Socket.Shutdown方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.Shutdown方法的具体用法?C# Socket.Shutdown怎么用?C# Socket.Shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.Shutdown方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AttemptConnection
public void AttemptConnection( string ipAddressString, string portString )
{
debugLog.ReceiveMessage ( "\tAttempting Connection to " + ipAddressString + " on " + portString );
connecting = true;
connectionType = ConnectionType.Connecting;
debugLog.ReceiveMessage ( "\tConnection Type Set to Connecting" );
try
{
IPAddress ipAddress = Dns.GetHostEntry ( ipAddressString ).AddressList[0];
IPEndPoint remoteEndPoint = new IPEndPoint ( ipAddress, Convert.ToInt32 ( portString ));
Socket client = new Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
client.BeginConnect ( remoteEndPoint, new AsyncCallback ( ConnectCallback ), client );
Send ( client, "This is a test<EOF>" );
Receive ( client );
UnityEngine.Debug.Log ( "Response received : " + response );
client.Shutdown ( SocketShutdown.Both );
client.Close ();
} catch ( Exception connectionError )
{
UnityEngine.Debug.LogError ( connectionError );
}
}
示例2: Main
static void Main(string[] args)
{
//设定服务器IP地址
IPAddress ip = IPAddress.Parse ("127.0.0.1");
Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try {
clientSocket.Connect (new IPEndPoint (ip, 8885)); //配置服务器IP与端口
Console.WriteLine ("连接服务器成功");
} catch {
Console.WriteLine ("连接服务器失败,请按回车键退出!");
return;
}
//通过clientSocket接收数据
int receiveLength = clientSocket.Receive (result);
Console.WriteLine ("接收服务器消息:{0}", Encoding.ASCII.GetString (result, 0, receiveLength));
//通过 clientSocket 发送数据
for (int i = 0; i < 10; i++) {
try {
Thread.Sleep (1000); //等待1秒钟
string sendMessage = "client send Message Hellp" + DateTime.Now;
clientSocket.Send (Encoding.ASCII.GetBytes (sendMessage));
Console.WriteLine ("向服务器发送消息:{0}" + sendMessage);
} catch {
clientSocket.Shutdown (SocketShutdown.Both);
clientSocket.Close ();
break;
}
}
Console.WriteLine ("发送完毕,按回车键退出");
Console.ReadLine ();
}
示例3: StartClient
public static void StartClient()
{
try
{
byte[] bytes = File.ReadAllBytes("c:\\file.bin");
string x = Convert.ToBase64String(bytes);
IPHostEntry ipHostInfo = Dns.Resolve(Environment.MachineName);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP, ConnectCallback, client);
ConnectDone.WaitOne();
Send(client, x + "<EOF>");
SendDone.WaitOne();
Receive(client);
ReceiveDone.WaitOne();
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
throw;
}
}
示例4: fUniversal
//Universal function
public string fUniversal(string sAction, string sUser = "", string sArgsList = "")
{
try
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Create TCP/IP socket.
connectDone.Reset();
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); // Connect to the remote endpoint.
connectDone.WaitOne();
if (!ReferenceEquals(myException, null)) //No connection present
throw (myException);
sendDone.Reset();
Send(client, sAction + "," + sUser + "," + sArgsList); // Send data to the remote device.
sendDone.WaitOne();
receiveDone.Reset();
Receive(client); // Receive the response from the remote device.
receiveDone.WaitOne();
client.Shutdown(SocketShutdown.Both); //End connection
client.Close();
return response;
}
catch (Exception ex)
{
log.Debug(ex.ToString());
throw ex;
}
}
示例5: MakeRequestInternal
private void MakeRequestInternal(string message)
{
// Data buffer for incoming data.
byte[] bytes = new byte[Transport.PacketSize];
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
IPAddress ipAddress = IpHelper.ServerIp;
IPEndPoint remoteEP = new IPEndPoint(ipAddress,port);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try
{
sender.Connect(remoteEP);
byte[] msg = Encoding.ASCII.GetBytes(message+Transport.EndFlag);
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
var data = Encoding.ASCII.GetString(bytes,0,bytesRec);
_clientProtocol.ProcessResponse(data);
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
_clientProtocol.NotifyError();
Debug.Log(ane.ToString());
}
catch (SocketException se)
{
_clientProtocol.NotifyError();
Debug.Log(se.ToString());
}
catch (Exception e)
{
_clientProtocol.NotifyError();
Debug.Log(e.ToString());
}
}
catch (Exception e)
{
_clientProtocol.NotifyError();
Debug.Log( e.ToString());
}
}
示例6: Main
public static void Main(string[] args)
{
if(args.Length!=1)
{
usage();
return;
}
try
{
string hostname=args[0];
IPHostEntry IPHost=Dns.Resolve(hostname);
Console.WriteLine(IPHost.HostName);
string []aliases=IPHost.Aliases;
IPAddress[] addr=IPHost.AddressList;
Console.WriteLine(addr[0]);
EndPoint cp=new IPEndPoint(addr[0],80);
Socket sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Connect(cp);
if(sock.Connected)
{
Console.WriteLine("OK");
}
Encoding ASCII=Encoding.ASCII;
string Get="GET/HTTP/1.1\r\nHost:"+hostname+"\r\nConnection:Close\r\n\r\n";
Byte[] ByteGet=ASCII.GetBytes(Get);
Byte[] RecvBytes=new Byte[256];
sock.Send(ByteGet,ByteGet.Length,0);
Int32 bytes=sock.Receive(RecvBytes,RecvBytes.Length,0);
Console.WriteLine(bytes);
String strRetPage=null;
strRetPage=strRetPage+ASCII.GetString(RecvBytes,0,bytes);
while(bytes>0)
{
bytes=sock.Receive(RecvBytes,RecvBytes.Length,0);
strRetPage=strRetPage+ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(strRetPage);
}
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例7: Main
static void Main ()
{
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect (IPAddress.Parse ("127.0.0.1"), 8081);
string request = "GET / HTTP/1.1\r\n" +
"Host: 127.0.0.1\r\n\r\n";
//request = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8001\r\n\r\n";
s.Send (Encoding.UTF8.GetBytes (request));
s.Shutdown (SocketShutdown.Both);
s.Close ();
}
示例8: StartClient
public static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
//string fileName = @"D:\OTC_Batch\itemCode_700_14112014059522584.xml<EOF>";
string fileName = @"D:\OTC_Batch\itemCode_700_14112014059522584.xml";
string text = fileName;//System.IO.File.ReadAllText(fileName);
text = text + "<EOF>";
Send(client, text);
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例9: StartClient
public static void StartClient()
{
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec));
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
} catch (Exception e) {
Console.WriteLine( e.ToString());
}
}
示例10: StartClient
private static void StartClient()
{
// Connect to a remote device.
Console.WriteLine("Enter your message to the server: ");
string message = Console.ReadLine();
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve("192.168.1.79");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, message+"<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
Console.ReadLine();
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
示例11: StartClient
private static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
//IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("129.241.187.44"), 34933);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Created a TCP/IP socket.");
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
Console.WriteLine("Connected to the remote endpoint.");
// Send test data to the remote device.
Send(client, "Connect to: 129.241.187.40:20004\0");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例12: Cliente
private static string Cliente(IPAddress servidor, int puerto)
{
try
{
string request = "<DIR>";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
// Crear socket ip, puerto
IPEndPoint ipe = new IPEndPoint(servidor, puerto);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipe);
if (s == null) return "Connexion falló!";
s.Send(bytesSent, bytesSent.Length, 0);
try
{
byte[] data = new byte[1024];
int receivedDataLength = s.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
Console.WriteLine(stringData);
s.Shutdown(SocketShutdown.Both);
s.Close();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
catch (Exception se)
{
Console.WriteLine("Error en conexión" + se.StackTrace);
}
return "";
}
示例13: GetData
static void GetData(string code, int length, Socket socket) {
if(running) {
if(!socketCode.ContainsKey(socket))
socketCode[socket] = "";
socketCode[socket] += code;
updatedSockets.Enqueue(socket);
} else {
socket.Shutdown(SocketShutdown.Both);
}
}
示例14: socket_close
public static void socket_close(Socket sock)
{
sock.Shutdown( SocketShutdown.Both ); // XXX perhaps should be separate
sock.Close();
}
示例15: Main
static void Main(string[] args)
{
// Receiving byte array
byte[] bytes = new byte[1024];
try
{
// Create one SocketPermission for socket access restrictions
SocketPermission permission = new SocketPermission(
NetworkAccess.Connect, // Connection permission
TransportType.Tcp, // Defines transport types
"", // Gets the IP addresses
SocketPermission.AllPorts // All ports
);
// Ensures the code to have permission to access a Socket
permission.Demand();
// Resolves a host name to an IPHostEntry instance
IPHostEntry ipHost = Dns.GetHostEntry("");
// Gets first IP address associated with a localhost
IPAddress ipAddr = ipHost.AddressList[0];
// Creates a network endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 6000);
// Create one Socket object to setup Tcp connection
Socket sender = new Socket(
ipAddr.AddressFamily,// Specifies the addressing scheme
SocketType.Stream, // The type of socket
ProtocolType.Tcp // Specifies the protocols
);
sender.NoDelay = true; // Using the Nagle algorithm
// Establishes a connection to a remote host
sender.Connect(ipEndPoint);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
// Sending message
//<Client Quit> is the sign for end of data
string theMessage = "Hello World!";
byte[] msg =
Encoding.Unicode.GetBytes(theMessage +"<Client Quit>");
// Sends data to a connected Socket.
int bytesSend = sender.Send(msg);
// Receives data from a bound Socket.
int bytesRec = sender.Receive(bytes);
// Converts byte array to string
theMessage = Encoding.Unicode.GetString(bytes, 0, bytesRec);
// Continues to read the data till data isn't available
while (sender.Available > 0)
{
bytesRec = sender.Receive(bytes);
theMessage += Encoding.Unicode.GetString(bytes, 0, bytesRec);
}
Console.WriteLine("The server reply: {0}", theMessage);
// Disables sends and receives on a Socket.
sender.Shutdown(SocketShutdown.Both);
//Closes the Socket connection and releases all resources
sender.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.ToString());
}
}