易截截图软件、单文件、免安装、纯绿色、仅160KB

C#的TCP通信


TCP是连接模型,如下:
服务器连接                     服务器断开
    ↓                                   ↑
服务器接收→服务器处理→服务器发送
    ↑                                   ↓
客户端发送←客户端处理←客户端接收
    ↑                                   ↓
客户端连接                     客户端断开
服务器代码如下:
//
ServerListen
TcpListener tcplistener
=
new
 TcpListener(
int
.Parse(txtPort.Text));
tcplistener.Start();
byte
[] btServerReceive
=
new
 
byte
[
256
];
string
 strServerReceive
=
string
.Empty;
//
Loop for Listen
while
(
true
)
{
 TcpClient tcp
=
tcplistener.AcceptTcpClient();
 NetworkStream ns
=
tcp.GetStream();
 
int
 intReceiveLength
=
 ns.Read(btServerReceive,
0
,btServerReceive.Length);
 strServerReceive
=
UnicodeEncoding.Unicode.GetString(btServerReceive,
0
,intReceiveLength);
 txtServerResult.AppendText(
"
ServerReceive:
"
 
+
 strServerReceive
+
"

"
);
 ns.Write(btServerReceive,
0
,btServerReceive.Length);
 txtServerResult.AppendText(
"
ServerSend:
"
&


相关文档:

C#中C/C++程序员注意问题

1、C/C++程序员请注意,不能在case语句不为空时“向下执行”。
2、值类型和引用类型之间的区别:C#的基本类型(int,char等)都是值类型,是在栈中创建的。而对象是引用类型,创建于堆中,需要使用关键字new。
3、在C#中通过实例访问静态方法或成员变量是不合法的,会生成编译器错误。但是我们可以通过声明他们 ......

C#调用DLL (strmagic)

  大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过C#中的DllImport直接调用这些功能。
  DllImport所在的名字空间 using System.Runt ......

c# 操作excel

 OleDbConnection conn = null;
            try
            {
               `string strConn;
  &n ......

C# 回滚

 private void save_db(){
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[""].ToString());
    SqlCommand comm = new SqlCommand();
    conn.Open();
    SqlTransaction rollbk2= conn.BeginTransaction();
& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号