C#网络编程实例
客户端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketClient
{
class Program
{
static void Main(string[] args)
{
//保存输入要发送的字符串
string input;
//要连接的远程IP
IPAddress remoteHost = IPAddress.Parse("172.16.86.90");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(remoteHost, 8080);
//把地址绑定到Socket
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接远程服务器
try
{
clientSocket.Connect(iep);
Console.WriteLine("请输入您要发送的字符串");
//保存输入的字符串
input = Console.ReadLine();
//用字节数组保存要发送的字符串
byte[] message = System.Text.Encoding.Unicode.GetBytes(input);
//新建一个NetworkStream对象发送数据
NetworkStream netstream = new NetworkStream(clientSocket);
//向服务器端发送message内容
netstream.Write(message, 0, message.Length);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
catch (System.Exception ex)
{
Console.WriteLine("服务器端连接失败");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketClient
{
class Program
{
static void Main(string[] args)
{
//保存输入要发送的字符串
string input;
//要连接的远程IP
IPAddress remoteHost = IPAddress.Parse("172.16.86.90");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(remoteHost, 8080);
//把地址绑定到Socket
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接远程服务器
try
{
clientSocket.Connect(iep);
Console.WriteLine("请输入您要发送的字符串");
//保存输入的字符串
input = Console.ReadLine();
//用字节数组保存要发送的字符串
byte[] message = System.Text.Encoding.Unicode.GetBytes(input);
//新建一个NetworkStream对象发送数据
NetworkStream netstream = new NetworkStream(clientSocket);
//向服务器端发送message内容
netstream.Write(message, 0, message.Length);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
catch (System.Exception ex)
{
Console.WriteLine("服务器端连接失败");
}
}
}
}
服务器端代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
class Program
{
static void Main(string[] args)
{
//本机IP
IPAddress ip = IPAddress.Parse("172.16.86.90");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(ip, 8080);
//创建Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定Socket
socket.Bind(iep);
//服务器已经做好接收任何连接的准备
socket.Listen(10);
while (true)
{
//执行accept方法
Socket Client = socket.Accept();
byte[] message = new byte[1024];
NetworkStream networkStream = new NetworkStream(Client);
int len = networkStream.Read(message, 0, message.Length);
//byte数组转换成string
string output = System.Text.Encoding.Unicode.GetString(message);
Console.WriteLine("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:"+output);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
class Program
{
static void Main(string[] args)
{
//本机IP
IPAddress ip = IPAddress.Parse("172.16.86.90");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(ip, 8080);
//创建Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定Socket
socket.Bind(iep);
//服务器已经做好接收任何连接的准备
socket.Listen(10);
while (true)
{
//执行accept方法
Socket Client = socket.Accept();
byte[] message = new byte[1024];
NetworkStream networkStream = new NetworkStream(Client);
int len = networkStream.Read(message, 0, message.Length);
//byte数组转换成string
string output = System.Text.Encoding.Unicode.GetString(message);
Console.WriteLine("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:"+output);
}
}
}
}