socket 学习(转载)
原文:
http://www.cnblogs.com/licongjie/archive/2006/10/26/540640.html服务器端代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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.Text;
8
using System.Windows.Forms;
9
using System.Net;
10
using System.Net.Sockets;
11
using System.Threading;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
namespace Server
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
public partial class Form1 : Form
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
public Form1()
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
InitializeComponent();
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
Thread serverThread;
23
Thread clientThread;
24
Socket serverSocket;
25
Socket clientSocket;
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
public void ServerStart()
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 6009);
30
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
serverSocket.Bind(ipep);
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
serverSocket.Listen(10);
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
while (true)
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
try
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
clientSocket = serverSocket.Accept();
41
clientThread = new Thread(new ThreadStart(ReceiveData));
42
clientThread.Start();
43
}
44
catch (Exception ex)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
MessageBox.Show("listening Error:" + ex.Message);
47
}
48
}
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
private void ReceiveData()
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
bool keepalive = true;
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
Socket s = clientSocket;
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
Byte[] buffer = new Byte[1024];
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
//根据接收到得客户端套接字,向客户端发送信息
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
IPEndPoint clientep = (IPEndPoint)s.RemoteEndPoint;
62
lstServer.Items.Add("Client:" + clientep.Address + "(" + clientep.Port + ")");
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
string welcome = "welcome to here ";
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
byte[] data = new byte[1024];
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
data = Encoding.ASCII.GetBytes(welcome);
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
s.Send(data, data.Length, SocketFlags.None);
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
while (keepalive)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
int bufLen = 0;
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
try
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
bufLen = s.Available;
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
s.Receive(buffer, 0, bufLen, SocketFlags.None);
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
if (bufLen == 0)
85
continue;
86
}
87
catch (Exception ex)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
MessageBox.Show("Receive Error:" + ex.Message);
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
return;
92
}
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
clientep = (IPEndPoint)s.RemoteEndPoint;
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0, bufLen);
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
lstServer.Items.Add(clientcommand + "(" + clientep.Address + ":" + clientep.Port + ")");
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
string today = "today";
103
byte[] data2 = new byte[1024];
104
data2 = Encoding.ASCII.GetBytes(today);
105![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
s.Send(data2, data2.Length, SocketFlags.None);
107
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
}
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
private void btnOpen_Click(object sender, EventArgs e)
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
serverThread = new Thread(new ThreadStart(ServerStart));
116
serverThread.Start();
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
lstServer.Items.Add("Server Start
.");
119
}
120
}
121
}
122![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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.Text;
8
using System.Windows.Forms;
9
using System.Net;
10
using System.Net.Sockets;
11
using System.Threading;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
namespace Client
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
public partial class Form1 : Form
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
Socket clientSocket;
18
Thread clientThread;
19
20
public Form1()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
InitializeComponent();
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
private void ConnectToServer()
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
byte[] data = new byte[1024];
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6009);
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
try
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
clientSocket.Connect(ipep);
37
}
38
catch (SocketException ex)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
MessageBox.Show("connect error:" + ex.Message);
41
return;
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
while (true)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
int bufLen = 0;
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
try
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
bufLen = clientSocket.Available;
51
clientSocket.Receive(data, 0, bufLen, SocketFlags.None);
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
if (bufLen == 0)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
continue;
56
}
57
}
58
catch (Exception ex)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
MessageBox.Show("Receive Error:" + ex.Message);
61
return;
62
}
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
string clientcommand = System.Text.Encoding.ASCII.GetString(data).Substring(0, bufLen);
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
lstClient.Items.Add(clientcommand);
67
}
68
}
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
private void button1_Click(object sender, EventArgs e)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
clientThread = new Thread(new ThreadStart(ConnectToServer));
73
clientThread.Start();
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
private void btnSend_Click(object sender, EventArgs e)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
byte[] data = new byte[1024];
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
data = Encoding.ASCII.GetBytes(txtClient.Text);
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
clientSocket.Send(data, data.Length, SocketFlags.None);
83
}
84
}
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
代码下载:
/Files/wenjie/socketsample.rar
原文地址:https://www.cnblogs.com/wenjie/p/1554606.html