/* * /// <summary> * /// 发送文件线程 * /// </summary> * private void SendThread(Socket socket) * { * bool ret = Server.SendFile(socket, "C:/123.bmp"); //clientSocketList[0]改以及路径没错 * * if (ret) * { * SendMessage(MSG_SEND_SUCCEED, "发送文件成功"); * } * else * { * SendMessage(MSG_SEND_FAIL, "发送文件失败"); * } * * * } */ private void Form1_Load(object sender, EventArgs e) { //填充IP string[] s = MyServer.GetLocalIPs(); foreach (string st in s) { comboBox1.Items.Add(st); if (st.StartsWith("210") || st.StartsWith("10")) { comboBox1.Text = st; } } //player.URL = "D:/亲爱的那不是爱情.mp3"; //player.close(); player.enableContextMenu = false; player.Ctlcontrols.currentPosition = 0.4; //DataBase.Open(); //player.settings.autoStart = true; player.settings.volume = 20; }
private void btnCreateServer_Click(object sender, EventArgs e) { string ip = comboBox1.Text.Trim(); String strPort = txtPort.Text.Trim(); int port; try { port = Int32.Parse(strPort); } catch { MessageBox.Show("请输入正确的端口号"); return; } if (MyServer.haveCreate()) { MessageBox.Show("已创建服务器,不能再创建"); return; } if (!MyServer.CreateServer(ip, 1234)) { lblServerState.Text = "创建失败"; } else { lblServerState.Text = "创建成功"; //显示本机IP和服务器占用的端口 //lblIP.Text = Server.GetServerIP(); //lblPort.Text = Server.GetPort(); //创建新线程来监听客户端的请求 Thread thread = new Thread(new ThreadStart(ServerListen)); thread.Start(); //显示二维码 string path = UserData.getPath() + "\\code.bmp"; CreateTwoCode ctc = new CreateTwoCode(); //调用的时候换成这个方法 传递2个参数就行了 一个是要生成的字符,一个是生成之后二维码保存路径 MyFile.DeleteFile(path); ctc.CreateNewCode("ip<" + comboBox1.Text + ">port<" + txtPort.Text + ">", path); this.pictureBox1.BackgroundImage = Image.FromFile(path); } }
/// <summary> /// 监听客户端的连接 /// </summary> private void ServerListen() { //同步方案 Socket serverSocket = MyServer.GetServerSocket(); //TODO 程序退出时不能退出死循环 serverSocket.Listen(10); //开始监听,限制同时在线的最大客户端数量为10 serverSocket.BeginAccept(AcceptCallBack, serverSocket); //Tool.Log("监听死循环结束,"); }
/// <summary> /// 接收文字线程 /// </summary> /// <param name="socket"></param> public void ReceiveThread(Socket socket, string id) { if (whatRecv == 0) //接收文字 { while (ClientList.haveId(id)) { if (whatRecv == 0) { Tool.Log("接收死循环开始"); string recvStr = MyServer.ReceiveText((Socket)socket); if (recvStr != null) { if (!recvStr.Equals("退出")) { Tool.Log("接收到文字:" + recvStr); SendMessage(id, MSG_RECEIVE, recvStr); } else { Tool.Log("ID为" + id + "的客户端退出"); ClientList.Remove(id); } } } else if (whatRecv == 1) //接收文件 { int ret = MyServer.ReceiveFile((Socket)socket); //接收文件 MessageBox.Show("接收返回"); //不能返回 if (ret == Return.SUCCEED) { //SendMessage(MSG_RECEIVE_SUCCEED, "接收成功"); } else { //SendMessage(MSG_RECEIVE_FAIL, "接收失败"); } } } Tool.Log("ID为" + id + "的接收死循环结束"); SendMessage(id, MSG_UPADTE_COUNT, null); } }
/*发送文件 * Thread thread = new Thread(new ThreadStart(SendThread)); * thread.Start(); */ private void SendTextThread(object socket, object sendStr) { Tool.Log("发送: " + (string)sendStr); MyServer.SendText((Socket)socket, (string)sendStr); }