C#實(shí)現(xiàn)Socket通信的解決方法
本文以實(shí)例詳述了C#實(shí)現(xiàn)Socket通信的解決方法,具體實(shí)現(xiàn)步驟如下:
1、首先打開(kāi)VS新建兩個(gè)控制臺(tái)應(yīng)用程序:
ConsoleApplication_socketServer和ConsoleApplication_socketClient。
2、在ConsoleApplication_socketClient中輸入以下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication_socketClient
{
class Program
{
static Socket clientSocket;
static void Main(string[] args)
{
//將網(wǎng)絡(luò)端點(diǎn)表示為IP地址和端口 用于socket偵聽(tīng)時(shí)綁定
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("*.*.*.*"), 3001); //填寫自己電腦的IP或者其他電腦的IP,如果是其他電腦IP的話需將ConsoleApplication_socketServer工程放在對(duì)應(yīng)的電腦上。
clientSocket = new Socket(ipep.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
//將Socket連接到服務(wù)器
try
{
clientSocket.Connect(ipep);
String outBufferStr;
Byte[] outBuffer = new Byte[1024];
Byte[] inBuffer = new Byte[1024];
while (true)
{
//發(fā)送消息
outBufferStr = Console.ReadLine();
outBuffer = Encoding.ASCII.GetBytes(outBufferStr);
clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);
//接收服務(wù)器端信息
clientSocket.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當(dāng)前循環(huán)
Console.WriteLine("服務(wù)器說(shuō):");
Console.WriteLine(Encoding.ASCII.GetString(inBuffer));
}
}
catch
{
Console.WriteLine("服務(wù)未開(kāi)啟!");
Console.ReadLine();
}
}
}
}
3、在ConsoleApplication_socketServer中輸入以下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApplication_socketServer
{
class Program
{
static Socket serverSocket;
static Socket clientSocket;
static Thread thread;
static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001);
serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(ipep);
serverSocket.Listen(10);
while (true)
{
clientSocket = serverSocket.Accept();
thread = new Thread(new ThreadStart(doWork));
thread.Start();
}
}
private static void doWork()
{
Socket s = clientSocket;//客戶端信息
IPEndPoint ipEndPoint = (IPEndPoint)s.RemoteEndPoint;
String address = ipEndPoint.Address.ToString();
String port = ipEndPoint.Port.ToString();
Console.WriteLine(address + ":" + port + " 連接過(guò)來(lái)了");
Byte[] inBuffer = new Byte[1024];
Byte[] outBuffer = new Byte[1024];
String inBufferStr;
String outBufferStr;
try
{
while (true)
{
s.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當(dāng)前循環(huán)
inBufferStr = Encoding.ASCII.GetString(inBuffer);
Console.WriteLine(address + ":" + port + "說(shuō):");
Console.WriteLine(inBufferStr);
outBufferStr = Console.ReadLine();
outBuffer = Encoding.ASCII.GetBytes(outBufferStr);
s.Send(outBuffer, outBuffer.Length, SocketFlags.None);
}
}
catch
{
Console.WriteLine("客戶端已關(guān)閉!");
}
}
}
}
4、先運(yùn)行ConsoleApplication_socketServer,后運(yùn)行ConsoleApplication_socketClient就可以通信了。
本例給出了基本的實(shí)現(xiàn)代碼,讀者可以根據(jù)自身的需求進(jìn)一步完成個(gè)性化功能。
相關(guān)文章
C#自定義類型強(qiáng)制轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了C#自定義類型強(qiáng)制轉(zhuǎn)換的方法,實(shí)例分析了C#類型轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-05-05
WPF實(shí)現(xiàn)Badge標(biāo)識(shí)的示例代碼
這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)Badge標(biāo)識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-06-06
Unity技術(shù)手冊(cè)之Slider滑動(dòng)器使用實(shí)例詳解
這篇文章主要為大家介紹了Unity技術(shù)手冊(cè)之Slider滑動(dòng)器使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
C#?Timer控件學(xué)習(xí)之使用Timer解決按鈕冪等性問(wèn)題
Timer控件又稱定時(shí)器控件或計(jì)時(shí)器控件,該控件的主要作用是按一定的時(shí)間間隔周期性地觸發(fā)一個(gè)名為Tick的事件,因此在該事件的代碼中可以放置一些需要每隔一段時(shí)間重復(fù)執(zhí)行的程序段,這篇文章主要介紹了關(guān)于C#使用Timer解決按鈕冪等性問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-10-10
C#利用IDbDataAdapter/IDataReader實(shí)現(xiàn)通用數(shù)據(jù)集獲取
這篇文章主要為大家詳細(xì)介紹了C#利用IDbDataAdapter/IDataReader實(shí)現(xiàn)通用數(shù)據(jù)集獲取的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
C#備忘錄人生存檔的設(shè)計(jì)模式實(shí)例
這篇文章主要為大家介紹了C#設(shè)計(jì)模式中備忘錄模式的實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

