C/S代码一例

在博客园下的又稍加改进成了现在的样子

Server code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ServerSocketDemo
{
class Program
{
static IPEndPoint serverEndPoint;
static IPEndPoint clientEndPoint;
static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static Socket clientSocket;


///
///设置终结点
///
public static void SetServerEndPoint()
{
//IPHostEntryhostEntry=newIPHostEntry();
IPAddress[] ipadddress = Dns.GetHostAddresses(Dns.GetHostName());

Console.WriteLine("serverip:{0}", ipadddress[0].ToString());
//IPAddress.Any
serverEndPoint = new IPEndPoint(ipadddress[0], 7788);

}
///
///监听
///
static void SetServerSocketListen()
{
//绑定
serverSocket.Bind(serverEndPoint);
//监听
serverSocket.Listen(20);
Console.WriteLine("开始在{0}:{1}上监听", serverEndPoint.Address.ToString(), serverEndPoint.Port.ToString());

//新的客户端socket

clientSocket = serverSocket.Accept();

//有新连接创建时
clientEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
Console.WriteLine("connectwithclient:" + clientEndPoint.Address + "atport:" + clientEndPoint.Port);
//向客户端发送欢迎信息
string welcome = "welcome!";
byte[] welcomeByte = Encoding.UTF8.GetBytes(welcome);
clientSocket.Send(welcomeByte);

ReviceData();
}
///
///接收数据
///
static void ReviceData()
{
///循环接收数据
while (true)
{
if (clientSocket != null)
{
byte[] data = new byte[1024];
//将数据缓存到data
int buffer = clientSocket.Receive(data);
if (buffer > 0)
{
//数据不到1024时使用Encoding.UTF8.GetString(byte[])回接收大片空白
string strData = Encoding.UTF8.GetString(data, 0, buffer);//只转换相应的数据大小
Console.WriteLine("from{0}:{1}", ((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString(), ((IPEndPoint)clientSocket.RemoteEndPoint).Port.ToString());
Console.WriteLine(strData);

//将数据返回
int recv = clientSocket.Receive(data);
clientSocket.Send(data, recv, SocketFlags.None);
// client.Send(data, recv, SocketFlags.None);
clientSocket.Send(data, buffer, SocketFlags.None);

}
}
}
}
static void Main(string[] args)
{
SetServerEndPoint();
Thread serverThrad = new Thread(new ThreadStart(SetServerSocketListen));
serverThrad.IsBackground = true;
serverThrad.Start();
serverThrad.Join();

}
}
}

Client code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ClientSocketDemo
{
class Program
{
static IPAddress[] ipadress;
static IPEndPoint clientEndPoint;
static IPEndPoint servetEndPoint;
static string info;
static Socket clientSocket;//=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

static void send()
{
try
{
//循环发送
while (true)
{
//客户端发送信息
info = Console.ReadLine();
Byte[] infobyte = Encoding.UTF8.GetBytes(info);
clientSocket.Send(infobyte);

//接收返回的信息
byte[] data = new byte[1024];
int recv = clientSocket.Receive(data);
string stringdata = Encoding.UTF8.GetString(data, 0, recv);
Console.WriteLine(stringdata);

}
}
finally
{

clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}


static void Main(string[] args)
{
IPAddress serverIpaddress;
//服务节点
IPAddress.TryParse("192.168.70.242", out serverIpaddress);
servetEndPoint = new IPEndPoint(serverIpaddress, 7788);

////客户节点
ipadress = Dns.GetHostAddresses(Dns.GetHostName());
clientEndPoint = new IPEndPoint(ipadress[0], 8877);

//创建客户端socket
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//连接
clientSocket.Connect(servetEndPoint);
byte[] data = new byte[1024];
//将接受的数据缓存到data
int recv = clientSocket.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, recv);
//输出欢迎信息证明连接上。
Console.WriteLine(stringdata);


Thread th = new Thread(new ThreadStart(send));
th.Start();


}
}
}

采用vs2008创建控制台应用程序,经测试,只能传输一条数据。即从服务器传给客户端welcome!和从客户端传给服务器一句任意的话。
原文地址:https://www.cnblogs.com/bymeet/p/client_Server.html