客户端的实现相对来说很简单,这里的IP地址要根据具体情况进行修改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace SocketClient
{
class Program
{
private static byte[] result = new byte[1024];
static void Main(string[] args)
{
#region IP和端口的初始化设置
//设定服务器IP地址
IPAddress ip = IPAddress.Parse("192.168.1.105");
//设置指定的地址族,套接字类型和协议初始化
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, 8885));//配置服务器IP与端口
Console.WriteLine("连接服务器成功");
}
catch
{
Console.WriteLine("连接服务器失败,请按