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.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace TCP
{
public partial class TcpListenerTest : Form
{
public TcpListenerTest()
{
InitializeComponent();
}
//声明
private IPAddress localAddress;// IP地址 IPAddress类 在命名空间 using System.Net下
private const int port = 58080;//端口
private TcpListener tcpListener;//监听套接字 TcpLestener与TcpClient类 在命名空间 using System.Net.Sockets下
private TcpClient tcpClient;//服务端与客户端建立连接
private NetworkStream newworkStream;//利用NetworkStream对象与远程主机发送数据或接收数据\
private BinaryReader binaryReader;//读取 BinaryReader与BinaryWriter类在命名空间using System.IO下
private BinaryWriter binaryWrite;//写入
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] listenerIp = Dns.GetHostAddresses("www.baidu.com");//返回主机指定的IP地址
localAddress = listenerIp[0]; //初始化IP地址为本地地址
tcpListener = new TcpListener(localAddress,port);//创建TcpListener对象
tcpListener.Start();//开始监听
Thread thread = new Thread(AcceptClientConnect);//启动一个线程接收请求
thread.Start();//启动线程
}
//发起请求
private void AcceptClientConnect()
{
while(true)
{
try
{
tcpClient = tcpListener.AcceptTcpClient();//从端口接收一个连接,并赋予它TcpClient对象
if (tcpClient != null)
{
newworkStream = tcpClient.GetStream();
binaryReader = new BinaryReader(newworkStream);
binaryWrite = new BinaryWriter(newworkStream);
}
}
catch
{
break;
}
}
}
}
}
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.IO;
namespace TCP
{
public partial class TcpClientTest : Form
{
public TcpClientTest()
{
InitializeComponent();
}
private TcpClient tcpClient;//声明
private IPAddress iAdress;//IP地址
private const int port=58080; //端口
private NetworkStream networkStream;//网络流
private BinaryReader binayRead;//读取
private BinaryWriter binayWrite;//写入
private void TcpClient_Load(object sender, EventArgs e)
{
IPAddress[] ipAddress = Dns.GetHostAddresses("www.baidu.com");//返回主机指定的IP地址
iAdress = ipAddress[0]; //初始化IP地址为本地地址
IPEndPoint ipoint = new IPEndPoint(iAdress,port);//将网络端点表示为IP和端口号
tcpClient = new TcpClient(ipoint);//实例化TcpClient类
//最方便 TcpClient tcpClient=new TcpClient("www.baidu.com",port);
if (tcpClient != null)
{
networkStream = tcpClient.GetStream();//得到网络流
binayRead = new BinaryReader(networkStream);
binayWrite = new BinaryWriter(networkStream);
}
}
private void ReMessage()
{
while (true)
{
try
{
string rcMsg = binayRead.ReadString();//从网络流中读取数据
if (rcMsg != null)
{
MessageBox.Show(rcMsg);
}
else
{
break;
}
}
catch
{
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有