using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace TCP服务器端
{
class Program
{
static void Main(string[] args)
{
StartServerAsync();
Console.ReadKey();
}
static void StartServerAsync()
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("192.168.43.231");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);
serverSocket.Bind(ipEndPoint);//绑定ip和端口号
serverSocket.Listen(0);//开始监听端口号
//Socket clientSocket = serverSocket.Accept();//接收一个客户端链接
serverSocket.BeginAccept(AcceptCallBack,serverSocket);
}
static Message msg = new Message();
//发送
static void AcceptCallBack(IAsyncResult ar)
{
Socket serverSocket = ar.AsyncState as Socket;
Socket clientSocket = serverSocket.EndAccept(ar);
//向客户端发送消息
string message = "hello 你好";
byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, null);
serverSocket.BeginAccept(AcceptCallBack, serverSocket);
}
//接收
static byte[] dataBuffer = new byte[1024];
static void ReceiveCallBack(IAsyncResult ar)
{
Socket clientSocket = null;
try
{
clientSocket = ar.AsyncState as Socket;
int count = clientSocket.EndReceive(ar);
if (count == 0)
{
clientSocket.Close();
return;
}
msg.AddCount(count);
//string message = Encoding.UTF8.GetString(dataBuffer, 0, count);
//Console.WriteLine("从客户端接收到数据:"+count + message);
//clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, null);
msg.ReadMessage();
clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, null);
}
catch (Exception e)
{
Console.WriteLine(e);
if (clientSocket != null)
{
clientSocket.Close();
}
}
finally
{
}
;
}
void StartServerSync()
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress ipAddress=new IPAddress(new byte[] { 192,168,43,231});
IPAddress ipAdress = IPAddress.Parse("192.168.43.231");
IPEndPoint ipEndPoint = new IPEndPoint(ipAdress, 7788);
serverSocket.Bind(ipEndPoint); //绑定ip+端口号
serverSocket.Listen(0); //开始监听端口号
Socket clientSocket = serverSocket.Accept(); //接收一个客户端连接
//向客户端发送消息
string message = "hello 你好";
byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
//接收客户端的消息
byte[] dataBuffer = new byte[1024];
int count = clientSocket.Receive(dataBuffer);
string messageReceive = System.Text.Encoding.UTF8.GetString(dataBuffer, 0, count);
Console.WriteLine(messageReceive);
Console.ReadKey();
serverSocket.Close();
clientSocket.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TCP服务器端
{
class Message
{
private byte[] data = new byte[1024];
private int startIndex = 0;//在数组里存取的字节个数
public void AddCount(int count)
{
startIndex += count;
}
public byte[] Data
{
get { return data; }
}
public int StartIndex
{
get { return startIndex; }
}
public int RemainSize
{
get { return data.Length - startIndex; }
}
//解析数据/读取数据
public void ReadMessage()
{
while (true)
{
if (startIndex <= 4) break;
int count = BitConverter.ToInt32(data, 0);
if (startIndex - 4 >= count)
{
string s = Encoding.UTF8.GetString(data, 4, count);
Console.WriteLine("解析出一条数据" + s);
Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
startIndex -= count + 4;
}
else break;
}
}
}
}