日期:2014-05-18 浏览次数:21035 次
public class Sco { string id;//按钮ID public string Id { get { return id; } set { id = value; } } Socket socketIp; public Socket SocketIp { get { return socketIp; } set { socketIp = value; } } } private void Sending(IAsyncResult rec_socket) { Sco s = new Sco(); s = (Sco)rec_socket.AsyncState; try { if (s.SocketIp.Connected) { byte[] msgBuff = Encoding.UTF8.GetBytes(s.Id); s.SocketIp.Send(msgBuff); } else { Console.WriteLine("Error!", "Error!"); } } catch { Console.WriteLine("Error!", "Error!"); } } private void button1_Click(object sender, EventArgs e) { Sco s = new Sco(); s.Id = ((Button)sender).Text; IPEndPoint epServer = new IPEndPoint(IPAddress.Parse("192.168.2.23"), 9000); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.SocketIp = socket; socket.BeginConnect(epServer, new AsyncCallback(Sending), s); }