码迷,mamicode.com
首页 > 其他好文 > 详细

05SockeT网络编程之客户端

时间:2016-02-28 22:42:04      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Net;
  8 using System.Net.Sockets;
  9 using System.Text;
 10 using System.Threading;
 11 using System.Threading.Tasks;
 12 using System.Windows.Forms;
 13 
 14 namespace _03ChatClient
 15 {
 16     public partial class ChatClient : Form
 17     {
 18         public ChatClient()
 19         {
 20             Control.CheckForIllegalCrossThreadCalls = false;
 21             InitializeComponent();
 22         }
 23 
 24         Socket skClient = null;//因为发送消息和连接服务器均要用的通讯socket所以声明在外面
 25         private void button1_Click(object sender, EventArgs e)
 26         {
 27             if (skClient == null)
 28             {
 29                 //不存在的时候创建一个客户端通讯
 30                 skClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 31                 //连接服务器
 32                 string ip = txtIP.Text.Trim();
 33                 string port = txtPort.Text.Trim();
 34                 skClient.Connect(new IPEndPoint(IPAddress.Parse(ip), int.Parse(port)));
 35                 txtLog.AppendText(string.Format("连接服务器{0}成功,端口号为{1}" + Environment.NewLine, ip, port));
 36 
 37                 ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
 38                 {
 39                     byte[] buffer = new byte[1024 * 1024 * 2];
 40                     while (true)
 41                     {
 42                        
 43                         //判断连接服务器的socket存在,并且发来的消息不为空才接收
 44                         if (skClient != null&&skClient.Available>0)
 45                         {
 46 
 47                             int count = skClient.Receive(buffer);//会阻塞线程,所以写入新线程中.
 48                             //接收数据的时候要判断第一个字节是什么
 49                             if (buffer[0] == 0)
 50                             {
 51                                 //发送的是字符串
 52                                 string msg = Encoding.UTF8.GetString(buffer,1, count-1);
 53                                 txtLog.AppendText("服务器发来消息:" + msg + Environment.NewLine);
 54                             }
 55                             else if (buffer[0] == 1)
 56                             {
 57                                 //发送的是文件
 58                             }
 59                             else if(buffer[0]==2)
 60                             {
 61                                 //发送的闪屏
 62                                 ShanPing();
 63                             }
 64                            
 65                         }
 66                         
 67                     }
 68 
 69                 }));
 70 
 71             }
 72             //skClient.Receive()
 73         }
 74         Random r = new Random();
 75         private void ShanPing()
 76         {
 77             //获取当前的创建的位置
 78             Point point = this.Location;
 79             //执行50次的创建小范围的移动
 80             for (int i = 0; i < 50; i++)
 81             {
 82                 int x = r.Next(1, 10) + point.X;
 83                 int y = r.Next(1, 15) + point.Y;
 84                 this.Location = new Point(x, y);
 85                 this.Location = point;
 86             }
 87         }
 88 
 89 
 90 
 91         //客户端向服务器发送消息
 92         private void button2_Click(object sender, EventArgs e)
 93         {
 94             //获取文本框中的内容太
 95             string strMsg = txtSendMsg.Text.Trim();
 96             //判断socket是否连接
 97             if (skClient != null && skClient.Connected)
 98             {
 99                 byte[] buffer = Encoding.UTF8.GetBytes(strMsg);
100                 skClient.Send(buffer);
101                 txtLog.AppendText("向服务器发送消息:" + strMsg+Environment.NewLine);
102             }
103             else
104             {
105                 MessageBox.Show("请先连接服务器");
106             }
107         }
108 
109         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
110         {
111             skClient.Shutdown(SocketShutdown.Both);
112             skClient.Close();
113             skClient.Dispose();
114             skClient = null;
115 
116         }
117     }
118 }

 

05SockeT网络编程之客户端

标签:

原文地址:http://www.cnblogs.com/clcloveHuahua/p/5225648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!