TCP---Socket通讯
服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _021_socket编程_TCP协议 {
class Program {
static void Main(string[] args) {
//1、创建socket
Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//2、绑定ip跟端口号 192.168.0.112 10.0.5.35
IPAddress ipaddress = new IPAddress(new byte[]{10,0,5,35});
//ipendpoint是对ip+端口做了一层封装的类
EndPoint point = new IPEndPoint(ipaddress,8090);
//3、向操作系统申请一个可用的ip跟端口号 用来做通信
tcpServer.Bind(point);
//4、开始监听 (等待客户端连接)
tcpServer.Listen(100);//参数是最大连接数
Console.WriteLine("开始监听");
//暂停当前线程,直到有一个客户端连接过来,之后进行下面的代码
Socket clientSocket = tcpServer.Accept();
Console.WriteLine("一个客户端连接过来了");
//使用返回的socket跟客户端做通信
string message = "hello 欢迎你";
byte[] data = Encoding.UTF8.GetBytes(message);//对字符串做编码,得到一个字符串的字节数组
clientSocket.Send(data);
Console.WriteLine("向客户端发送了一跳数据");
byte[] data2 = new byte[1024];//创建一个字节数组用来当做容器,去承接客户端发送过来的数据
int length = clientSocket.Receive(data2);
string message2 = Encoding.UTF8.GetString(data2, 0, length);//把字节数据转化成 一个字符串
Console.WriteLine("接收到了一个从客户端发送过来的消息:"+message2);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _001_socket编程_tcp协议_客户端 {
class Program {
static void Main(string[] args) {
//1,创建socket
Socket tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
//2,发起建立连接的请求
IPAddress ipaddress = IPAddress.Parse("10.0.5.35");//可以把一个字符串的ip地址转化成一个ipaddress的对象
EndPoint point = new IPEndPoint(ipaddress,8090);
tcpClient.Connect(point);//通过ip:端口号 定位一个要连接到的服务器端
byte[] data = new byte[1024];
int length = tcpClient.Receive(data);//这里传递一个byte数组,实际上这个data数组用来接收数据
//length返回值表示接收了多少字节的数据
string message = Encoding.UTF8.GetString(data,0,length);//只把接收到的数据做一个转化
Console.WriteLine(message);
//向服务器端发送消息
string message2 = Console.ReadLine();//读取用户的输入 把输入发送到服务器端
tcpClient.Send( Encoding.UTF8.GetBytes(message2) );//把字符串转化成字节数组,然后发送到服务器端
Console.ReadKey();
}
}
}
tcplistener
服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace _024_tcplistener {
class Program {
static void Main(string[] args) {
//1,TcpListener对socket进行了一层封装,这个类里面自己会去创建socket对象
TcpListener listener = new TcpListener(IPAddress.Parse("10.0.5.35"), 7788);
//2,开始进行监听
listener.Start();
//3,等待客户端连接过来
TcpClient client = listener.AcceptTcpClient();
//4,取得客户端发送过来的数据
NetworkStream stream = client.GetStream();//得到了一个网络流 从这个网络流可以取得客户端发送过来的数据
byte[] data = new byte[1024];//创建一个数据的容器,用来承接数据
while (true)
{
//0 表示从数组的哪个索引开始存放数据
//1024表示最大读取的字节数
int length = stream.Read(data, 0, 1024);//读取数据
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine("收到了消息:" + message);
}
stream.Close();
client.Close();
listener.Stop();//停止监听
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _003_tcpclient {
class Program {
static void Main(string[] args) {
//当我们创建tcpclient对象的时候,就会跟server去建立连接
TcpClient client = new TcpClient("10.0.5.35", 7788);
NetworkStream stream = client.GetStream();//通过网络流进行数据的交换
//read用来读取数据,write用来写入数据其实就是发送数据
//利用一个死循环,重复向服务器端发送数据
while (true)
{
string message = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(message);
stream.Write(data, 0, data.Length);
}
stream.Close();
client.Close();
Console.ReadKey();
}
}
}
udpClient
服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _025_udpclient {
class Program {
static void Main(string[] args) {
//创建udpclient 绑定ip跟端口号
UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("10.0.5.35"), 8090));
while (true)
{
//接收数据
IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组,就是我们的数据
string message = Encoding.UTF8.GetString(data);
Console.WriteLine("收到了消息:" + message);
}
udpClient.Close();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _004_udpclient {
class Program {
static void Main(string[] args) {
//创建udpclient对象
UdpClient client = new UdpClient();
while (true)
{
string message = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(message);
client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("10.0.5.35"), 8090));
}
client.Close();
Console.ReadKey();
}
}
}