c# TCPclient
服务端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TcpListener tcp;
private void button1_Click(object sender, EventArgs e)
{
tcp = new TcpListener(IPAddress.Any, 5000);
tcp.Start();
Thread t = new Thread(server);
t.IsBackground = true;
t.Start(tcp);
} private void server(object o)
{ TcpListener list = o as TcpListener;
this.textBox1.AppendText("连接成功"+"\r\n");
while (true)
{
const int buffer = 256;
TcpClient client = list.AcceptTcpClient();
NetworkStream strem = client.GetStream();
byte[] b = new byte[buffer];
int r = strem.Read(b, 0, buffer);
string str = Encoding.UTF8.GetString(b, 0, r); this.textBox2.AppendText(client.Client.RemoteEndPoint.ToString() + ":"+str+"\r\n");
strem.Close(); } } public void showmsg(string msg)
{
this.textBox2.AppendText(msg + "\r\n");
} private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
}
}
客户端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading; namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TcpClient client;
private void button1_Click(object sender, EventArgs e)
{
const int size = 406;
client = new TcpClient();
client.Connect(IPAddress.Parse("172.16.5.24"),5000);
NetworkStream strem = client.GetStream();
string str = this.textBox1.Text.Trim();
byte[] b = Encoding.UTF8.GetBytes(str);
strem.Write(b, 0, b.Length);
strem.Close();
client.Close(); } private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
}
}
c# TCPclient的更多相关文章
- C# - 网络编程 之 TcpClient与TcpListener
TcpClient类 TcpListener类 TCP通信 UDP通信 参考:
- TcpClient 有好多坑
下面2篇文章里头的问题都碰到了,真是好坑哈, 在此留念. 使用 TcpClient 與 NetworkStream 類別開發時的注意事項 [C#] NetworkStream.Write()存在严重b ...
- Socket聊天室-TcpListener,TcpClient
参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端:
- TcpClient类与TcpListener类
TcpClient类 //构造方法1 TcpClient t = new TcpClient(); t.Connect(); //构造方法2 IPEndPoint iep = ); TcpClient ...
- Socket的三个功能类TCPClient、TCPListener 和 UDPClient (转)
应用程序可以通过 TCPClient.TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务.这些协议类建立在 System.Net.So ...
- C# TcpClient TcpListener 简单练习01
下面是读<Visual C#.Net 网络编程>整理的练习代码. 客户端发送命令给服务端,从服务器端获取所有人员的成绩或者指定人员的成绩. 命令格式为 GET 0|1 [Name].0为获 ...
- Socket与TcpClient的区别(转载)
Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...
- TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决
TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决 //以下是tcpclient服务器端的监听程序,假设服务器端和客户端在同一台机器上,//为了使客户端可 ...
- C#TCPClient应用-一个简单的消息发送和接收
TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...
随机推荐
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)
12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...
- The Havel-Hakimi Algorithm
1.If any di>=n then fail 2.If there is an odd number of odd degrees then fail 3.If there is a di& ...
- 转:Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- ubuntu安装jdk遇到的问题:cannot execute binary file
安装完jdk,配置好环境变量出现如下状况: cannot execute binary file 问题原因: jdk的位数与ubuntu的系统位数不一致 jdk 64位 ubuntu 32位 然后通过 ...
- java程序运行时内存分配详解 (转)
转自:http://www.tuicool.com/articles/uU77v2 一. 基本概念 每运行一个java程序会产生一个java进程,每个java进程可能包含一个或者多个线程,每一个Ja ...
- javascript 异常处理和事件处理
异常捕获 1.异常:当javascript引擎执行JS代码时,发生了错误,导致程序停止运行. 2.异常抛出:当异常产生,并且将这个异常生成一个错误信息 3.异常捕获: try{发生异常的代码块:}ca ...
- HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4900 Problem Description There is an old country and ...
- spring day02-go3
1.复制xml到container/basic包下2.ExampleBean中设置属性,包括基本类型(String和age)和集合类型(List,Set,Map),Properties类型3.添加无参 ...
- 2.session与cookie的区别?
session:储存用户访问的全局唯一变量,存储在服务器上的php指定的目录中的(session_dir)的位置进行的存放 cookie:用来存储连续訪問一个頁面时所使用,是存储在客户端,对于Cook ...
- Calculation
定义一个Strategy接口,其中定义一个方法,用于计算 using System; using System.Collections.Generic; using System.Linq; usin ...