C#实现Socket通信的解决方法
作者:shichen2014 时间:2022-06-01 22:06:00
本文以实例详述了C#实现Socket通信的解决方法,具体实现步骤如下:
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)
{
//将网络端点表示为IP地址和端口 用于socket侦听时绑定
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("*.*.*.*"), 3001); //填写自己电脑的IP或者其他电脑的IP,如果是其他电脑IP的话需将ConsoleApplication_socketServer工程放在对应的电脑上。
clientSocket = new Socket(ipep.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
//将Socket连接到服务器
try
{
clientSocket.Connect(ipep);
String outBufferStr;
Byte[] outBuffer = new Byte[1024];
Byte[] inBuffer = new Byte[1024];
while (true)
{
//发送消息
outBufferStr = Console.ReadLine();
outBuffer = Encoding.ASCII.GetBytes(outBufferStr);
clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);
//接收服务器端信息
clientSocket.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环
Console.WriteLine("服务器说:");
Console.WriteLine(Encoding.ASCII.GetString(inBuffer));
}
}
catch
{
Console.WriteLine("服务未开启!");
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);//如果接收的消息为空 阻塞 当前循环
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("客户端已关闭!");
}
}
}
}
4、先运行ConsoleApplication_socketServer,后运行ConsoleApplication_socketClient就可以通信了。
本例给出了基本的实现代码,读者可以根据自身的需求进一步完成个性化功能。
标签:C#,Socket
0
投稿
猜你喜欢
Eclipse下Javassist正确使用方法代码解析
2021-07-24 18:43:57
Android Notification使用方法详解
2023-12-14 13:03:17
C# WinForm中Panel实现用鼠标操作滚动条的实例方法
2021-08-08 01:52:42
C语言枚举(enum)和联合(union)实例分享
2023-06-17 01:56:42
maven之packaging标签的使用
2021-10-25 05:14:42
Netty分布式Future与Promise执行回调相关逻辑剖析
2021-08-18 23:33:50
Java获取随机数的3种方法
2022-08-04 02:51:01
如何使用BufferedReader循环读文件
2022-04-20 17:32:23
Mybatis注解增删改查的实例代码
2022-03-31 01:26:15
从Cocos2d-x2迁移到Cocos2d-x3的过程分享
2021-06-07 23:52:28
在Winform程序中使用Spire.Pdf实现页面添加印章功能的实现
2022-05-29 16:57:27
java8实现List中对象属性的去重方法
2023-08-30 20:50:48
实例讲解Java中动态代理和反射机制
2022-06-29 04:00:50
Java中BigDecimal的加减乘除、比较大小与使用注意事项
2022-02-17 21:24:09
Android实现EditText的富文本编辑
2022-04-14 21:44:29
Servlet实现文件的上传与下载
2023-08-08 06:13:43
Flutter实现底部导航栏创建详解
2023-09-29 10:01:04
Spring中bean集合注入的方法详解
2022-08-26 07:37:41
学习Java HashMap,看这篇就够了
2023-11-11 11:18:56
java睡眠排序算法示例实现
2022-04-01 00:13:32