05SockeT网络编程之客户端

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了05SockeT网络编程之客户端相关的知识,希望对你有一定的参考价值。

  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网络编程之客户端的主要内容,如果未能解决你的问题,请参考以下文章

golang代码片段(摘抄)

Python 之 socket网络模块简单应用

socket编程之TCP/UDP

网络编程基础之粘包现象

网络编程-SOCKET开发之----3. socket通信工作流程

Java语言 网络编程之Socket用法