附件地址:http://download.youkuaiyun.com/detail/lyh1112/5692845
Server端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace SocketServer
{
class Program
{
static Socket socketserver;
static Socket socketclient;
static Thread thread;
static void Main(string[] args)
{
SetSocket(IPAddress.Any, 3334);
}
private static void SetSocket(IPAddress address, int point)
{
//将网络端点表示为IP地址和端口 用于socket侦听时绑定
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 3334);
//使用指定的地址族、套接字类型和协议初始化 Socket 类的新实例。
socketserver = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//将所创建的Socket与IPEndPoint绑定
socketserver.Bind(ipe);
//设置套接字为收听模式 定队列中最多可容纳的等待接受的传入连接数为10
socketserver.Listen(10);
//在Socket上接收接入的连接
while (true)
{
socketclient = socketserver.Accept();
thread = new Thread(new ThreadStart(Program.doWork));
thread.Start();
}
}
private static void doWork()
{
Socket client = socketclient;
//客户端信息
IPEndPoint ipEndPoint = (IPEndPoint)client.RemoteEndPoint;
string address = ipEndPoint.Address.ToString();
string port = ipEndPoint.Port.ToString();
Console.WriteLine(address + ":" + port + " 已经连接.");
while (true)
{
Byte[] inBuffer = new Byte[(client.ReceiveBufferSize)];
string inbufstr = string.Empty;
string outbufstr = string.Empty;
try
{
client.Receive(inBuffer, client.ReceiveBufferSize, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环
}
catch
{
client.Close();
Console.WriteLine(address + ":" + port + " 的连接已关闭.");
return;
}
inbufstr = Encoding.Default.GetString(inBuffer);
inbufstr = inbufstr.TrimEnd('\0');
Console.WriteLine(address + ":" + port + " 说:");
Console.WriteLine(inbufstr);
outbufstr = "服务器收到消息:" + inbufstr;
Byte[] outBuffer = Encoding.Default.GetBytes(outbufstr);
client.Send(outBuffer, outBuffer.Length, SocketFlags.None);
}
}
}
}
client端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace SocketClient
{
class Program
{
public static Socket socketclient;
static void Main(string[] args)
{
SetClientSocket(IPAddress.Parse("127.0.0.1"), 3334);
}
private static void SetClientSocket(IPAddress targetIP,int port)
{
try
{
//将网络端点表示为IP地址和端口 用于socket侦听时绑定
IPEndPoint ipe = new IPEndPoint(targetIP, port);
socketclient = new Socket(ipe.AddressFamily,SocketType.Stream, ProtocolType.Tcp);
socketclient.Connect(ipe);
string outbufstr = string.Empty;
Byte[] inbuffer = new Byte[1024];
Byte[] outbuffer = new Byte[1024];
while (true)
{
outbufstr = Console.ReadLine();
outbuffer = Encoding.Default.GetBytes(outbufstr);
socketclient.Send(outbuffer, outbuffer.Length, SocketFlags.None);
Console.WriteLine("服务器响应:");
socketclient.Receive(inbuffer, 1024, SocketFlags.None);
string inbufstr = Encoding.Default.GetString(inbuffer);
Console.WriteLine(inbufstr.TrimEnd('\0').TrimEnd());
}
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
socketclient.Close();
}
}
}
}