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