作者:神秘布拉阁俱乐部 | 来源:互联网 | 2023-07-03 12:16
引用Newtonsoft.Json.dllusingNewtonsoft.Json;进行对象序列化和反序列化。服务端:usingSystem;usingSystem.Collect
引用Newtonsoft.Json.dll
using Newtonsoft.Json;
进行对象序列化和反序列化。
服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleAppS
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse(args[0]);
int port = int.Parse(args[1]);
IPEndPoint iPEndPoint = new IPEndPoint(ip, port);
Socket socketS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketS.Bind(iPEndPoint);
socketS.Listen(10);
Console.WriteLine("开启服务:" + iPEndPoint.ToString());
while (true)
{
Socket socketC = socketS.Accept();
if (socketC.Connected)
{
Thread clientThread = new Thread(new ParameterizedThreadStart(MyClient));
clientThread.IsBackground = true;
clientThread.Start(socketC);
}
}
}
private const int BUFFER_SIZE = 1024; // 缓冲区大小
private static void MyClient(object socket)
{
Socket socketClient = socket as Socket;
try
{
byte[] buffer = new byte[BUFFER_SIZE];
while (true)
{
int countRcv = socketClient.Receive(buffer);
string rcvStr = Encoding.Default.GetString(buffer, 0, countRcv);
if (rcvStr[0] == '{')
{
object stuObj = JsonConvert.DeserializeObject(rcvStr);
Student stu = stuObj == null ? null : stuObj as Student;
if (stu != null)
{
Console.WriteLine("反序列对象:" + stu.ToString());
}
}
Console.WriteLine("服务端收到:" + socketClient.RemoteEndPoint.ToString() + " :" + rcvStr);
socketClient.Send(Encoding.Default.GetBytes("Ok! Server recevied"));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("客户端:" + socketClient.RemoteEndPoint.ToString() + "退出");
}
}
}
[Serializable]
public class Student
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft;
using Newtonsoft.Json;
namespace ConsoleAppC
{
class Program
{
static void Main(string[] args)
{
IPAddress ipAdr = IPAddress.Parse(args[0]);
int port = int.Parse(args[1]);
IPEndPoint ipEndP = new IPEndPoint(ipAdr, port);
int bufferSize = 1024;
int rcvCount;
byte[] buffer = new byte[bufferSize];
try
{
m_SocketClient.Connect(ipEndP);
Console.WriteLine($"链接服务器{ipEndP}成功...");
while (true)
{
m_COntent= Console.ReadLine();
if (m_COntent== "q" || m_COntent== "Q")
{
break;
}
if (m_COntent== "json")
{
Student stu = new Student
{
Name = "小明",
Sex = "难",
Age = 12
};
// Joson 序列化
m_COntent= JsonConvert.SerializeObject(stu);
}
m_SocketClient.Send(Encoding.Default.GetBytes(m_Content));
rcvCount = m_SocketClient.Receive(buffer);
Console.WriteLine(Encoding.Default.GetString(buffer, 0, rcvCount));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
private static string m_Content;
private static Socket m_SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
[Serializable]
public class Student
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
}
参考:
- https://www.jb51.net/article/204058.htm
- https://www.jb51.net/article/256530.htm