public static void ListenConnection() { Socket ConnectionSocket = null; while (true) { try { ConnectionSocket = ServerSocket.Accept(); } catch (Exception ex) { Console.WriteLine("监听套接字异常{0}", ex.Message); break; } //获取客户端端口号和IP IPAddress ClientIP = (ConnectionSocket.RemoteEndPoint as IPEndPoint).Address; int ClientPort = (ConnectionSocket.RemoteEndPoint as IPEndPoint).Port; string SendMessage = "连接服务器成功\r\n" + "本地IP:" + ClientIP + ",本地端口:" + ClientPort.ToString(); //Console.WriteLine(SendMessage); ConnectionSocket.Send(Encoding.Unicode.GetBytes(SendMessage)); string remotePoint = ConnectionSocket.RemoteEndPoint.ToString(); Console.WriteLine("成功与客户端{0}建立连接!\t\n", remotePoint); ClientInformation.Add(remotePoint, ConnectionSocket); Thread thread = new Thread(ReceiveMessage); thread.IsBackground = true; thread.Start(ConnectionSocket); } }
static void Main(string[] args) { ServerSocket.Bind(6556); ServerSocket.Listen(500); ServerSocket.Accept(); while (true) { Console.ReadLine(); } }
static void Main(string[] args) { ServerSocket server = new ServerSocket(); server.Bind(1029); server.Listen(5); server.Accept(); while (true) { Console.ReadKey(); } }
static void Main(string[] args) { ServerSocket server = new ServerSocket(); server.Bind("127.0.0.1", 1043); server.Listen(1); server.Accept(); while (server.IsConnected) { server.Receive(); } }
//Metoda koja binda socket i sluša konekcije private void btnSlusajKonekcije_Click(object sender, EventArgs e) { try { //Server socket prvo bindamo na port ServerSocket.Bind(defaultPort); //Slušamo ulazne konekcije ServerSocket.Listen(500); //Prihvaćamo konekcije ServerSocket.Accept(); lblStatusLabel.Text = "Slušam konekcije na portu " + defaultPort; btnRefreshClientList_Click(); } catch (Exception ex) { MessageBox.Show("Trenutno nije moguće slušati na konekcije!", "Greška", MessageBoxButtons.OK, MessageBoxIcon.Error); } }