























1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
// 新添加的命名空间。
8
using System.Net;
9
using System.Net.Sockets;
10
using System.Text;
11
using System.IO;
12
13
namespace SocketClient
14
{
15
/// <summary>
16
/// Socket套接字实现客户端连接。
17
/// </summary>
18
public class Form1 : System.Windows.Forms.Form
19
{
20
private System.Windows.Forms.Button button1;
21
private System.Windows.Forms.RichTextBox richTextBox1;
22
/// <summary>
23
/// 必需的设计器变量。
24
/// </summary>
25
private System.ComponentModel.Container components = null;
26
27
public Form1()
28
{
29
//
30
// Windows 窗体设计器支持所必需的
31
//
32
InitializeComponent();
33
34
//
35
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
36
//
37
}
38
39
/// <summary>
40
/// 清理所有正在使用的资源。
41
/// </summary>
42
protected override void Dispose( bool disposing )
43
{
44
if( disposing )
45
{
46
if (components != null)
47
{
48
components.Dispose();
49
}
50
}
51
base.Dispose( disposing );
52
}
53
54
Windows Form Designer generated code
96
97
/// <summary>
98
/// 应用程序的主入口点。
99
/// </summary>
100
[STAThread]
101
static void Main()
102
{
103
Application.Run(new Form1());
104
}
105
106
private void button1_Click(object sender, System.EventArgs e)
107
{
108
// 服务器IP地址
109
IPAddress serIP;
110
// 服务器节点
111
IPEndPoint server;
112
// 客户端Socket
113
Socket client;
114
// 客户端网络数据流
115
NetworkStream stream;
116
// 客户端写数据流
117
TextWriter writer;
118
// 客户端读数据流
119
TextReader reader;
120
121
serIP = IPAddress.Parse("218.66.48.230");
122
server = new IPEndPoint(serIP,34567);
123
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
124
client.Connect(server);
125
if(client.Connected)
126
{
127
stream = new NetworkStream(client);
128
writer = new StreamWriter(stream);
129
reader = new StreamReader(stream);
130
richTextBox1.Text += "连接到服务器
\n";
131
richTextBox1.Text += reader.ReadLine();
132
richTextBox1.Text += reader.ReadLine();
133
richTextBox1.Text += "\n";
134
writer.WriteLine("你好,服务器,很高兴能和你通讯,谢谢。");
135
writer.Flush();
136
reader.Close();
137
writer.Close();
138
stream.Close();
139
client.Close();
140
}
141
}
142
}
143
}
144
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。