C# Socket编程
2014-01-16 19:29
218 查看
服务端(server side)
客户端(Client Side)
如果要在Unity中在web platform使用Socket编程(Window平台),需要做以下操作:
1、首先找到sockpol.exe文件。文件路径..\Unity\Editor\Data\Tools\SocketPolicyServer\sockpol.exe
2、然后打开cmd控制台,并且进入到sockpol.exe目录,打开sockpol服务,服务不能关闭。在cmd中使用netstat -ano命令可以看到一个843端口的服务。
3、在客户端连接(Unity中)的开始部分,加上下面这句代码。
sing System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace MyChatServer { internal class server { [STAThread] private static void Main(string[] args) { int recv; byte[] data = new byte[1024]; IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050); Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newSocket.Bind(ipEnd); newSocket.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newSocket.Accept(); IPEndPoint clientIp = client.RemoteEndPoint as IPEndPoint; Console.WriteLine("connect with client: " + clientIp.Address + " at port; " + clientIp.Port); string welcome = "welcome here"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); while (true) { data = new byte[1024]; recv = client.Receive(data); Console.WriteLine("recv = " + recv); if (recv == 0) { break; } Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); client.Send(data, recv, SocketFlags.None); } Console.WriteLine("Disconnected from " + clientIp.Address); client.Close(); newSocket.Close(); } } }
客户端(Client Side)
using System; using System.Net; using System.Net.Sockets; using System.Text; namespace tcpclient { /// <summary> /// Class1 的摘要说明。 /// </summary> class client { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // byte[] data=new byte[1024]; Socket newclient=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); Console.Write("please input the server ip:"); string ipadd=Console.ReadLine(); Console.WriteLine(); Console.Write("please input the server port:"); int port=Convert.ToInt32(Console.ReadLine()); IPEndPoint ie=new IPEndPoint(IPAddress.Parse(ipadd),port);//服务器的IP和端口 try { //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。 newclient.Connect(ie); } catch(SocketException e) { Console.WriteLine("unable to connect to server"); Console.WriteLine(e.ToString()); return; } int recv = newclient.Receive(data); string stringdata=Encoding.ASCII.GetString(data,0,recv); Console.WriteLine(stringdata); while(true) { string input=Console.ReadLine(); if(input=="exit") break; newclient.Send(Encoding.ASCII.GetBytes(input)); data=new byte[1024]; recv=newclient.Receive(data); stringdata=Encoding.ASCII.GetString(data,0,recv); Console.WriteLine(stringdata); } Console.WriteLine("disconnect from sercer"); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } } }
如果要在Unity中在web platform使用Socket编程(Window平台),需要做以下操作:
1、首先找到sockpol.exe文件。文件路径..\Unity\Editor\Data\Tools\SocketPolicyServer\sockpol.exe
2、然后打开cmd控制台,并且进入到sockpol.exe目录,打开sockpol服务,服务不能关闭。在cmd中使用netstat -ano命令可以看到一个843端口的服务。
3、在客户端连接(Unity中)的开始部分,加上下面这句代码。
Security.PrefetchSocketPolicy(ipAddress, 843); //端口号843固定,ipAddress为服务器的ip
相关文章推荐
- C#跳转网页7种方法
- winform(C#)的几种弹框
- C#获取汉字首字母
- C#调用dll,dll中执行C#端的回调函数
- c#中string.trimstart() 和string.trimend() 的用法
- C#一行一行读取DataSet 数据
- c#读写注册表示例分享
- C# 根据Word 模板生成word 文件
- 怎么复制DataTable过滤部分数据到另一个DataTable
- 【转载】C#中splitContainer用法
- (转)C# 中的委托和事件
- C#中delegate实现回调函数示例
- C#如何调试VC编译的DLL
- 针对C#调用OutLook发送邮件模块出现的各类问题解决方案
- c#中利用“|”运算组合多项
- C#向串口发指令
- C#读取Excel文件:通过OleDb连接,把excel文件作为数据源来读取
- C#反射概念以及实例详解
- C#下XML序列化出现System.InvalidOperationException的一种解释
- C#语法糖(Csharp Syntactic sugar)大汇总