C#网络编程之客户端编程与服务端的一般步骤(8)
来源:互联网 发布:迅雷赚钱宝网络监控 编辑:程序博客网 时间:2024/05/16 11:41
TCP编程的一般步骤
1.网络通信的最基本的前提就是客户端要先和服务器建立TCP连接
2.服务端要不断的监听客户端是否有连接请求、并且服务端能要识别特定的客户端
3.连接并创建对应的套接字
4.发送数据和接收数据
编写服务器端程序的一般步骤
1.创建一个TcpListener对象,然后调用该对象的Start方法在指定的端口进行监听
//生命
2.在单独的线程中,首先循环调用AcceptTcpClient方法接收客户端的连接请求
,从该方法中的返回结果中得到与该客户端对应的TcpClient对象,并利用该对象
的GetStream方法得到NetworkStream。然后再利用该对象得到其他使用更方便的
对象,比如BinaryReader对象、BinaryWrite对象,为进一步与对方通信做准备。
3.每得到一个新的TcpClient对象,就创建一个与客户对应的线程,在线程与对应的
客户进行通信。
TCP编程 客户端程序一般步骤
1.利用TcpClient的构造函数创建一个TcpClient对象
2.使用Connect方法与服务器建立连接
3.利用TcpClient对象的GetStream对象得到网络流,然后利用该网络流与服务器进行数据传输
4.创建一个线程监听指定的端口,循环接收并处理服务器发送过来的信息。
5.完成工作之后,想服务器发送关闭信息,并关闭与服务器的连接。
1.网络通信的最基本的前提就是客户端要先和服务器建立TCP连接
2.服务端要不断的监听客户端是否有连接请求、并且服务端能要识别特定的客户端
3.连接并创建对应的套接字
4.发送数据和接收数据
编写服务器端程序的一般步骤
1.创建一个TcpListener对象,然后调用该对象的Start方法在指定的端口进行监听
//生命
2.在单独的线程中,首先循环调用AcceptTcpClient方法接收客户端的连接请求
,从该方法中的返回结果中得到与该客户端对应的TcpClient对象,并利用该对象
的GetStream方法得到NetworkStream。然后再利用该对象得到其他使用更方便的
对象,比如BinaryReader对象、BinaryWrite对象,为进一步与对方通信做准备。
3.每得到一个新的TcpClient对象,就创建一个与客户对应的线程,在线程与对应的
客户进行通信。
4.根据传送信息的情况确定是否关闭与客户连接。
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; } } } }}
TCP编程 客户端程序一般步骤
1.利用TcpClient的构造函数创建一个TcpClient对象
2.使用Connect方法与服务器建立连接
3.利用TcpClient对象的GetStream对象得到网络流,然后利用该网络流与服务器进行数据传输
4.创建一个线程监听指定的端口,循环接收并处理服务器发送过来的信息。
5.完成工作之后,想服务器发送关闭信息,并关闭与服务器的连接。
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 { } } } }}
- C#网络编程之客户端编程与服务端的一般步骤(8)
- 网络编程的一般步骤
- 网络编程的一般步骤
- UNIX网络编程之简易客户端与服务端
- Winsocket编程一般步骤-服务端编程
- Java网络编程基于TCP的客户端与服务端
- JAVA--网络编程之服务端和客户端
- C# Socket编程 服务端与客户端(一)
- C# Socket编程 服务端与客户端(二)
- Python网络编程的一般步骤
- C# Socket编程 服务端与客户端(四) 异步服务端
- socket 编程之TCP服务端与客户端
- java网络编程 (客户端、服务端)
- TCP,UDP客户端和服务器端编程的一般步骤
- QT网络编程:服务端和客户端编程
- Android网络编程之TCP通信----简单客户端与服务端通信
- Android网络编程之UDP通信----简单客户端与服务端通信
- 黑马程序员_网络编程中的服务端与客户端
- 软测第五次实验结果
- 减去脂肪,轻身上阵 (zz)
- 致我已经逝去的青春
- 康乃笔记法---5R笔记法
- Unix/Linux编程之size_t、ssize_t
- C#网络编程之客户端编程与服务端的一般步骤(8)
- 【Obj-C】UIImage与位图(RGBA8 Bitmaps)的相互转化
- codeforces 11A - Increasing Sequence
- 转载文章:Microsoft 将僵尸网络威胁智能分析程序引入云中以提供近实时数据
- [反思] 2013-05-28那天健身
- php str_pad把字符串填充为指定的数量
- 检查文件名是否有效
- php stripos substr sprintf
- nginx下实现thinkphp伪静态