我是靠谱客的博主 独特世界,这篇文章主要介绍c# 客户端,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net.Sockets; using System.Net; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket client; private void button2_Click(object sender, EventArgs e) { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(this.ip.Text); IPEndPoint prot = new IPEndPoint(ip, Convert.ToInt32(this.port.Text)); client.Connect(prot); //服务器端口 listin("连接成功"); Thread th = new Thread(sends); th.Start(); } public void listin(string msg) { this.textBox2.AppendText(msg + "rn"); } void sends() { try{ while (true) { Byte[] buf = new Byte[1024 * 1024 * 3]; int result = client.Receive(buf); if (result == 0) { break; } if (buf[0] == 0) { //接收消息 string str = Encoding.UTF8.GetString(buf, 1, result-1); listin(client.RemoteEndPoint.ToString() + ":" + str); } else if (buf[0] == 1) { SaveFileDialog sd = new SaveFileDialog(); //创建 // 保存文件对话框 sd.InitialDirectory = @"C:Documents and SettingsAll Users桌面"; //设置对话框路径 sd.Title = "对话框1"; //对话框标题 sd.Filter = "所有文件|*.*"; sd.ShowDialog(); string path = sd.FileName; using (FileStream fsv = new FileStream(path, FileMode.Create, FileAccess.Write)) { //byte[] bytes = Encoding.Default.GetBytes(this.textBox1.Text); //string str = Encoding.UTF8.GetString(buf, 1, result - 1); fsv.Write(buf, 1, buf.Length - 1); } MessageBox.Show("保存成功"); } else if (buf[0] == 2) { zheng(); } } } catch { } } void zheng() { int x = this.Location.X; int y = this.Location.Y; for (int i = 0; i < 500; i++) { this.Location = new Point(x - 20, y - 20); this.Location = new Point(x , y); } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } private void button1_Click(object sender, EventArgs e) { string str = this.textBox1.Text.Trim(); Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str); client.Send(buf); } } }

  

转载于:https://www.cnblogs.com/mengluo/p/5648809.html

最后

以上就是独特世界最近收集整理的关于c# 客户端的全部内容,更多相关c#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部