private void ServerBindButton_Click(object sender, EventArgs e) { IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint anEndPoint = new IPEndPoint(ipAddress, Port); ServerSocket = new ClientSocket("ServerSocket", MAX_LENGTH); ServerSocket.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSocket.socket.Blocking = checkBoxServerSocketBlocked.Checked; ServerSocket.socket.Bind(anEndPoint); ListBoxLog.Items.Add(string.Format("ServerSocket.Bind() (IsBound: {0})", ServerSocket.socket.IsBound)); ServerBindButton.Enabled = false; ListBoxLog.Items.Add("ServerSocket.Listen()"); ServerSocket.socket.Listen(10); ListBoxLog.Items.Add("ServerSocket.BeginAccept()"); ServerSocket.socket.BeginAccept(result => { Socket socket = (Socket)ServerSocket.socket.EndAccept(result); if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add("ServerSocket.EndAccept() (" + socket.RemoteEndPoint.ToString() + ")"); })); } else { ListBoxLog.Items.Add("ServerSocket.EndAccept() (" + socket.RemoteEndPoint.ToString() + ")"); } if (ServerSocket.Sockets == null) { ServerSocket.Sockets = new List <Socket>(); } ServerSocket.Sockets.Add(socket); if (listBoxClients.InvokeRequired) { listBoxClients.Invoke(new MethodInvoker(delegate { listBoxClients.Items.Add("Client# " + ServerSocket.Sockets.Count + " (" + socket.RemoteEndPoint.ToString() + ")"); listBoxClients.SelectedIndex = ServerSocket.Sockets.Count - 1; ButtonReceiveServer.Enabled = true; ButtonSendServer.Enabled = true; })); } else { listBoxClients.Items.Add("Client# " + ServerSocket.Sockets.Count + " (" + socket.RemoteEndPoint.ToString() + ")"); listBoxClients.SelectedIndex = ServerSocket.Sockets.Count - 1; ButtonReceiveServer.Enabled = true; ButtonSendServer.Enabled = true; } }, null); }
void ClientConnect(ClientSocket ClientSocket, IPEndPoint remoteEP) { ListBoxLog.Items.Add(string.Format("{0}.BeginConnect()", ClientSocket.Name)); ClientSocket.socket.BeginConnect(remoteEP, result => { ClientSocket.socket.EndConnect(result); if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0}.EndConnect()", ClientSocket.Name)); ListBoxLog.Items.Add(string.Format("{0}.Connect() (Connected: {1})", ClientSocket1.Name, ClientSocket1.socket.Connected)); })); } else { ListBoxLog.Items.Add(string.Format("{0}.EndConnect()", ClientSocket.Name)); ListBoxLog.Items.Add(string.Format("{0}.Connect() (Connected: {1})", ClientSocket1.Name, ClientSocket1.socket.Connected)); } }, null); }
void SendCallback(IAsyncResult ar) { ClientSocket Socket = (ClientSocket)ar.AsyncState; int SelectedIndex = -1; if (listBoxClients.InvokeRequired) { listBoxClients.Invoke(new MethodInvoker(delegate { SelectedIndex = listBoxClients.SelectedIndex; })); } else { SelectedIndex = listBoxClients.SelectedIndex; } Socket s; if ((s = Socket.Sockets == null ? Socket.socket : (SelectedIndex >= 0 && SelectedIndex < Socket.Sockets.Count ? Socket.Sockets[SelectedIndex] : null)) == null) { return; } int bytesSent = s.EndSend(ar); if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0}.EndSend() (Sent {1} bytes)", Socket.Name, bytesSent)); })); } else { ListBoxLog.Items.Add(string.Format("{0}.EndSend() (Sent {1} bytes)", Socket.Name, bytesSent)); } }
void ReceiveCallback(IAsyncResult ar) { ClientSocket Socket = (ClientSocket)ar.AsyncState; int SelectedIndex = -1; if (listBoxClients.InvokeRequired) { listBoxClients.Invoke(new MethodInvoker(delegate { SelectedIndex = listBoxClients.SelectedIndex; })); } else { SelectedIndex = listBoxClients.SelectedIndex; } Socket s; if ((s = Socket.Sockets == null ? Socket.socket : (SelectedIndex >= 0 && SelectedIndex < Socket.Sockets.Count ? Socket.Sockets[SelectedIndex] : null)) == null) { return; } int bytesRead = s.EndReceive(ar); if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0}.EndReceive()", Socket.Name)); })); } else { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0}.EndReceive()", Socket.Name)); })); } if (bytesRead > 0) { Socket.sb.Append(Encoding.ASCII.GetString(Socket.buffer, 0, bytesRead)); if (s.Available > 0) { s.BeginReceive(Socket.buffer, 0, Socket.buffer.Length, 0, new AsyncCallback(ReceiveCallback), Socket); } else { if (Socket.sb.Length > 1) { string tmpString = Socket.sb.ToString(); Socket.sb.Length = 0; if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0} (Received {1} bytes Data {2})", Socket.Name, tmpString.Length, tmpString)); })); } else { ListBoxLog.Items.Add(string.Format("{0} (Received {1} bytes Data {2})", Socket.Name, tmpString.Length, tmpString)); } } } } else { if (Socket.sb.Length > 1) { string tmpString = Socket.sb.ToString(); if (ListBoxLog.InvokeRequired) { ListBoxLog.Invoke(new MethodInvoker(delegate { ListBoxLog.Items.Add(string.Format("{0} (Received {1} bytes Data {2})", Socket.Name, tmpString.Length, tmpString)); })); } else { ListBoxLog.Items.Add(string.Format("{0} (Received {1} bytes Data {2})", Socket.Name, tmpString.Length, tmpString)); } } } }