C#實現(xiàn)Socket通信的解決方法
本文以實例詳述了C#實現(xiàn)Socket通信的解決方法,具體實現(xiàn)步驟如下:
1、首先打開VS新建兩個控制臺應用程序:
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ò)端點表示為IP地址和端口 用于socket偵聽時綁定
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("*.*.*.*"), 3001); //填寫自己電腦的IP或者其他電腦的IP,如果是其他電腦IP的話需將ConsoleApplication_socketServer工程放在對應的電腦上。
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);//如果接收的消息為空 阻塞 當前循環(huán)
Console.WriteLine("服務(wù)器說:");
Console.WriteLine(Encoding.ASCII.GetString(inBuffer));
}
}
catch
{
Console.WriteLine("服務(wù)未開啟!");
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 + " 連接過來了");
Byte[] inBuffer = new Byte[1024];
Byte[] outBuffer = new Byte[1024];
String inBufferStr;
String outBufferStr;
try
{
while (true)
{
s.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當前循環(huán)
inBufferStr = Encoding.ASCII.GetString(inBuffer);
Console.WriteLine(address + ":" + port + "說:");
Console.WriteLine(inBufferStr);
outBufferStr = Console.ReadLine();
outBuffer = Encoding.ASCII.GetBytes(outBufferStr);
s.Send(outBuffer, outBuffer.Length, SocketFlags.None);
}
}
catch
{
Console.WriteLine("客戶端已關(guān)閉!");
}
}
}
}
4、先運行ConsoleApplication_socketServer,后運行ConsoleApplication_socketClient就可以通信了。
本例給出了基本的實現(xiàn)代碼,讀者可以根據(jù)自身的需求進一步完成個性化功能。
相關(guān)文章
Unity技術(shù)手冊之Slider滑動器使用實例詳解
這篇文章主要為大家介紹了Unity技術(shù)手冊之Slider滑動器使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
C#利用IDbDataAdapter/IDataReader實現(xiàn)通用數(shù)據(jù)集獲取
這篇文章主要為大家詳細介紹了C#利用IDbDataAdapter/IDataReader實現(xiàn)通用數(shù)據(jù)集獲取的相關(guān)知識,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11

