资源简介
C#代码写的TCP/IP通信接收数据,输入IP与端口即可

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TCP_Client
{
public partial class Form1 : Form
{
NetworkStream ns;
TcpClient clientsocket;
bool connected;
Thread receive;
public Form1()
{
InitializeComponent();
this.Text = “客户端“;
iptxt.Text = “127.0.0.1“;
}
public delegate void Recdatadelegate(String str);
private void showInfo(string s)
{
Invoke(new Recdatadelegate(rtbChatIn.AppendText) new object[] { s });
}
private void EstablishConnection()
{
sslbStatus.Text = “正在连接到服务器“;
try
{
string serverIP = iptxt.Text;
int serverPort = Convert.ToInt32(porttxt.Text);
clientsocket = new TcpClient(serverIP serverPort);
ns = clientsocket.GetStream();
//sr = new StreamReader(ns);
connected = true;
sslbStatus.Text = “已连接“;
}
catch (Exception)
{
MessageBox.Show(“不能连接到服务器!“ “错误“
MessageBoxButtons.OK MessageBoxIcon.Exclamation);
sslbStatus.Text = “已断开连接“;
}
}
private void RegisterWithServer()
{
try
{
string command = “*RS“ + txtCarNO.Text + “V1000047V0000.0000N00000.0000E000.00000080804FFFFFBFF#“; // string command = “CHAT|“ + clientname + “: “ + ChatOut.Text + “\r\n“;
Byte[] outbytes;
if (rdoASCII.Checked)
outbytes = System.Text.Encoding.Default.GetBytes(command.ToCharArray());
else
outbytes = System.Text.Encoding.Unicode.GetBytes(command.ToCharArray());
ns.Write(outbytes 0 outbytes.Length);
//clientsocket.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void QuitChat()
{
if (connected)
{
try
{
ns.Close();
clientsocket.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (receive != null && receive.IsAlive)
receive.Abort();
}
connected = false;
}
private void ReceiveChat()
{
bool keepalive = true;
while (keepalive)
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10343 2008-02-01 11:11 Form1.Designer.cs
文件 6014 2008-02-01 11:11 Form1.resx
文件 469 2008-02-01 10:27 Program.cs
文件 3227 2008-02-01 10:28 TCP_Client.csproj
目录 0 2008-02-01 10:27 Properties
文件 1184 2008-02-01 10:27 Properties\AssemblyInfo.cs
文件 2874 2008-02-01 10:27 Properties\Resources.Designer.cs
文件 5612 2008-02-01 10:27 Properties\Resources.resx
文件 1093 2008-02-01 10:27 Properties\Settings.Designer.cs
文件 249 2008-02-01 10:27 Properties\Settings.settings
文件 7863 2008-02-01 14:00 Form1.cs
----------- --------- ---------- ----- ----
38928 11
- 上一篇:C#USB通信,可以直接用的
- 下一篇:C# 微信公众平台开发.NET源码
相关资源
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 020ASP.NET车辆综合管理系统.zip
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
- C#指纹识别系统程序 报告
评论
共有 条评论