-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketsTest.cs
More file actions
162 lines (108 loc) · 3.84 KB
/
Copy pathSocketsTest.cs
File metadata and controls
162 lines (108 loc) · 3.84 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.Net.Sockets;
using System.Net;
using System.Threading;
namespace SocketsClient
{
public partial class SocketsTest : Form
{
public System.Net.Sockets.Socket clientSocket { get; set; }
public Socket ServerSocket { get; set; }
System.Threading.Thread th;
public SocketsTest()
{
InitializeComponent();
}
private void SetMessage(string message)
{
this.label3.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (this.clientSocket != null && this.clientSocket.Connected )
{
return;
}
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(SocketsClient.Properties.Settings.Default.ServerAddress), SocketsClient.Properties.Settings.Default.Port);
this.clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(ipep);
ServerSocket.Bind(ipep);
ServerSocket.Listen(10);
while (true)
{
clientSocket = ServerSocket.Accept();
th = new Thread(new ThreadStart(doWork));
th.Start();
}
th = new System.Threading.Thread(() => {
});
}
catch (Exception ex)
{
SetMessage("发送消息错误!" + ex.Message);
}
}
private void doWork()
{
Socket s = clientSocket;//客户端信息
IPEndPoint ipEndPoint = (IPEndPoint)s.RemoteEndPoint;
String address = ipEndPoint.Address.ToString();
String port = ipEndPoint.Port.ToString();
Console.WriteLine(address + ":" + port + " 连接过来了");
Byte[] inBuffer = new Byte[1024];
Byte[] outBuffer = new Byte[1024];
String inBufferStr;
String outBufferStr;
try
{
while (true)
{
s.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环
inBufferStr = Encoding.ASCII.GetString(inBuffer);
Console.WriteLine(address + ":" + port + "说:");
Console.WriteLine(inBufferStr);
outBufferStr = Console.ReadLine();
outBuffer = Encoding.ASCII.GetBytes(outBufferStr);
s.Send(outBuffer, outBuffer.Length, SocketFlags.None);
}
}
catch
{
Console.WriteLine("客户端已关闭!");
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
Byte[] outBuffer = Encoding.Default .GetBytes( this.textBox1.Text);
//发送消息
clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);
this.textBox1.Text = "";
}
catch(Exception ex)
{
SetMessage("发送消息错误!"+ ex.Message );
}
}
private void SocketsTest_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.clientSocket != null)
{
if (this.clientSocket.Connected)
{
this.clientSocket.Close();
}
}
}
}
}