Varför fungerar inte denna host socket bara en undran varför har du metoderna som static?C#-Socket problem
<code>C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketListener
{
// Data buffer for incoming data.
//byte[] msg = new Byte[1024];
public static void StartListening()
{
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
//IPEndPoint remoteEP = new IPEndPoint(Dns.Resolve("localhost").AddressList[0],11000);
IPHostEntry ipHostInfo = Dns.Resolve("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,20000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(remoteEP);
listener.Listen(10);
// Start listening for connections.
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket sender = listener.Accept();
// 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);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
public static int Main(String[] args)
{
StartListening();
return 0;
}
}</code>Sv: C#-Socket problem
Vad är det som inte fungerar? Kan du inte koppla upp dig eller vad?
Vad får du för fel i så fall?