没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
C#,C++,Delphi 的 Socket 编程
今天分别用 Visual C#2003,Dev C++4, Delphi7 编写了 Socket 的 client 发数据给 server 的两端程
序,并成功实现了不同种程序间的通讯。比如 delphi socket client 或 C++socket client访问 C#
socketserver 和 C++ socket server。其中 C++运行最快,C#最稳定,Delphi 编写最快,效率也还
行。C++的代码如果在 linux 下编译,可在 linux 下运行。delphi 代码 放到 kylix 下或许也行。C#的
恐怕要 edit 后用 Mono 编译才能在 Linux 下运行。
下面代码在 Visual C#2003 下编译通过
/************SocketClient****VS C#2003********************/
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net.Sockets ;
//使用到 TcpListen 类
using System.Net ;
namespace WindowsApplication3
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ListBox listBox1;
///
/// 必需的设计器变量。
int port = 8000 ;
//定义侦听端口号
private TcpClient tcpc ;
//对服务器端创建 TCP 连接
private Socket stSend ;
//创建发送数据套接字
private bool tcpConnect = false ;
//定义标识符,用以表示 TCP 连接是否建立
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
int iLength = textBox2.Text.Length ;
//获取要发送的数据的长度
Byte [ ] bySend = new byte [ iLength ] ;
//根据获取的长度定义一个 Byte 类型数组
bySend = System.Text.Encoding.Default.GetBytes( textBox2.Text ) ;
//按照指定编码类型把字符串指定到指定的 Byte 数组
int i = stSend.Send ( bySend ) ;
//发送数据
listBox1.Items.Add ( textBox2.Text ) ;
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 26);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(99, 38);
this.label1.TabIndex = 0;
this.label1.Text = "IP 地址:";
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(125, 23);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(222, 25);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(373, 18);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(83, 36);
this.button1.TabIndex = 2;
this.button1.Text = "连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(21, 82);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(134, 30);
this.label2.TabIndex = 3;
this.label2.Text = "发送信息:";
//
// textBox2
//
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox2.Location = new System.Drawing.Point(125, 75);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(222, 25);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// listBox1
//
this.listBox1.ItemHeight = 15;
this.listBox1.Location = new System.Drawing.Point(27, 152);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(448, 184);
this.listBox1.TabIndex = 6;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 378);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(493, 29);
this.statusBar1.TabIndex = 7;
this.statusBar1.Text = "无连接";
//
// label3
//
this.label3.Location = new System.Drawing.Point(19, 121);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(170, 29);
this.label3.TabIndex = 8;
this.label3.Text = "已经发送的信息:";
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(373, 69);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(83, 36);
this.button2.TabIndex = 9;
this.button2.Text = "发送";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(493, 407);
this.Controls.Add(this.button2);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.label3);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "利用 Socket 来发送数据";
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//以下代码是判断是否和远程终结点成功连接
try
剩余22页未读,继续阅读
资源评论
- 酸酸甜甜就是我啊2014-05-28是个doc文档,变成代码上传多好Y
- juzzw762012-06-27怎么抓到 socket连接失败的信息呢,再改改就5分了
- 浪漫炒饭2013-06-13不错,挺好用的~
shijiaohui123
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Fortran语言语法快速入门.pdf
- 明日方舟 年 鼠标指针.rar
- 全国银行经济监管可视化系统
- e商小二-供应链管理物流交仓创业商业计划书.rar
- Edge浏览器下载文件提示 “无法安全下载” 的解决方法
- 基于springboot+layui的医院日常耗材管理系统.zip
- Emkex亿迈克思新型互联网磁材供应链商业计划书.rar
- 计算机毕业设计-ASP.NET某店POS积分管理系统-销售情况,会员卡再发行数据生成(源代码+)-毕设源码实例.zip
- 计算机毕业设计-asp.net某店POS积分管理系统-清除履历表、日志表、月购买额(源代码+)-毕设源码实例.zip
- 计算机毕业设计-ASP.NET某店POS积分管理系统-积分实绩更新及销售状况统计(源代码+)-毕设源码实例.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功